+ 5
My simple doubt, can anyone explain?
print("a" or "z") # output a print("a" and "z") # output z print(1 or 9) # output 1 print(1 and 9) # output 9 https://code.sololearn.com/cfHxDH0oWD1L/?ref=app
3 ответов
+ 13
From the documentation for logical operators `and`, `or`
and
and operator returns the first Falsy value if there are any, else return the last value in the expression.
For example,
print(a and b)
if a is a Falsy value it returns a.
Similarly, if a is a Truthy value b is returned.
That's why ("a" and "z") returns z and 9 for 1 and 9
Here, Falsy value means value that evaluate to False are considered Falsy.
or
or returns the first Truthy value if there are any, else return the last value in the expression.
Here, Truthy value means values that evaluate to True are considered Truthy.
"a" or "z" since "a" is a Truthy value, it returns a and 1 for 1 or 9
+ 4
OR - return first truthy value otherwise other value.
AND - return first falsy value otherwise other value.
+ 2
Thanks, Simba