Conditional Statements in Python – Python Tutorial

Conditional Statements in Python – Python Tutorial

3 mins read59.5K Views Comment
clickHere
Updated on Jan 18, 2024 17:49 IST

As the name suggests, a conditional statement is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by the program. In this article, we will explore three conditional statements in Python: if statement, if-else statements, if-elif-else ladder.

2022_02_Conditional-Statements-in-Python.jpg

Like every other programming language, Python also has some predefined conditional statements. A conditional statement as the name suggests itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by the program.

Must Check: Free Python Online Course and Certifications

Must Read: What is Python?

Python has 3 key Conditional Statements that you should know:

Let’s discuss each of them in detail.

if Statement

The if statement is a conditional statement in Python used to determine whether a block of code will be executed. If the program finds the condition defined in the if statement true, it will execute the code block inside the if statement.

Syntax:

if condition:
        # execute code block

To better understand this, take a look at the below example.

Must Check: Top Python Online Courses and Certifications

Python Example: Python program to check if a number is odd or even.


 
# list of numbers
list_of_numbers = [2,4,6,9,5]
# for loop to iterate through the list and check each elements of the list
for i in list_of_numbers:
# check if no. is odd
if i % 2 != 0:
# if condition is True print "odd"
print (i, "is an odd number")
# check if no. is even
if i % 2 == 0:
# if condition is false print "even"
print (i, "is an even number")
Copy code

Output:

2023_05_for-statement-in-python.jpg

Also Read: For Loop in Python

if-else Statement

As discussed above, the if statement executes the code block when the condition is true. Similarly, the else statement works in conjuncture with the if statement to execute a code block when the defined if condition is false.

 

Syntax:

if condition:
        # execute code if condition is true
else:
        # execute code if condition if False

Now, let’s expand on this concept in the previous example.

Python Example: Python program to check if a number is odd or even.


 
# list of numbers
list_of_numbers = [2,4,6,9,5]
# for loop to iterate through the list and check each element of the list
for i in list_of_numbers:
# check if no. is odd
if i%2 != 0:
# if condition is True print "odd"
print(i, "is an odd number")
# if the no. is not odd then the no. is even therfore print "even"
else:
# if condition is True print "even"
print(i, "is an even number")
Copy code

Output:

2023_05_for-statement-in-python-1.jpg
 

if-elif-else ladder

The elif statement is used to check for multiple conditions and execute the code block within if any of the conditions are evaluated to be true.

The elif statement is similar to the else statement in that it is optional. Still, unlike the else statement, multiple elif statements can be in a block of code following an if statement.

if condition1:
    # execute this statement
elif condition2:
    # execute this statement
    ..
    ..
else:
    # if non of the above conditions evaluate to True execute this statement

Take a look at the below example for context.

Python Example: Python program to check if a string matches the condition.


 
# take a sample string
string ="ShikshaOnline"
# check if value in the string variable
# matches "Shik"
if string == "Shik":
print ("The first condition is true")
# check if value in the string variable
# matches "Shiksha"
elif string == "Shiksha":
print("The second condition is true")
# check if value in the string variable
# matches "Online"
elif string == "Online":
print("The third condition is true")
# check if value in the string variable
# matches "ShikshaOnline"
elif string=="ShikshaOnline":
print("The fourth condition is true")
# if none of the above
# conditions evaluate to true
# execute the code inside the else block
else:
print ("All the above conditions are false")
Copy code

Output:

The fourth condition is true

Nested if Statements:

A nested if statement is considered as if within another if statement(s).  These are generally used to check for multiple conditions.

Syntax:

if condition1:
        if condition2:
                # execute code if both condition1and condition2 are True

Look at the example below for a better understanding of nested if statements.

Python Example: Python program to check if a number is odd and divisible by 3.


 
# list of numbers
list_of_numbers = [4,5,9,17,21]
# for loop to iterate through the list and check each element of the list
for i in list_of_numbers:
# condition1: check if no. is odd
# if yes execute the code block inside the first if statement
if i%2!=0:
# condition2: check if no. is also divisible by 3.
if i%3==0:
# if condition2 is true
# execute the below code block
print (i,"is an odd number & divisible by 3")
# if codition2 if False
# execute the below code block
else:
print (i, "is an odd number but not divisible by 3")
# if condition1 is False
# execute the below code block
else:
print(i,"is an even number")
Copy code

Output:

2023_05_nested-uf-statement-2.jpg

Conclusion:

In this article, we explored the following concepts:

  1. What is the if statement?
  2. How does it work?
  3. What is the if-else statement?
  4. What is the if-elif-else ladder?
  5. How do nested if statements work?

 

FAQs

What is a Conditional Statement in Python?

A conditional statement as the name suggests itself, is used to handle conditions in your program. These statements guide the program while making decisions based on the conditions encountered by the program.

What are the benefits of using conditional statements in Python?

Conditional statements allow you to make decisions in your code, which can make your code more efficient and easier to read. For example, if you want to print a message only if a certain condition is met, you can use a conditional statement. This can help you to avoid writing long and complicated blocks of code.

What are some common mistakes to avoid when using conditional statements in Python?

Here are some common mistakes to avoid when using conditional statements in Python: Forgetting to indent the code inside the if statement and else statement. Forgetting to add a colon (:) after the condition in the if statement. Using the wrong operator to compare two values. Forgetting to add an else statement.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments