1,105,406 Community Members

Python Logical "OR":

Member Avatar
mattyd
Posting Maven
2,677 posts since Oct 2006
Reputation Points: 76 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
Featured
 
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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
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
Member Avatar
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 77 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 0 [?]
 
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!'

Member Avatar
mattyd
Posting Maven
2,677 posts since Oct 2006
Reputation Points: 76 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
Featured
 
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
Member Avatar
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 77 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 0 [?]
 
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