It seems like when I use boolean 'in', when it is true,
it is not equal to 'True'
I would appreciate any help or explanation
An example that I have is:
rainbow = "red, orange, yellow, green, blue, indigo, violet"
if 'red' in rainbow == True:
print("Congrats")
I see print('red' in rainbow) gives 'True'
and type('red' in rainbow) gives and bool
but print('red' in rainbow == True) gives me False
Why? and how can I get "Congrats" to be printed?
rainbow = ['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
if 'red' in rainbow:
print('Congrats')
else:
print('No red in rainbow')
Output:
Congrats
(May-27-2020, 10:30 PM)hkim10 Wrote: [ -> ]It seems like when I use boolean 'in', when it is true,
it is not equal to 'True'
I would appreciate any help or explanation
An example that I have is:
rainbow = "red, orange, yellow, green, blue, indigo, violet"
if 'red' in rainbow == True:
print("Congrats")
I see print('red' in rainbow) gives 'True'
and type('red' in rainbow) gives and bool
but print('red' in rainbow == True) gives me False
Why? and how can I get "Congrats" to be printed?
(May-27-2020, 10:42 PM)menator01 Wrote: [ -> ]rainbow = ['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
if 'red' in rainbow:
print('Congrats')
else:
print('No red in rainbow')
Output:
Congrats
Thank you for the response.
I was wondering more about why bool True by 'in' is not equal to True tho
(May-28-2020, 01:19 AM)hkim10 Wrote: [ -> ]I was wondering more about why bool True by 'in' is not equal to True tho
Operator precedence - in and == have same precedence
comparison operators can be chained arbitrarily - so
if 'red' in rainbow == True
is same as
if 'red' in rainbow and rainbow == True
, which is evaluated to
if True and False
which in turn is evaluated
False
Finally, as already advised, don't compare to boolean -
if 'red' in rainbow:
is enough.
Also from PEP8:
Quote:Don't compare boolean values to True or False using ==:
# Correct:
if greeting:
# Wrong:
if greeting == True:
Worse:
# Wrong:
if greeting is True:
(May-28-2020, 01:19 AM)hkim10 Wrote: [ -> ] (May-27-2020, 10:30 PM)hkim10 Wrote: [ -> ]It seems like when I use boolean 'in', when it is true,
it is not equal to 'True'
I would appreciate any help or explanation
An example that I have is:
rainbow = "red, orange, yellow, green, blue, indigo, violet"
if 'red' in rainbow == True:
print("Congrats")
I see print('red' in rainbow) gives 'True'
and type('red' in rainbow) gives and bool
but print('red' in rainbow == True) gives me False
Why? and how can I get "Congrats" to be printed?
(May-27-2020, 10:42 PM)menator01 Wrote: [ -> ]rainbow = ['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
if 'red' in rainbow:
print('Congrats')
else:
print('No red in rainbow')
Output:
Congrats
Thank you for the response.
I was wondering more about why bool True by 'in' is not equal to True tho
Logical opposites? I think it'd be like saying if red in rainbow True == True which would be like saying if red in rainbow != True
rainbow = "red, orange, yellow, green, blue, indigo, violet"
if 'red' in rainbow != True:
print("Congrats"
This would result in Congrats being printed.
@
eightnine, please, check my answer. what you suggest is just trying to get the expected result in this particular case, without understanding how it works.
(May-28-2020, 03:58 AM)buran Wrote: [ -> ]@eightnine, please, check my answer. what you suggest is just trying to get the expected result in this particular case, without understanding how it works.
Yea, we posted at the same time. After I saw your post, I googled and found an article that explained it properly. Thanks.
"if x is True" does have a use, though a fairly restricted use. "if x" will evaluate as True for many different values of x whereas "if x is True" is only True if x is True. I could see this possibly coming up in a parser, but the need for such a thing would be rare.
"if x" evaluates to False when x is None, so "if x is None" looks to be as bad as "if x is True", but it is not. Just as many thing evaluate to True, many things also evaluate to False. Zero, a null string, an empty list, False and Null all evaluate to False when used as the condition in an if statement. Of these, only one "is None", a special value used to mark when something doesn't have a value.
(May-28-2020, 03:55 AM)buran Wrote: [ -> ] (May-28-2020, 01:19 AM)hkim10 Wrote: [ -> ]I was wondering more about why bool True by 'in' is not equal to True tho
Operator precedence - in and == have same precedence
comparison operators can be chained arbitrarily - so if 'red' in rainbow == True
is same as if 'red' in rainbow and rainbow == True
, which is evaluated to if True and False
which in turn is evaluated False
Finally, as already advised, don't compare to boolean - if 'red' in rainbow:
is enough.
Also from PEP8:
Quote:Don't compare boolean values to True or False using ==:
# Correct:
if greeting:
# Wrong:
if greeting == True:
Worse:
# Wrong:
if greeting is True:
Thank you so much for the explanation and the reference! It helped a lot.