Python Logical "OR":
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
Related Article: python and mySQLdb module
is a solved Python discussion thread by chupacabra that has 2 replies and was last updated 6 years ago.
mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
Skill Endorsements: 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
vegaseat
DaniWeb's Hypocrite
6,478 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36
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!'
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 160
Skill Endorsements: 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
mattyd
Posting Maven
2,607 posts since Oct 2006
Reputation Points: 105
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 6 Years Ago by
jrcagle
and
vegaseat Not to worry. The reason I caught it is that I've made that error more than once ... :-)
Jeff
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 160
Skill Endorsements: 0