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
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)