This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
How does the logical `and` operator work with integers? [duplicate]
(2 answers)
"4 and 5" is 5, while "4 or 5" is 4. Is there any reason? [duplicate]
(4 answers)
Closed 3 years ago.
What does 'a' and 'b' in Python mean and why does it equal 'b'? Why does it not equal 'a'?
>>> 'a' and 'b'
'b'
From the Pycharm docs:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
Because 'a' is not False 'b' is evaluated and returned.
Also nice to know. What is evaluated as True and what as False:
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.
Either 'a' or 'b' would be an acceptable answer to 'a' and 'b' (since both are truthy), but only False would be an acceptable answer to 'a' and False, just as only 0 would be an acceptable answer to 'a' and 0 (since the result of this evaluation must be false-y to be logically correct).
Having a short-circuiting boolean evaluation follow the right-hand path when the left-hand is true allows there to be a single rule that applies in all cases.
Related
This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
enter image description here
i'm now working on if statements
this is my code
a = [1,2,3]
print(4 or 5 in a)
and the outcome is 4 not False
this code returns what i have expected
a = [1,2,3]
print((4 or 5) in a)
I can't understand how or operator works
why the return is integer..? not True or False
If there are multiple conditions in an if statement, if the first statement is correct, it doesn't check the second statement. Since 4 is true, it doesn't check if 5 is in the variable a or not. Therefore, it returns the value 4 and not False
In this case you have expression written incorrectly and as a result it evaluates the way it does. What you should have written was:
>>> a = [2, 1, 4]
>>> 4 in a or 5 in a
Out[18]: True
In your case the expression evaluates only the condition 4 or 5 which evaluates to 4 as the first truthy value.
>>> 4 or 5 in a
Out[27]: 4
>>> 4 or 5
Out[28]: 4
In fact, variable a is completely skipped here and not evaluated at all. We can do the same but using variable b that has never been defined in this session and we get 4 instead of UnboundLocalError:
>>> 4 or 5 in b
Out[31]: 4
Similarly we can test this behaviour. When you compare two truthy values the result is always the first one the interpreter encounters.
>>> 'a' or 'b'
Out[29]: 'a'
>>> 'd' or 'a'
Out[30]: 'd'
I suggest reading docs on boolean operators in Python to get a better understanding of what's happening (for example, here). It can get confusing.
This question already has answers here:
Logical operators in Python [duplicate]
(4 answers)
Closed 2 years ago.
I though I understood Python the "and" operator. But after seeing
assert (None and None) is None
it was apparently that my understanding was not precise. Which was that None and None would be the same as bool(None) and bool(None).
Does anybody have definition of the "and" operator and can explain the logic.
According to the official documentation:
help('and')
[...] The expression "x and y" first evaluates x; if x is false,
its value is returned; otherwise, y is evaluated and the resulting
value is returned. [...]
and returns the first value if it is "falsey"*. Otherwise it returns the second one.
For example:
3 and 6 -> 6
0 and 7 -> 0
[] and 'abc' -> []
* a thing is falsey if bool(thing) is False
This question already has answers here:
Logical operators in Python [duplicate]
(4 answers)
Closed 4 years ago.
How the logical operators i.e;(or, and) works on the strings or numbers?
Example:
print(2 or 3)--> o/p: 2
print('two' or 'three')--> o/p: 'two'
I want to know how internally it works and reason behind the output's
or will "choose" the first thing if it is not False, else the second one:
print(2 or 3) # non-0 numbers are True
print('two' or 'three') # non empty strings are True
print(0 or 3) # 0 is False
print('' or 'three') # empty string is False
Output:
2
two
3
three
Certain "things" are considered False.
See and,or,not and Logical comparisons:
Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below.
By default, an object is considered true unless its class defines
either a bool() method that returns False or a len() method
that returns zero, when called with the object. [1] Here are most of
the built-in objects considered false:
constants defined to be false: None and False.
zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
empty sequences and collections: '', (), [], {}, set(), range(0)
This question already has answers here:
What is the motivation for the "or" operator to not return a bool?
(6 answers)
Closed 5 years ago.
I've this code:
url='http://mybeautifulurl.com'
('h' or 'f') in url[0:4] #This is True
('f' or 'h') in url[0:4] #This is False
I'm just trying to understand why the 'or' operator seems to evaluate just the first condition.
This or returns the first element if it evaluates to true (as a bool) and the second one otherwise... it's is usually applied to set default values.
This way, 'h' or 'f' is simply 'h' and 'f' or 'h' is simply 'f'.
You can achieve what you want with something like:
any(x in url[:4] for x in ['h', 'f'])
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
Why does the following evaluates to False in Python?
6==(5 or 6)
False
'b'==('a' or 'b')
False
The first expression evaluates (5 or 6) first, which evaluates to 5 because 5 is truthy. 5 is NOT equal to 6 so it returns False.
The second expression evaluates ('a' or 'b') first, which evaluates to 'a' for the same reason as above. 'a' is NOT equal to 'b' so it returns False.
A good example to explain this would be to try to put a falsey value as the first part of the or expression like 6 == ([ ] or 6) or 6 == (None or 6). Both of these will return true because the or statement will evaluate to the truthy value (in each case it is 6).
There are a couple of ways to create the statement I think you want. The first would be to use in like 6 in (6,5). The second way would be to expand your boolean expression to read like this (6 == 5) or (6 == 6). Both of these will return True.
The condition within parentheses is evaluated first. So
6==(5 or 6)
evaluates to
6==(5) # since 5 != 0 (True), the second operand is not evaluated
which is False. Same goes for the second one. ('a' != None)
Try:
>>>6 == (6 or 5)
True
What's going on? Try:
>>5 or 6
5
I'm not totally sure why "5 or 6" evaluates to 5, but I would just try writing that expression differently.
But there is a caveat:
>> 5 == (0 or 5)
True
Because 0 is not truth-worthy, hence the bracket result in 5.