Python: Conditional. 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")

If Expression

result = "yes" if 3 > 0 else "no"
print(result)
# yes
result = (1 if 3 > 0 else 0) + 1
print(result)
# 2

Python, Operators