When making decisions in the real world, we first evaluate the information and circumstances surrounding us, then arrive at a conclusion based on our judgment of the details we have and the situations we face. Similarly, when building applications, we have to implement various conditional statements that decide the course of action to be taken by a program.

Just like humans in the real world, a Python program needs to perform some decision making based on whether some statements meet a set of conditions or not. That is where conditional statements come in. In the subsequent paragraphs of this tutorial, we will be exploring the various conditional statements that can be used in Python.

What Are Conditional Statements in Python?

In any programming language, if a program needs to solve a problem, it has to offer different branches of solutions to the problem. As a programmer, you guide the execution of your code based on whether some set of conditions are satisfied or not.

Conditional statements are control structures that define how decisions can be made when a script reaches a point where it has to select different courses of action.

The IF Condition in Python

The if control statement runs a particular code only when certain conditions have been met. For example, we can build an intelligent code that prompts our buyer to shop only when the sum of the prices of the grocery items is less than or equal to $50.

The syntax for an if condition in Python is as follows:

if test expression:
 Statement(s)

Example:

# The amount of budget that I have for grocery shopping
myBudget = 50
# The cost of each grocery item
bread = 5
milk = 10
beef = 25
apples = 5


if (bread + milk + beef + apples) <= myBudget:
    print("Within budget, buy all the items.")

Output

Within budget, buy all the items.

The ELIF Condition in Python

The elif (“else if”) control statement tests a new condition in Python if the previous condition evaluated to false. In this way, we can build multiple chained conditions in our programs. The program executes the elif block only if the past conditions evaluated to false.

Syntax

if test expression:
 statement(s)
elif test expression:
 statement(s) 

Example

# The amount of budget that I have for grocery shopping
myBudget = 50
# The cost of each grocery item
bread = 5
milk = 10
beef = 35
apples = 5


if (bread + milk + beef + apples) <= myBudget:
    print("Within budget, buy all the items.")
elif (bread + milk + beef + apples) > myBudget
    print("You don't have enough budget, perhaps you should forgo Milk.")    

Output

You don't have enough bubdget, perhaps you should forgo Milk.

The ELSE Condition in Python

The else conditional statement performs a given condition if all the previous set of conditions were false. An else statement doesn’t check for a condition. Instead, it will always execute if all the previous conditions were false.

Syntax

if test expression:
 statement(s)
elif test expression:
 statement(s) 
else test expression:
 statement(s)

Example

# The cost of each grocery item
bread = 10
milk = 10
beef = 35
apples = 5
chicken = 25

if int(bread + milk + beef + apples + chicken <= 50):
    print("Within budget, buy all the items.")
elif int(bread + milk + beef + apples <= 55):
    print("Remove Chicken to meet your budget level.")
else:
    print("You have to remove some items from your cart to meet your budget level.")

When the above code is executed, we overstretch our budget. Hence get the following results:

You have to remove some items from your cart to meet your budget level.

Nested conditional statements in Python

A nested conditional statement is when one or more conditions are within another condition. It is used to check for extra conditions if the previous condition is fulfilled/true.

Syntax for nested condition statements

if test expression1:
 statement(s)
if test expession2:
 statement(s)
elif test expression1:
 statement(s) 
elif text expression2:
 statement(s)
else test expression1:
 statement(s)
else test expession2:
 statements(s)

Example:

 # The cost of each grocery item
bread = 5
milk = 10
beef = 20
apples = 5
chicken = 15

if int(bread + milk + beef + apples <= 55):
    print("Within budget. Buy the four items.")
    # Nested if-else condition
    if int(bread + milk + beef + apples + chicken > 60):
        print("We will have to do without the chicken.")
    else:
        int(bread + milk + beef + apples + chicken <= 60)
        print("Still within budget. You can add the chicken.")
else:
    print("Your budget is not enough")

When the above code is executed with, we get the following results:

Within budget. Buy the four items.
Still within budget. You can add the chicken. 

Note!

Python conditional statements support logical mathematical operations which can be used in various ways depending on the functionality of a program.