Reputation: 53
>>> 'a' in 'aeiou' or 'steve'
True
>>> 'S' in 'Sam' and 'Steve'
'Steve'
>>> 'a' in 'aeiou' and 'steve'
'steve'
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'
I was working on a class for some students and was surprised by the last three outputs. I was expecting a boolean. Can anyone shed some light on this?
Upvotes: 4
Views: 105
Reputation: 2251
In Python
or: from left to right if value is boolean if value is true return this or if value is all false return last item. example: False or 3 return 3, 4 or 3 return 3, '' or False or - 1 return -1.
and: evaluate from left to right and return last value != False or first value if value is False.
example:
1 and 2 and 3 and 4 and 5 return 5: because 1,2,3,4,5 all is not False then return last value is 5.
-1 and 3 and 4 return 4; -1 is not False; 3,4 is not False too then return the last. Remember that negative value is not False :D. 0 and 3 return 0. 0 is False then return False
>>> 'a' in 'aeiou' or 'steve' True
'a' in 'aeiou' is not False ; 'steve' is not False In or operator return first value is not False then this is 'a' in 'aeiou' is True.
>>> 'S' in 'Sam' and 'Steve' 'Steve'
'S' in 'Sam' is True; 'Steve' is not False In and operator return last value is not False this is 'Steve' then resule is 'Steve'
>>> 'a' in 'aeiou' and 'steve' 'steve'
'a' in 'aeiou' is True, 'steve' is not False then return last value is not False is 'steve'
>>> 's' in 'aeiou' or 'AEIOU' 'AEIOU'
's' in 'aeiou' is False ; 'AEIOU' is not False then return first value is not False is 'AEIOU'
More in that link: http://www.diveintopython.net/power_of_introspection/and_or.html
Upvotes: 0
Reputation: 10213
Boolean Operations
x or y | if x is false, then y, else x
Demo
>>> 0 or 1
1
>>> 0 or 0
0
x and y | if x is false, then x, else y
Demo
>>> 0 and 1
0
>>> 1 and 0
0
>>> 1 and 1
1
>>>
Note: These only evaluate their second argument if needed for their outcome.
This will return True
when condition is satisfy otherwise False
.
Demo:
>>> "a" in "abc"
True
>>> "a" in "xyz"
False
>>>
Now about our statement:
1. As 'a' in 'aeiou'
return True
value and we are performing or
operation, so this will return True
because First(Left) value of expression is True
.
Demo:
>>> 'a' in 'aeiou'
True
>>> 'a' in 'aeiou' or 'steve'
True
>>>
2. As 'S' in 'Sam'
return True
and we are performing and
operation, So this will return second value from the expression.
Demo:
>>> 'S' in 'Sam'
True
>>> 'S' in 'Sam' and 'Steve'
'Steve'
>>>
3. Same as second statement.
4. As 's' in 'aeiou'
return False
and we are performing or
operation, So this will return second value from the expression.
Demo:
>>> 's' in 'aeiou'
False
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'
>>>
Upvotes: 5
Reputation: 239443
Your expressions are evaluated like this
>>> ('a' in 'aeiou') or ('steve')
True
>>> ('S' in 'Sam') and ('Steve')
'Steve'
>>> ('a' in 'aeiou') and ('steve')
'steve'
>>> ('s' in 'aeiou') or ('AEIOU')
'AEIOU'
The or
and and
operators, evaluate the expressions and return the result of the evaluation.
So, or
will first evaluate the LHSE (Left Hand Side Expression) and if it is Truthy (see below), it will return the evaluated result immediately (short-circuiting). Only if it is Falsy (see below), it will evaluate and return the the result of RHSE.
Similarly, and
will evaluate the LHSE and if it is Falsy it will return the value immediately otherwise it will return the result of evaluation of the RHSE.
Truthy and Falsy
Quoting the official documentation.
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:
False
,None
, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
So, in the first case, ('a' in 'aeiou')
is True
, so it returns True
immediately.
In the second case, ('S' in 'Sam')
is False
, so it returns the result of evaluating ('Steve')
which is Steve
only.
In the third case, ('a' in 'aeiou')
is True
, and so it returns the result of evaluating ('steve')
which is steve
only.
In the last case, ('s' in 'aeiou')
is False
, so it returns the result of evaluating ('AEIOU')
which is AEIOU
only.
Apart from this, you can get to know if your expressions are actually Truthy or Falsy with the bool
function, like this
>>> bool('a' in 'aeiou' or 'steve')
True
>>> bool('S' in 'Sam' and 'Steve')
True
>>> bool('a' in 'aeiou' and 'steve')
True
>>> bool('s' in 'aeiou' or 'AEIOU')
True
Upvotes: 4
Reputation: 623
I believe this page explains it http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/boolean.html. You're running into interesting BODMAS rules and what happens when you apply logical operations to a bool and a string (strings evaluate to true - https://docs.python.org/2/library/stdtypes.html).
Both the operation and the order matter:
Upvotes: 0
Reputation: 881153
Because:
'a' in somestring or 'steve'
is interpreted as:
('a' in somestring) or 'steve'
which will either give you True
or 'steve'
, depending on whether a
is in somestring
. It is for 'aeiou'
but not for 'xyzzy'
:
>>> 'a' in 'aeiou' or 'steve'
True
>>> 'a' in 'xyzzy' or 'steve'
'steve'
If you want to check if the letter is in either word, use:
('a' in 'aeiou') or ('a' in 'steve')
Upvotes: 1