Why are 'and/or' operations in this Python statement behaving unexpectedly?
Clash Royale CLAN TAG#URR8PPP
I have a conceptual question about Python. This is the code
list1=['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
sub1 = "teacher"
sub2 = "sales"
ans=
for item in list1:
if (sub1 and sub2) in item:
ans.append(item)
Here, I expect the list to be empty as none of the items satisfy the condition if sub1 and sub2 in item:
But when I print the list I get the output#1 as this
>>> ans
['salesperson', 'sales manager'] # I expected an empty list here
Also, when I use or
instead of and
as given below
for item in list1:
if (sub1 or sub2) in item:
ans.append(item)
the output#2 I get is
>>> ans
['schoolteacher', 'mathematics teacher'] # I expected a list of words containing sub1 or sub2 as their substrings
I saw a similar looking solution here, but it does not exactly solve my problem. Both the times I get a result which I do not expect while using and
and or
. Why is this happening during both these operations?
python logical-operators
add a comment |
I have a conceptual question about Python. This is the code
list1=['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
sub1 = "teacher"
sub2 = "sales"
ans=
for item in list1:
if (sub1 and sub2) in item:
ans.append(item)
Here, I expect the list to be empty as none of the items satisfy the condition if sub1 and sub2 in item:
But when I print the list I get the output#1 as this
>>> ans
['salesperson', 'sales manager'] # I expected an empty list here
Also, when I use or
instead of and
as given below
for item in list1:
if (sub1 or sub2) in item:
ans.append(item)
the output#2 I get is
>>> ans
['schoolteacher', 'mathematics teacher'] # I expected a list of words containing sub1 or sub2 as their substrings
I saw a similar looking solution here, but it does not exactly solve my problem. Both the times I get a result which I do not expect while using and
and or
. Why is this happening during both these operations?
python logical-operators
1
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked againstitem
.
– Klaus D.
Feb 22 at 7:13
Change(sub1 and sub2) in item
tosub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
2
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18
add a comment |
I have a conceptual question about Python. This is the code
list1=['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
sub1 = "teacher"
sub2 = "sales"
ans=
for item in list1:
if (sub1 and sub2) in item:
ans.append(item)
Here, I expect the list to be empty as none of the items satisfy the condition if sub1 and sub2 in item:
But when I print the list I get the output#1 as this
>>> ans
['salesperson', 'sales manager'] # I expected an empty list here
Also, when I use or
instead of and
as given below
for item in list1:
if (sub1 or sub2) in item:
ans.append(item)
the output#2 I get is
>>> ans
['schoolteacher', 'mathematics teacher'] # I expected a list of words containing sub1 or sub2 as their substrings
I saw a similar looking solution here, but it does not exactly solve my problem. Both the times I get a result which I do not expect while using and
and or
. Why is this happening during both these operations?
python logical-operators
I have a conceptual question about Python. This is the code
list1=['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
sub1 = "teacher"
sub2 = "sales"
ans=
for item in list1:
if (sub1 and sub2) in item:
ans.append(item)
Here, I expect the list to be empty as none of the items satisfy the condition if sub1 and sub2 in item:
But when I print the list I get the output#1 as this
>>> ans
['salesperson', 'sales manager'] # I expected an empty list here
Also, when I use or
instead of and
as given below
for item in list1:
if (sub1 or sub2) in item:
ans.append(item)
the output#2 I get is
>>> ans
['schoolteacher', 'mathematics teacher'] # I expected a list of words containing sub1 or sub2 as their substrings
I saw a similar looking solution here, but it does not exactly solve my problem. Both the times I get a result which I do not expect while using and
and or
. Why is this happening during both these operations?
python logical-operators
python logical-operators
edited Feb 22 at 14:00
Peter Mortensen
13.8k1987113
13.8k1987113
asked Feb 22 at 7:11
SatyaSatya
364217
364217
1
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked againstitem
.
– Klaus D.
Feb 22 at 7:13
Change(sub1 and sub2) in item
tosub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
2
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18
add a comment |
1
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked againstitem
.
– Klaus D.
Feb 22 at 7:13
Change(sub1 and sub2) in item
tosub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
2
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18
1
1
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked against item
.– Klaus D.
Feb 22 at 7:13
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked against item
.– Klaus D.
Feb 22 at 7:13
Change
(sub1 and sub2) in item
to sub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
Change
(sub1 and sub2) in item
to sub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
2
2
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18
add a comment |
2 Answers
2
active
oldest
votes
("teacher" and "sales") in "salesmanager"
do not mean the same in Python and in English.
In English, it is synonynous to ("teacher" in "salesmanager") and ("sales" in "salesmanager")
(which Python would understand as you thought it should, and evaluate to False
).
Python on the other hand will first evaluate "teacher" and "sales"
, because it is in parentheses, and thus has higher priority. and
will return the first argument if falsy, otherwise the second argument. "teacher"
is not falsy, so "teacher" and "sales"
evaluates as "sales"
. Then, Python continues to evaluate "sales" in "salesmanager"
, and returns True
.
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggestany
andall
for this pattern.
– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions likeall
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).
– Amadan
Feb 25 at 4:56
add a comment |
The and
and or
operators don't do what you think they do. Try breaking up your expressions:
if sub1 in item or sub2 in item:
if sub1 in item and sub2 in item:
The and
operator evaluates its left-hand operand and, if the result is truthy, returns the right-hand operand, otherwise the left-hand operand.
The or
operator evaluates its left-hand operand and, if the result is falsy, returns the right-hand operand, otherwise the left-hand operand.
So, in your first expression evaluates as follows:
(sub1 and sub2) in item
("teacher" and "sales") in item
("sales") in item
which is not what you expected.
Similarly for your second expression:
(sub1 or sub2) in item
("teacher" or "sales") in item
("teacher") in item
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%2f54821841%2fwhy-are-and-or-operations-in-this-python-statement-behaving-unexpectedly%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
("teacher" and "sales") in "salesmanager"
do not mean the same in Python and in English.
In English, it is synonynous to ("teacher" in "salesmanager") and ("sales" in "salesmanager")
(which Python would understand as you thought it should, and evaluate to False
).
Python on the other hand will first evaluate "teacher" and "sales"
, because it is in parentheses, and thus has higher priority. and
will return the first argument if falsy, otherwise the second argument. "teacher"
is not falsy, so "teacher" and "sales"
evaluates as "sales"
. Then, Python continues to evaluate "sales" in "salesmanager"
, and returns True
.
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggestany
andall
for this pattern.
– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions likeall
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).
– Amadan
Feb 25 at 4:56
add a comment |
("teacher" and "sales") in "salesmanager"
do not mean the same in Python and in English.
In English, it is synonynous to ("teacher" in "salesmanager") and ("sales" in "salesmanager")
(which Python would understand as you thought it should, and evaluate to False
).
Python on the other hand will first evaluate "teacher" and "sales"
, because it is in parentheses, and thus has higher priority. and
will return the first argument if falsy, otherwise the second argument. "teacher"
is not falsy, so "teacher" and "sales"
evaluates as "sales"
. Then, Python continues to evaluate "sales" in "salesmanager"
, and returns True
.
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggestany
andall
for this pattern.
– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions likeall
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).
– Amadan
Feb 25 at 4:56
add a comment |
("teacher" and "sales") in "salesmanager"
do not mean the same in Python and in English.
In English, it is synonynous to ("teacher" in "salesmanager") and ("sales" in "salesmanager")
(which Python would understand as you thought it should, and evaluate to False
).
Python on the other hand will first evaluate "teacher" and "sales"
, because it is in parentheses, and thus has higher priority. and
will return the first argument if falsy, otherwise the second argument. "teacher"
is not falsy, so "teacher" and "sales"
evaluates as "sales"
. Then, Python continues to evaluate "sales" in "salesmanager"
, and returns True
.
("teacher" and "sales") in "salesmanager"
do not mean the same in Python and in English.
In English, it is synonynous to ("teacher" in "salesmanager") and ("sales" in "salesmanager")
(which Python would understand as you thought it should, and evaluate to False
).
Python on the other hand will first evaluate "teacher" and "sales"
, because it is in parentheses, and thus has higher priority. and
will return the first argument if falsy, otherwise the second argument. "teacher"
is not falsy, so "teacher" and "sales"
evaluates as "sales"
. Then, Python continues to evaluate "sales" in "salesmanager"
, and returns True
.
answered Feb 22 at 7:16
AmadanAmadan
133k13146197
133k13146197
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggestany
andall
for this pattern.
– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions likeall
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).
– Amadan
Feb 25 at 4:56
add a comment |
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggestany
andall
for this pattern.
– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions likeall
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).
– Amadan
Feb 25 at 4:56
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for
( X and Y )
– Carl Witthoft
Feb 22 at 14:00
So far as I know, all languages give parentheses first priority, not just Python. But you are correct in explaining what Python returns for
( X and Y )
– Carl Witthoft
Feb 22 at 14:00
You might want to suggest
any
and all
for this pattern.– Neil G
Feb 24 at 21:04
You might want to suggest
any
and all
for this pattern.– Neil G
Feb 24 at 21:04
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions like
all
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).– Amadan
Feb 25 at 4:56
@NeilG I thought about it, then figured there's a big disparity between the level where precedence needs to be explained, and the level where someone can understand generators and functions like
all
, and that it would just confuse the asker (i.e. a person who would be ready to benefit from that would not be looking for this question in the first place).– Amadan
Feb 25 at 4:56
add a comment |
The and
and or
operators don't do what you think they do. Try breaking up your expressions:
if sub1 in item or sub2 in item:
if sub1 in item and sub2 in item:
The and
operator evaluates its left-hand operand and, if the result is truthy, returns the right-hand operand, otherwise the left-hand operand.
The or
operator evaluates its left-hand operand and, if the result is falsy, returns the right-hand operand, otherwise the left-hand operand.
So, in your first expression evaluates as follows:
(sub1 and sub2) in item
("teacher" and "sales") in item
("sales") in item
which is not what you expected.
Similarly for your second expression:
(sub1 or sub2) in item
("teacher" or "sales") in item
("teacher") in item
add a comment |
The and
and or
operators don't do what you think they do. Try breaking up your expressions:
if sub1 in item or sub2 in item:
if sub1 in item and sub2 in item:
The and
operator evaluates its left-hand operand and, if the result is truthy, returns the right-hand operand, otherwise the left-hand operand.
The or
operator evaluates its left-hand operand and, if the result is falsy, returns the right-hand operand, otherwise the left-hand operand.
So, in your first expression evaluates as follows:
(sub1 and sub2) in item
("teacher" and "sales") in item
("sales") in item
which is not what you expected.
Similarly for your second expression:
(sub1 or sub2) in item
("teacher" or "sales") in item
("teacher") in item
add a comment |
The and
and or
operators don't do what you think they do. Try breaking up your expressions:
if sub1 in item or sub2 in item:
if sub1 in item and sub2 in item:
The and
operator evaluates its left-hand operand and, if the result is truthy, returns the right-hand operand, otherwise the left-hand operand.
The or
operator evaluates its left-hand operand and, if the result is falsy, returns the right-hand operand, otherwise the left-hand operand.
So, in your first expression evaluates as follows:
(sub1 and sub2) in item
("teacher" and "sales") in item
("sales") in item
which is not what you expected.
Similarly for your second expression:
(sub1 or sub2) in item
("teacher" or "sales") in item
("teacher") in item
The and
and or
operators don't do what you think they do. Try breaking up your expressions:
if sub1 in item or sub2 in item:
if sub1 in item and sub2 in item:
The and
operator evaluates its left-hand operand and, if the result is truthy, returns the right-hand operand, otherwise the left-hand operand.
The or
operator evaluates its left-hand operand and, if the result is falsy, returns the right-hand operand, otherwise the left-hand operand.
So, in your first expression evaluates as follows:
(sub1 and sub2) in item
("teacher" and "sales") in item
("sales") in item
which is not what you expected.
Similarly for your second expression:
(sub1 or sub2) in item
("teacher" or "sales") in item
("teacher") in item
answered Feb 22 at 7:14
RobᵩRobᵩ
117k13142223
117k13142223
add a comment |
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%2f54821841%2fwhy-are-and-or-operations-in-this-python-statement-behaving-unexpectedly%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
1
(sub1 and sub2) in item
what will be the result of the expression in the brackets? That will be checked againstitem
.– Klaus D.
Feb 22 at 7:13
Change
(sub1 and sub2) in item
tosub1 in item and sub2 in item
– Tom Karzes
Feb 22 at 7:14
2
Possible duplicate of can Python 'and' return None?
– DirtyBit
Feb 22 at 7:18