Python If...Else


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.


📌 Why We Use 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.


✅ Basic if Statement

Let’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 Statement

But 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.


✅ The 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.


🧠 A Note on Indentation

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.


🔀 Using if with Logical Operators

You can check multiple conditions using and, or, and not:

age = 25
citizen = True

if age >= 18 and citizen:
    print("You can vote.")

🧪 A Real-World Example

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.


✅ Quick Summary

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

Practice Questions

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.


Go Back Top