Python “in” and “==” confusion
up vote
5
down vote
favorite
print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))
The output is
True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable
If line 2 is neither line 3 nor line 4, then what is it? How does it get False?
python
|
show 3 more comments
up vote
5
down vote
favorite
print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))
The output is
True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable
If line 2 is neither line 3 nor line 4, then what is it? How does it get False?
python
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
1
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
1
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46
|
show 3 more comments
up vote
5
down vote
favorite
up vote
5
down vote
favorite
print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))
The output is
True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable
If line 2 is neither line 3 nor line 4, then what is it? How does it get False?
python
print('a' in 'aa')
print('a' in 'aa' == True)
print(('a' in 'aa') == True)
print('a' in ('aa' == True))
The output is
True
False
True
Traceback (most recent call last):
File "main.py", line 6, in <module>
print('a' in ('aa' == True))
TypeError: argument of type 'bool' is not iterable
If line 2 is neither line 3 nor line 4, then what is it? How does it get False?
python
python
asked Nov 21 at 18:13
pete
7112827
7112827
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
1
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
1
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46
|
show 3 more comments
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
1
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
1
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
1
1
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
1
1
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46
|
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
According to Expressions
print('a' in 'aa' == True)
is evaluated as
'a' in 'aa' and 'aa' == True
which is False.
See
print("a" in "aa" and "aa" == True)
==> False
The rest is trivial - it helps to keep operator precedence in mind to figure them out.
Similar ones:
- Multiple comparison operators in single statement
- Why does the expression 0 < 0 == 0 return False in Python?
with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.
add a comment |
up vote
1
down vote
Case 1 : it's simple the answers is True.
print('a' in 'aa')
Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.
print('a' in 'aa' == True)
Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True
print(('a' in 'aa') == True)
Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.
print('a' in ('aa' == True))
1
comparing a string with a bool is perfectly fine - the error is trying to useinon a non iterable bool type ...
– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
According to Expressions
print('a' in 'aa' == True)
is evaluated as
'a' in 'aa' and 'aa' == True
which is False.
See
print("a" in "aa" and "aa" == True)
==> False
The rest is trivial - it helps to keep operator precedence in mind to figure them out.
Similar ones:
- Multiple comparison operators in single statement
- Why does the expression 0 < 0 == 0 return False in Python?
with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.
add a comment |
up vote
7
down vote
accepted
According to Expressions
print('a' in 'aa' == True)
is evaluated as
'a' in 'aa' and 'aa' == True
which is False.
See
print("a" in "aa" and "aa" == True)
==> False
The rest is trivial - it helps to keep operator precedence in mind to figure them out.
Similar ones:
- Multiple comparison operators in single statement
- Why does the expression 0 < 0 == 0 return False in Python?
with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.
add a comment |
up vote
7
down vote
accepted
up vote
7
down vote
accepted
According to Expressions
print('a' in 'aa' == True)
is evaluated as
'a' in 'aa' and 'aa' == True
which is False.
See
print("a" in "aa" and "aa" == True)
==> False
The rest is trivial - it helps to keep operator precedence in mind to figure them out.
Similar ones:
- Multiple comparison operators in single statement
- Why does the expression 0 < 0 == 0 return False in Python?
with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.
According to Expressions
print('a' in 'aa' == True)
is evaluated as
'a' in 'aa' and 'aa' == True
which is False.
See
print("a" in "aa" and "aa" == True)
==> False
The rest is trivial - it helps to keep operator precedence in mind to figure them out.
Similar ones:
- Multiple comparison operators in single statement
- Why does the expression 0 < 0 == 0 return False in Python?
with different statements. I flagged for dupe but the UI is wonky - I answered non the less to explain why yours exactly printed what it did.
edited Nov 21 at 18:42
answered Nov 21 at 18:23
Patrick Artner
18.9k51940
18.9k51940
add a comment |
add a comment |
up vote
1
down vote
Case 1 : it's simple the answers is True.
print('a' in 'aa')
Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.
print('a' in 'aa' == True)
Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True
print(('a' in 'aa') == True)
Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.
print('a' in ('aa' == True))
1
comparing a string with a bool is perfectly fine - the error is trying to useinon a non iterable bool type ...
– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
add a comment |
up vote
1
down vote
Case 1 : it's simple the answers is True.
print('a' in 'aa')
Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.
print('a' in 'aa' == True)
Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True
print(('a' in 'aa') == True)
Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.
print('a' in ('aa' == True))
1
comparing a string with a bool is perfectly fine - the error is trying to useinon a non iterable bool type ...
– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
add a comment |
up vote
1
down vote
up vote
1
down vote
Case 1 : it's simple the answers is True.
print('a' in 'aa')
Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.
print('a' in 'aa' == True)
Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True
print(('a' in 'aa') == True)
Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.
print('a' in ('aa' == True))
Case 1 : it's simple the answers is True.
print('a' in 'aa')
Case 2 : This operation is evaluated as 'a' in 'aa' and 'aa' == True, so obviously it will return false.
print('a' in 'aa' == True)
Case 3: Now because we have () enclosing ('a' in 'aa') and the precedence of () is highest among all so first 'a' in 'aa' is evaluated as True and then True == True
print(('a' in 'aa') == True)
Case 4 : Same as above because of precedence of (), its evaluated as 'aa' == True, which will result in error as it tries to apply in on a non iterable that is bool value.
print('a' in ('aa' == True))
edited Nov 21 at 18:35
answered Nov 21 at 18:31
Sanchit Kumar
31117
31117
1
comparing a string with a bool is perfectly fine - the error is trying to useinon a non iterable bool type ...
– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
add a comment |
1
comparing a string with a bool is perfectly fine - the error is trying to useinon a non iterable bool type ...
– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
1
1
comparing a string with a bool is perfectly fine - the error is trying to use
in on a non iterable bool type ...– Patrick Artner
Nov 21 at 18:32
comparing a string with a bool is perfectly fine - the error is trying to use
in on a non iterable bool type ...– Patrick Artner
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 at 18:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418231%2fpython-in-and-confusion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@PatrickArtner I'm finding it unusually difficult to find the canonical I'm thinking of. It's due to chaining but I can't find the link :/
– roganjosh
Nov 21 at 18:23
That's the one you originally flagged with but I'm sure there is a canonical. Where is it?! :P
– roganjosh
Nov 21 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 at 18:27
1
@PatrickArtner it got hammered and they presumably decided to reverse that, but now it shows no close votes to me.
– roganjosh
Nov 21 at 18:42
1
@PatrickArtner i saw timgeb as the second closer. It must be a hammer. I'm not sure how to view that history, though.
– roganjosh
Nov 21 at 18:46