Python “in” and “==” confusion
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
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 '18 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 '18 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 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 '18 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 '18 at 18:46
|
show 3 more comments
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 '18 at 18:13
petepete
7262827
7262827
@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 '18 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 '18 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 at 18:27
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 18:46
|
show 3 more comments
2 Answers
2
active
oldest
votes
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 |
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 usein
on a non iterable bool type ...
– Patrick Artner
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 at 18:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 18:42
answered Nov 21 '18 at 18:23
Patrick ArtnerPatrick Artner
22.7k62243
22.7k62243
add a comment |
add a comment |
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 usein
on a non iterable bool type ...
– Patrick Artner
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 at 18:33
add a comment |
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 usein
on a non iterable bool type ...
– Patrick Artner
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 at 18:33
add a comment |
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 '18 at 18:35
answered Nov 21 '18 at 18:31
Sanchit KumarSanchit Kumar
34319
34319
1
comparing a string with a bool is perfectly fine - the error is trying to usein
on a non iterable bool type ...
– Patrick Artner
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 at 18:33
add a comment |
1
comparing a string with a bool is perfectly fine - the error is trying to usein
on a non iterable bool type ...
– Patrick Artner
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 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 '18 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 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
Comparing a string to a Boolean does not throw an error.
– roganjosh
Nov 21 '18 at 18:32
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 at 18:33
My bad, I put it in wrong words.
– Sanchit Kumar
Nov 21 '18 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.
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 '18 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 '18 at 18:25
@roganjosh this here? stackoverflow.com/questions/6074018/…
– Patrick Artner
Nov 21 '18 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 '18 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 '18 at 18:46