Python: if then else

By Xah Lee. Date: . Last updated: .

if

x = 1
if x == 1:
    print("yes")

if else

y = 2

if y == 1:
    print("yes")
else:
    print("no")

if else chain

Note it's elif:

z = 2
if z < 0:
    print("neg")
elif z == 0:
    print("zero")
elif z == 1:
    print("one")
else:
    print("other")

Python: Operators