If…Elif..else Statement in Python

First, understand what the elif means. It is Python’s way of saying, “If you cannot buy Huawei or LG, try Tecno. It is a way of giving another outlet.

If you write an if…elif…else statement, the computer will run the if condition first. If that one is False, it will move down to the elif statement and run it. Then if that one also comes false, it will run the else statement.

The if…elif…else statement in Python looks like this:

a = 245
b = 30

if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

A is bigger than B, so the first condition isn’t true. Also, the elif condition isn’t true, so we go to the else condition and display “a is bigger than B.”

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials