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

Recommended Answers

All 7 Replies

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

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!'

commented: Well-written and thorough. Thanks. +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!'

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

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

Jeff

Hi!

I am using a similar condition with If. But I am worried to see my conditions are not being checked.

Using the following code, my intention is to check whether the variable 'char' belongs to either small_case OR camel_case OR space.

s = "We promptly judged antique ivory buckles for the next prize"
small_case = "abcdefghijklmnopqrstuvwxyz"
space = " "
camel_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def check(n):
    print(small_case)
    if n in [small_case, camel_case, space]:
        result = 'pangram'
    else:
        result = 'not pangram'
for char in s:
    check(char)
print (result)

First of all, I am doubtful to use
if n in [small_case, camel_case, space]
is it permitted?

@Madhu_6 It is permitted, but it does not have the meaning you expect. Here is an experiment

>>> small_case = "abcdefghijklmnopqrstuvwxyz"
>>> space = " "
>>> camel_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"    
>>> "k" in [small_case, camel_case, space]
False
>>> "K" in [small_case, camel_case, space]
False
>>> "abcdef" in [small_case, camel_case, space]
False
>>> "abcdefghijklmnopqrstuvwxyz" in [small_case, camel_case, space]
True

A solution is to use a set() object

>>> accept = set(small_case + space + camel_case)
>>> "k" in accept
True
>>> "K" in accept
True
>>> "foo" in accept
False

Please don't revive 10 years old threads, start your own threads with your questions instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.