Python: if then else

By Xah Lee. Date: . Last updated: .

If Statement

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

If Expression

xx = "yes" if (3 == 3) else "no"

print(xx)
# yes

If Else

y = 2

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

Else If Chain

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

Python, Operators