1.11M Members

Python Logical "OR":

 
0
 

I am searching for the logical Python logical operator "or"; While C++ is:

1 || 4 (using pipes)

I assume Python would simply be "or", as in:

1 or 4

Or something like this:

if var1 == "image0.GIF" or "image1.GIF" or "image2.GIF":
varValue = 10

Is this syntax for Python "or" correct in my examples?

Thank-you in advance.

sharky_machine

 
0
 

Python, in an attempt to be more readable, uses 'and' and 'or' rather then '&&' and '||'. I think some international versions of C++ also have gone to this, because not all international keyboards have the '&' or "|" characters.

In your case you would have to use ...

if var1 == "image0.GIF" or var1 ==  "image1.GIF" or var1 == "image2.GIF":
    varValue = 10
 
1
 

Right. Notice the subtle difference and be careful about it: the original code is legitimate Python, but it doesn't mean what one might think it means.

if var1 == "image0.GIF" or "image1.GIF" or "image2.GIF":
     varValue = 10

will take the value of 'var1 == "image0.GIF"' (which might be True or False), || that with the value of "image1.GIF" (which is always True!), and then quit because a True state is reached. As a result, varValue will always be set to 10 regardless of the value of var1.

whereas

if var1 == "image0.GIF" or var1 == "image1.GIF" or var1 == "image2.GIF":
    varValue = 10

will take the values of the conditionals and || them together, which is usually what is intended and will result in the expected action of setting varValue to 10 if var1 is one of the right values.

Even better:

if var1 in ['image0.GIF', 'image1.GIF', 'image2.GIF']:
   varValue = 10

which eliminates the need for multiple conditionals, and is more easily extended to add additional .GIFs.

Jeff

'beware of geeks bare in GIFs!'

 
0
 

Right. Notice the subtle difference and be careful about it: the original code is legitimate Python, but it doesn't mean what one might think it means.

if var1 == "image0.GIF" or "image1.GIF" or "image2.GIF":
     varValue = 10

will take the value of 'var1 == "image0.GIF"' (which might be True or False), || that with the value of "image1.GIF" (which is always True!), and then quit because a True state is reached. As a result, varValue will always be set to 10 regardless of the value of var1.

whereas

if var1 == "image0.GIF" or var1 == "image1.GIF" or var1 == "image2.GIF":
    varValue = 10

will take the values of the conditionals and || them together, which is usually what is intended and will result in the expected action of setting varValue to 10 if var1 is one of the right values.

Even better:

if var1 in ['image0.GIF', 'image1.GIF', 'image2.GIF']:
   varValue = 10

which eliminates the need for multiple conditionals, and is more easily extended to add additional .GIFs.

Jeff

'beware of geeks bare in GIFs!'

Thank-you for explaining that all to me. It is easy to make errors in situations like this (and I usually do :o )

sharky_machine

Question Answered as of 7 Years Ago by vegaseat and jrcagle
 
0
 

Not to worry. The reason I caught it is that I've made that error more than once ... :-)

Jeff

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article