Conditional Statements in Python – Python Tutorial
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.
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 numberslist_of_numbers = [2,4,6,9,5]
# for loop to iterate through the list and check each elements of the listfor 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")
Output:
Also Read: For Loop in Python
Best-suited Python for data science courses for you
Learn Python for data science with these high-rated online courses
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 numberslist_of_numbers = [2,4,6,9,5] # for loop to iterate through the list and check each element of the listfor 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")
Output:
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 stringstring ="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 blockelse: print ("All the above conditions are false")
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 numberslist_of_numbers = [4,5,9,17,21] # for loop to iterate through the list and check each element of the listfor 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")
Output:
Conclusion:
In this article, we explored the following concepts:
- What is the if statement?
- How does it work?
- What is the if-else statement?
- What is the if-elif-else ladder?
- 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.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio
Comments
(2)
S
5 months ago
Report Abuse
Reply to Sekar Sri
S
5 months ago
Report Abuse
Reply to Sekar Sri