Python If-Else Statements
Introduction
In Python, conditional statements are used to make decisions in a program. They allow a program to execute different blocks of code depending on whether a condition is True or False.
For example, a program can check whether a number is positive or negative, whether a student has passed an examination, or whether a person is eligible to vote.
Python provides the following conditional statements:
ifstatementif-elsestatementif-elif-elsestatement- Nested
ifstatement - Conditional expression (ternary operator)
1. Python if Statement
The if statement executes a block of code only when the given condition is True.
Syntax
if condition:
# Code executes when condition is True
Example
age = 20
if age >= 18:
print("You are eligible to vote")
Output:
You are eligible to vote
Explanation:
- The variable
agecontains20. - Python checks whether
age >= 18. - The condition is True.
- Therefore, the statement inside the
ifblock executes.
Example: Checking a Positive Number
number = 10
if number > 0:
print("Positive number")
Output:
Positive number
2. Python if-else Statement
The if-else statement executes one block when the condition is True and another block when it is False.
Syntax
if condition:
# Executes when condition is True
else:
# Executes when condition is False
Example: Even or Odd Number
number = 7
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Odd number
Explanation:
- The modulus operator
%calculates the remainder. - If the remainder after division by 2 is zero, the number is even.
- Otherwise, the number is odd.
Example: Pass or Fail
marks = 35
if marks >= 40:
print("Pass")
else:
print("Fail")
Output:
Fail
3. Python if-elif-else Statement
The if-elif-else statement is used when we need to check multiple conditions.
Python checks the conditions from top to bottom. The first condition that evaluates to True has its block executed. If none is True, the else block executes.
Syntax
if condition1:
# Code for condition1
elif condition2:
# Code for condition2
elif condition3:
# Code for condition3
else:
# Code when no condition is True
Example: Student Grade
marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 80:
print("Grade B")
elif marks >= 70:
print("Grade C")
elif marks >= 40:
print("Grade D")
else:
print("Fail")
Output:
Grade B
Explanation:
- The first condition (
marks >= 90) is False. - The second condition (
marks >= 80) is True. - Therefore, Python prints
Grade Band skips the remaining branches.
4. Nested if Statement
A nested if statement means placing one if statement inside another if statement.
It is useful when a decision depends on another condition.
Syntax
if condition1:
if condition2:
# Code executes when both conditions are True
Example
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive")
else:
print("You need a driving license")
else:
print("You are not eligible to drive")
Output:
You can drive
5. Conditional Expression (Ternary Operator)
Python supports a short form of if-else, called a conditional expression or ternary operator.
It allows us to write a simple conditional assignment in one line.
Syntax
value_if_true if condition else value_if_false
Example
age = 20
result = "Adult" if age >= 18 else "Minor"
print(result)
Output:
Adult
6. Importance of Indentation in Conditional Statements
Indentation is important in Python because it defines the block of code belonging to a conditional statement.
Example:
age = 20
if age >= 18:
print("Eligible")
print("You can vote")
print("Program completed")
Output:
Eligible
You can vote
Program completed
Both indented statements belong to the if block. The final print statement is outside the block.
Incorrect indentation can cause an IndentationError.
7. Difference Between if, if-else, and if-elif-else
| Statement | Purpose |
|---|---|
if | Executes code when one condition is True |
if-else | Selects between two alternative blocks |
if-elif-else | Selects from multiple possible conditions |
Nested if | Checks a condition inside another condition |
8. Practice Questions (MCQs)
Q1. Which statement is used for decision-making in Python?
A. for
B. if
C. print
D. import
Correct Answer: B. if
Q2. Which keyword is used to execute an alternative block when an if condition is False?
A. elif
B. while
C. else
D. break
Correct Answer: C. else
Q3. What is the output of the following code?
x = 10
if x > 5:
print("A")
else:
print("B")
A. A
B. B
C. A B
D. No output
Correct Answer: A. A
Q4. Which keyword is used to check multiple conditions?
A. elif
B. continue
C. pass
D. def
Correct Answer: A. elif
Q5. What is a nested if statement?
A. An if statement inside another if statement
B. An if statement inside a loop only
C. A function inside a class
D. A loop inside a function
Correct Answer: A. An if statement inside another if statement
Q6. What is the output of the following code?
number = 8
if number % 2 == 0:
print("Even")
else:
print("Odd")
A. Odd
B. Even
C. 8
D. Error
Correct Answer: B. Even
Q7. What does indentation define in Python conditional statements?
A. Variable type
B. Code blocks
C. Data type
D. Function name
Correct Answer: B. Code blocks