Python is one of the most beginner-friendly programming languages, and one reason for its popularity is how intuitive its control flow statements are. Among these, the if, if-else, and if-elif-else statements are essential for making decisions in your programs.
In this article, we will break down these concepts in a simple and engaging way, complete with examples, so even if you’re new to programming, you will feel confident using them.
What are if, if-else, and if-elif-else ?
These are Python’s tools for decision-making. They let your program choose between actions based on conditions you set. Think of them as decision checkpoints:
- if :Executes a block of code only if a condition is true.
- if – else: Chooses between two actions—one for true and another for false.
- if – elif – else : Chooses between multiple actions by checking several conditions one by one.
Let’s learn one by one in detail with coding examples:
The ‘ if ‘ Statement
if statement evaluates a condition (a test, like comparing numbers). If the condition is true, the code inside the if block runs. Otherwise, it skips the block.
Example : Checking Age
age = 20
if age >= 18:
print("You are eligible to vote!")
Code Explanation:
- The condition age > = 18 checks if the value of age is 18 or more.
- If it’s true, the message “You are eligible to vote!” is printed.
- If the age were less than 18, nothing would happen.
Imagine you’re checking if you can enter a movie theater:
- If your age is 18 or older, you’re allowed in.
- Otherwise, you’ll just stand outside—but no one tells you explicitly why.
The ‘if-else’ Statement
What if you want your program to respond when a condition is not true? This is where if – else comes in. it gives your program an alternate option if condition is not true.
Example : Checking Age
age = 16
if age >= 18:
print("You are eligible to vote!")
else:
print("Sorry, you're too young to vote.")
Explanation
- If the condition age > = 18 is true, the program prints the first message.
- Otherwise, it runs the else block, showing the second message.
Let’s go back to the movie theater example:
- If you’re 18 or older, you enter the theater.
- If you’re younger, the staff tells you, “Sorry, come back when you’re older.”
The if – elif – else Statement
Sometimes, you have multiple conditions to check. For this, Python provides the if – elif – else statement. Think of it as a series of checkpoints.
Example: Exam Grades
score = 85
if score >= 90:
print("You got Grade A")
elif score >= 80:
print("You got Grade B")
elif score >= 70:
print("You got Grade C")
else:
print("You need to work harder.")
Code Explanation
- The program checks each condition in order.
- If score>=90 is true, it prints “You got Grade A” and skips the rest.
- If not, it moves to the next condition score> = 80 and so on.
- If none of the conditions are true, the else block runs.
Think of this as choosing transportation based on how far you need to travel:
- If the distance is less than 1 km, you walk.
- If it’s between 1 and 10 km, you bike.
- If it’s more than 10 km, you drive.
- If there’s no way to travel, you stay home.
Write a program that checks if a number is positive, negative, or zero.
number = int(input("Enter a Number"))
if number > 0:
print(f"Number {number} is positive")
elif number < 0:
print(f"Number {number} is negative")
else:
print(f"Number {number} is zero")
Conclusion
if, if – else, and if – elif – else statements are the building blocks of decision-making in your programs. With just a few lines of code, you can make your program respond dynamically to different situations.
Whether you’re checking voting eligibility, assigning grades, or building a chatbot, these statements will be your go-to tools. Start experimenting with them in your Python projects today.
Would you like more examples or help with any specific Python topic? Let me know.