>>> '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?
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'
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With