-
Hajipur, Bihar, 844101
Imagine you're writing a program that tells someone whether they can vote or not. The computer needs to decide based on the user's age — that's where Python’s if...else
statements come in.
In simple terms, if...else
lets your program make choices.
if...else
In real life, we make decisions like:
If it’s raining, take an umbrella.
If you're hungry, eat food. Else, skip lunch.
Python lets your code do the same thing.
if
StatementLet’s start with the simplest version:
age = 20
if age >= 18:
print("You are eligible to vote.")
Here’s what’s happening:
The code checks the condition: age >= 18
.
If it's True
, it runs the indented block of code.
if...else
StatementBut what if the person is not eligible? We can handle that using else
:
age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote yet.")
Now your program reacts to both cases — eligible and not eligible.
elif
Keyword (Else If)Sometimes you have more than two choices. For that, use elif
.
score = 85
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: F")
This checks each condition in order. The moment it finds one that’s True
, it runs that block and skips the rest.
Python doesn’t use curly braces {}
to mark blocks of code.
Instead, it relies on indentation (usually 4 spaces):
if condition:
# this must be indented
do_something()
If you don’t indent properly, Python will give you an error.
if
with Logical OperatorsYou can check multiple conditions using and
, or
, and not
:
age = 25
citizen = True
if age >= 18 and citizen:
print("You can vote.")
password = input("Enter password: ")
if password == "admin123":
print("Access granted.")
else:
print("Access denied.")
This is a basic login check — something you’ll find in many real applications.
Keyword | What it does |
---|---|
if |
Checks a condition |
else |
Runs if all if and elif are False |
elif |
Additional condition checks |
and , or , not |
Combine multiple conditions |
Q1. Write a Python program to check if a number is positive, negative, or zero.
Q2. Write a Python program to take the user's age and print whether they are a child, teenager, or adult.
Q3. Write a Python program to ask the user for a number and check if it is even or odd.
Q4. Write a Python program to get a number from the user and check if it is divisible by both 3 and 5.
Q5. Write a Python program to print the largest of three numbers entered by the user.
Q6. Write a Python program to check if a given year is a leap year.
Q7. Write a Python program to take a score and print the grade using if...elif...else
statements.
Q8. Write a Python program to ask the user to enter a password and check if it’s correct (you can define the correct password in the code).
Q9. Write a Python program to check if a character entered by the user is a vowel or consonant.
Q10. Write a Python program to get two numbers from the user and print which one is bigger, or if they are equal.
Q1: What is the purpose of an if statement?
Q2: What happens if the condition in if is False and there’s no else?
Q3: Which keyword checks multiple conditions after if?
Q4: What does if age > 18: check?
Q5: What happens if indentation is missing?
Q6: Which of these is a logical operator?
Q7: What will if not True: result in?
Q8: How many elif statements can be used?
Q9: What is the correct symbol for "equal to" comparison?
Q10: What keyword runs when all conditions fail?