If, elif and else in python

Conditional expressions, involving keywords such as if, elif, and else, provide Python programs with the ability to perform different actions depending on a Boolean condition: True or False.

Conditional Expression

n = 10
print("Greater than 5") if n > 5 else print("smaller than 5")

# output: Greater than 5

if, elif, and else

n = 5
if n > 2:
    print("n is greater than 2")
elif n < 2:
    print("n is smaller than 2")
else:
    print("n = 2")
# output: n is greater than 2

Previous||Next Chapter

Leave a Reply