Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are 'and/or' operations in this Python statement behaving unexpectedly?

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?

like image 635
Sssssuppp Avatar asked Feb 22 '19 07:02

Sssssuppp


People also ask

Why does my Python operator return a true or false value?

If the operands involved in an or operation are objects instead of Boolean expressions, then the Python or operator returns a true or false object, not the values True or False as you could expect. The truth value of this object is determined according to the rules you’ve seen before.

What happens if all objects are false in Python?

If all objects ( a and b in this case) are false objects, then the Python or operator returns None, which is the last operand. This works because the or operator returns one of its operands depending on their truth value.

What is the use of operators in Python?

Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic and logical computations. The value the operator operates on is known as Operand. In Python, Logical operators are used on conditional statements (either True or False).

What are operands in Python?

Operands are the subexpressions or objects involved in an expression (Boolean or not) and connected by an operator. Boolean or logical operators are AND (logical AND or conjunction), OR (logical OR or disjunction), and NOT (logical NOT or negation). The keywords and, or, and not are the Python operators for these operations.


1 Answers

("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.

like image 187
Amadan Avatar answered Nov 15 '22 06:11

Amadan