Python if else statements can be expressed on two ways
- Simple if-else statement
- if-elif-else statement with one or more occurrences of
elif
.
if else example
x = 1 y = 1 if x == y: print "equal" else: print "not equal"
equal
Env: Python 2.7.18
if-elif-else example
x = 1 if x < 0: print "lt zero" elif x == 0: print "zero" else: print "gt zero"
gt zero
Env: Python 2.7.18