Conditional Expressions

 avatar
c0dezer019
python
a month ago
685 B
7
Indexable
Never
a = 10
b = 20
c = False
d = None

# Use is when not comparing literals.
e = a is not None # True
f = a is not False # True
g = type(a) is str # False

# Use comparison operators ('==', '!=', '<', etc.) when comparing with literals
h = a < b # True
i = b > a # True
j = b >= a # True
k = a == b # False
l = a != b # True

# But what if we want it to be something else if it evaluates False? use the 'or' operator!
m = a > b or d # None
n = a < b or c # True
o = a > b or "Hello" # Hello

# You can also use 'and', or a simplified chain op
p = a < b and a == 10 # True
q = b > a == 10 # True, b is greater than a and a is 10. This syntax would not work for example 'p' because of order.
Leave a Comment