Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
True in 'in' != True?
#1
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?
Reply
#2
rainbow = ['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
if 'red' in rainbow:
    print('Congrats')
else:
    print('No red in rainbow')
Output:
Congrats
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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
Reply
#4
(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:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
@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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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.
Reply
#8
"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.
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Boolean: Anybody know why is this True? LilD 8 1,808 Feb-06-2022, 09:15 AM
Last Post: LilD
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,037 Jan-09-2022, 02:39 AM
Last Post: Python84
  Question on True expressions mgreen2251660 2 1,911 Jun-25-2021, 04:49 PM
Last Post: Yoriz
  pathlib destpath.exists() true even file does not exist NaN 9 4,565 Dec-01-2020, 12:43 PM
Last Post: NaN
  While True loop help Nickd12 2 1,957 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  path.exists returning True when it shouldn't natha18 0 1,457 Sep-21-2020, 01:04 PM
Last Post: natha18
  while True error Nickd12 2 2,029 Sep-13-2020, 06:29 AM
Last Post: Nickd12
  Recursive function returns None, when True is expected akar 0 3,353 Sep-07-2020, 07:58 PM
Last Post: akar
  isinstance() always return true for object type check Yoki91 2 2,503 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  Is using while True loop good? escape_freedom13 5 3,465 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020