Python if else elif Statement


If statements are a form of flow control, statements are run only conditionally. Like if good then do that, if bad then do that.

  • if-elif-else statement is used to create a conditional flow code.
  • The order is if..elif..else
  • The keyword ‘elif’ means the ‘else if’ statement
  • The statements else and elif are optional
  • You can create multiple elif statements
  • You can have only one else block in an if statement
  • The words if, else, and elif are reserved keywords
  • A switch-case statements doesn't exist in Python
  • You can create nested if-else blocks in Python

Related course: Complete Python Programming Course & Exercises

Python if syntax

The basic usage of the if statement is as follows.

if expressions: 
    statements

The expression can be a simple boolean value or a variable, or it can be a comparative expression or a logical expression (e.g., a > b and a != 5.

If the expression is True, then execute the "statement block"; If the value of the expression is False, then skip the "statement block" and continue to execute the programs statements.

Example

age = 18
if age >= 18:
    print("Can drive car")

Python if else Syntax

The basic usage of the if...else statement is as follows.

if expressions: 
    statements
else: 
    statements

When using the if...else statement, the expression can be a simple boolean value or variable, or it can be a comparative expression or a logical expression.

The program executes the statement block after if only if the expression is True, otherwise it runs the statement block after else.

When using the else statement, the else must not be used alone, it must be used with the reserved word if.

Example code:

num = 25
if num > 10:
    print("result is True")
else:
    print("result is False")

python if statement

Example code with two variables:

age = int(input('Please enter age'))
sex =input('Please enter sex')
if age >= 19 and (sex == 'male' or sex == 'female'):
    print ('Time to go to work')
else:
    print("Go to school.")

Example with a list: the if statement is run for every car in the list:

cars = ['audi','bmw','toyota','subaru','volkswagen']

for car in cars:    
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.lower())

Python if-elif-else Example

The basic usage of the if...elif...else statement is as follows.

if expression 1: 
    statements-block-1 
elif expression 2: 
    statements-block-2 
elif expression 3: 
    statements-block-3
else: 
    statement-block-n

When using the if...elif...else statement, the expression can be a boolean or variable, or it can be a comparative or logical expression, and if the expression is True, the statement is executed.

If the expression is False, the statement is skipped for the next elif, and the statement in else is executed only if all expressions are False

Example of if-elif-else:

age = 20
if age < 5:
    print("free entry")
elif age < 10:
    print("you need to pay 5$")
else:
    print("you need to pay 15$")

Large example code with two variables:

height = float(input('Please enter height'))
strong = float(input('Please enter weight'))
print('height is %s, weight is %s'%(height, strong))
BMI = strong/height**2
print('Body Mass Index Index is %s'%BMI)
if BMI < 18.5:
    print('too light')
elif BMI >= 18.5 and BMI <= 25:
    print('normal')
elif BMI >= 25 and BMI <= 28:
    print('overweight')
elif BMI >= 28 and BMI <= 32:
    print('fat')
elif BMI >= 32:
    print('severely obese')
else :
    print('overly obese')

Python if-else in One Line

If you have an if statement like this

x = 5
if x > 0:
    positive = True
else:
    positive = False

Then you can the Python ternary operation instead

>>> x = 5
>>> positive = True if x > 0 else False

Nested if-else Conditions

If statements inside if statements are permitted. This is called nested if statements. There is no limit to how many nested if statements you can do.

if Expression 1:
    if Expression 2:
        Statements block 1
    else:
        Statements Block 2
else:
    if Expression 3:
        Statements block 3