Python: if then else

By Xah Lee. Date: . Last updated: .

if

# python 3

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

if else

# python 3

y = 2

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

if else chain

Note it's elif:

# python 3

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

Python

Regex

Text Processing

Web

Misc