Python If Statement - Class XI Notes
What is an if Statement?
An if statement in Python allows the program to make decisions.
It checks if a condition is True and then runs a block of code; otherwise, it skips that block.
Think of it like a traffic signal:
- If the light is green, you go.
- If it's red, you stop.
Your action depends on the condition.
Syntax
if condition:
# code to run if condition is True
Example 1: Simple if
age = 18
if age >= 18:
print('You are eligible to vote.')
Output:
You are eligible to vote.
Example 2: if...else
age = 16
if age >= 18:
print('You are eligible to vote.')
else:
print('You are not eligible to vote.')
Output:
You are not eligible to vote.
Page 1
Python If Statement - Class XI Notes
Example 3: if...elif...else
marks = 75
if marks >= 90:
print('Grade A')
elif marks >= 75:
print('Grade B')
elif marks >= 50:
print('Grade C')
else:
print('Fail')
Output:
Grade B
Example 4: Nested if
age = 20
citizen = True
if age >= 18:
if citizen:
print('You can vote.')
else:
print('You must be a citizen to vote.')
Output:
You can vote.
Example 5: if with multiple conditions (and/or)
age = 25
has_license = True
if age >= 18 and has_license:
Page 2
Python If Statement - Class XI Notes
print('You can drive.')
Output:
You can drive.
Tips for Students
1. Indentation is mandatory in Python - usually 4 spaces.
2. Conditions are checked from top to bottom.
3. Use comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not) in conditions.
4. Always think: 'What happens if this is False?'
Practice Questions
1. Check if a number is positive.
2. Print 'Pass' if marks are above 40, otherwise 'Fail'.
3. Write a program to check if a number is odd or even.
4. Check if a person is eligible for a senior citizen discount (age >= 60).
5. Determine if a year is a leap year.
Page 3