Python / Python Control Structures
Using If-Else and Elif Statements
In this tutorial, we will explore If-Else and Elif statements in Python, which are key building blocks for any program. They allow us to control the flow of our program based on c…
Section overview
5 resourcesExplores conditionals, loops, and control flow in Python.
Using If-Else and Elif Statements in Python
1. Introduction
In this tutorial, we aim to understand the importance of If-Else and Elif statements in Python. These are fundamental elements in Python programming that allow us to control the flow of our program based on certain conditions.
By the end of this tutorial, you will learn:
- What are If, Else and Elif statements
- How to use them effectively
- Write basic Python programs using these statements
Prerequisites: Basic understanding of Python programming language.
2. Step-by-Step Guide
If Statement
In Python, the if statement is used for conditional execution of code. If the condition is true, the block of code under it is executed.
if condition:
# code to be executed if condition is True
Else Statement
The else statement is used to execute a block of code if the condition in the if statement is false.
if condition:
# code to be executed if condition is True
else:
# code to be executed if condition is False
Elif Statement
The elif statement allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True.
if condition1:
# code to be executed if condition1 is True
elif condition2:
# code to be executed if condition2 is True
else:
# code to be executed if both conditions are False
Best Practices and Tips:
- Keep your conditions simple and readable.
- Use parentheses () to group conditions, when necessary.
- Use elif statements effectively to avoid unnecessary checks.
3. Code Examples
Example 1:
temperature = 20
if temperature < 0:
print("It's freezing!")
else:
print("It’s not freezing.")
# Output: It’s not freezing.
In this example, the condition temperature < 0 is False, hence the code under else is executed.
Example 2:
temperature = 40
if temperature < 0:
print("It's freezing!")
elif 0 <= temperature <= 30:
print("The weather is normal.")
else:
print("It's hot!")
# Output: It's hot!
In this example, the condition temperature < 0 is False, hence the elif condition is checked. The elif condition is also False, hence the code under else is executed.
4. Summary
We have learned about the use of If-Else and Elif statements in Python. These statements help us to control the flow of our program based on certain conditions.
Next, you should learn about loops (like for and while) in Python. Here are some resources for that:
- Python For Loops
- Python While Loops
5. Practice Exercises
Exercise 1: Write a program that prints "Good Morning!" if the time is less than 12, "Good Afternoon!" if the time is between 12 and 17, and "Good Evening!" if the time is more than 17.
Solution:
time = 15
if time < 12:
print("Good Morning!")
elif 12 <= time < 17:
print("Good Afternoon!")
else:
print("Good Evening!")
# Output: Good Afternoon!
Exercise 2: Write a program that prints "Leap Year" if the given year is a leap year, "Not a Leap Year" otherwise. (Hint: A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400).
Solution:
year = 2000
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
# Output: Leap Year
For further practice, try to solve problems from the "Conditionals and Loops" section on websites like HackerRank or LeetCode.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article