Introduction
In Python, decision-making is achieved using conditional statements. These statements allow us to control the flow of a program by executing certain blocks of code based on conditions. The key decision-making statements in Python are:
if
if-else
if-elif-else
These statements use relational operators (>, <, ==, etc.) to evaluate conditions.
Why Use If-Else?
By default, Python executes code line by line in a sequential manner. However, sometimes we need to skip or alter the execution flow based on specific conditions. To achieve this, we use if-else statements.
How If-Else Works
An if statement evaluates a condition. If the condition is True, the indented block of code runs. If it is False, the block is skipped, and if an else block is provided, it executes instead.
Example: Checking Voting Eligibility
Let's check if a person is eligible to vote (age 18 or above):
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Syntax Breakdown
Colon (:) after if and else indicates the start of an indented block.
Indentation (spaces or a tab) defines which statements belong to if or else.
The else block executes only if the if condition is False.
Using elif for Multiple Conditions
Sometimes, we need to check multiple conditions. This is where elif (short for else if) comes in.
For example, let's categorize a person based on age:
age = int(input("Enter your age: "))
if age < 16:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
elif age > 50:
print("You are old.")
else:
print("You are an adult.")
Explanation:
If the first condition (age < 16) is True, the program executes that block and skips the rest.
If False, the next condition (age < 18) is checked.
If no conditions are True, the else block runs.
More Practical Examples
- Checking Even or Odd Number
A number is even if it is divisible by 2 (i.e., remainder 0 when divided by 2), otherwise, it is odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
- Checking Positive, Negative, or Zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Best Practices for Writing If-Else Statements
Always use indentation (4 spaces per level) to maintain readability.
- 1. Use elif instead of multiple if statements when checking multiple conditions.
- 3. Keep conditions simple and readable
- 5. Use parentheses if conditions are complex (e.g., (x > 0) and (y < 100)).
- 7. Use meaningful variable names.
Conclusion
- The if statement evaluates a condition.
- The else statement provides an alternative execution path.
- The elif statement allows multiple conditions to be checked sequentially.
By mastering if-else, you can write dynamic and interactive Python programs. Start experimenting with different conditions and enhance your coding skills!
Top comments (0)