Control Flow with Conditional Statements (if, elif, else) in Python
Control Flow with Conditional Statements (if, elif, else) in Python
Hello Everyone! Today we are going to explore Control Flow in Python. By default, a computer executes code sequentially line by line. However, we often want our program to make decisions—running certain lines of code only if specific conditions are met.
To achieve this, Python uses if, elif, and else statements.
The Concept of Indentation
In languages like C or Java, curly braces {} are used to define a block of code. Python does things differently. It uses Indentation (whitespace or tabs at the beginning of a line) to determine which statements belong to a specific block.
if False:
print("Line1") # This line is inside the block
print("Line2") # This line is inside the block
print("Line3") # This line is outside the block (Runs regardless of the conditional)
If the condition is False, the code blocks directly pushed over by spaces will be skipped completely.
1. The if-else Binary Decision
An if-else structure acts like a fork in the road. If the condition evaluated matches True, the if block executes. Otherwise, the else block runs.
age = 8
if age >= 18:
print("Yes You Can Vote.")
else:
print("No You Can Not Vote.")
2. Multi-Condition Checking with elif
What if you have more than two possibilities? For instance, a number isn’t just positive or negative—it could also be zero. To test multiple distinct expressions sequentially, use elif (short for else if).
a = int(input("Enter A Number: "))
if a > 0:
print("It's a +ve Number.")
elif a < 0:
print("It's a -ve Number.")
else:
print("It's a 0")
Python evaluates these from top to bottom. As soon as one condition is met, its code block executes, and Python exits the entire conditional structure.
3. Combining Conditions vs. Cleaner Syntax
Let’s look at a classic problem: checking whether a single character is a vowel. There are multiple ways to implement this check, ranging from verbose to clean and optimal.
Method A: The Verbose Chain (Not Recommended)
a = "x"
if a == "a":
print("yes")
elif a == "e":
print("yes")
# ... continuing down for i, o, u. This takes too many lines!
Method B: Using Logical Operators (or)
You can group conditions together on a single line using the logical or operator.
a = "a"
if a == "a" or a == "e" or a == "i" or a == "o" or a == "u":
print("yes")
else:
print("No")
Method C: The Pythonic Way (in Operator)
The most elegant solution in Python uses the membership operator in to scan a collection of matching target characters instantly:
char = 'v'
if char in "aeiouAEIOU":
print("Yes")
else:
print("Not")
4. Range Checking with Logical and
When you need an assignment to satisfy multiple criteria simultaneously, combine them with the and operator.
a = 25
# Checks if 'a' falls strictly within the boundary of 11 to 49
if a < 50 and a > 10:
print("yes")
else:
print("no")
Structure Summary Trace
Let’s look at how the execution jumps based on the conditions by adding checkpoints at the start and end of the block.
print("Start")
if 4 > 5: # This condition evaluates to False
print("I am in the if statement")
else: # Since 'if' failed, execution jumps straight here
print("I am in the else statement")
print("end")
Here is the console output:
Start
I am in the else statement
end