1

I am new to Python and would like to find out if I am able to use multiple of Boolean operators in one expression such as:

taco = int(input("Enter Tacos:\n"))

if taco == (taco == 3) or (taco == 5) or (taco == 7):
    print("Just an example.")
else:
    print("No taco for you.")

Instead of using two "or" operators in the statement, is it possible to make it smaller? Maybe something like taco == 3 or 5 or 7?

Please don't laugh at me; I'm really new to this.

zondo
  • 19,901
  • 8
  • 44
  • 83

2 Answers2

2

In this case you can write taco in (3, 5, 7) (though this is not a boolean or expression).

Jason S
  • 13,538
  • 2
  • 37
  • 42
  • In python3.5, if you're testing against a container of immutable literals, you're slightly better off using a set: `taco in {3, 5, 7}` – mgilson Jul 02 '16 at 03:36
  • Sure, but for me in this case it saves about 15ns :) Better if you have a large collection possibly. – Jason S Jul 02 '16 at 03:39
  • saves 15ns, but you loose nothing, but typing parentheses - and helps setting the mindset. It actually works in Python 2.7 as well. – jsbueno Jul 02 '16 at 03:40
  • Yeah, generally, if you're writing the literals by hand, the time savings aren't going to be much since the collections will always be small. I guess for me, it's more about the semantics. `set`s are made to excel at membership tests. `tuple` are generally used for other things. – mgilson Jul 02 '16 at 03:40
  • Thank you all for the assistance. This was for a class assignment and I was getting difficulty with it. – PYTH0NP0WERED Jul 02 '16 at 12:25
0

You can make the possible taco Options into a list. For example:

Options =[3,5,7]

Then your able to do

If taco in Options: Print ("example")

Or perhaps more consise you can also do

If tacos in [3,5,7]: Print ()