Python Conditional Basics

October 7, 2025

Every program needs to make decisions — whether it’s checking if a user is old enough to vote, verifying login details, or deciding if a stock should be bought or sold.
In Python, we make such decisions using conditional statements.

In this blog, you’ll learn:

  • What conditional statements are
  • The if, elif, and else keywords
  • Comparison and logical operators
  • Nested and shorthand conditionals
  • Real-world examples

🧠 1. What Are Conditional Statements?

Conditional statements allow your program to execute certain blocks of code only if specific conditions are met.

For example:

“If it’s raining, take an umbrella. Else, go outside.”

In Python, we use:

  • if – to check a condition
  • elif – to check another condition if the previous one fails
  • else – to handle all other cases

🧩 2. The if Statement

The simplest conditional in Python.

age = 18

if age >= 18:
    print("You are eligible to vote.")

Output:

You are eligible to vote.

Here’s what happens:

  • Python checks if age >= 18.
  • If True, it executes the print() statement.
  • If False, it skips that block.

Note: Python uses indentation (spaces) to define code blocks — not braces {} like other languages.


🔁 3. The if...else Statement

Sometimes you want one thing to happen if the condition is true, and something else if it’s false.

num = 10

if num % 2 == 0:
    print("Even number")
else:
    print("Odd number")

Output:

Even number

Here, the program checks if num is divisible by 2.
If not, it executes the else block.


🔄 4. The if...elif...else Chain

Use elif (short for else if) when you need to check multiple conditions.

marks = 75

if marks >= 90:
    print("Grade: A")
elif marks >= 75:
    print("Grade: B")
elif marks >= 60:
    print("Grade: C")
else:
    print("Grade: D")

Output:

Grade: B

Here:

  • Python checks each condition in order.
  • Once a condition is True, it runs that block and skips the rest.

⚙️ 5. Comparison Operators in Python

Comparison operators are used to compare values in conditions.

OperatorDescriptionExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 5True
<Less than3 < 8True
>=Greater than or equal5 >= 5True
<=Less than or equal4 <= 2False

🧩 6. Logical Operators

You can combine multiple conditions using logical operators:

OperatorMeaningExampleResult
andTrue if both conditions are truex > 0 and x < 10True if x between 0 and 10
orTrue if at least one condition is truex > 10 or x == 5True if any condition is true
notReverses the conditionnot(x > 10)True if x <= 10

Example:

age = 25
country = "India"

if age > 18 and country == "India":
    print("Eligible for Indian voter ID.")

Output:

Eligible for Indian voter ID.


🔁 7. Nested if Statements

You can put one if inside another — this is called a nested condition.

age = 20
citizen = "Indian"

if age >= 18:
    if citizen == "Indian":
        print("Eligible to vote in India.")
    else:
        print("Not an Indian citizen.")
else:
    print("Not old enough to vote.")

Output:

Eligible to vote in India.


⚡ 8. Shorthand if Statement (One-Liner)

Python allows compact single-line conditionals.

x = 10
print("Positive") if x > 0 else print("Non-positive")

Output:

Positive

This is useful for quick, simple checks.


🎯 9. Real-World Example — Checking Login Access

username = "admin"
password = "1234"

if username == "admin" and password == "1234":
    print("Access granted.")
else:
    print("Access denied.")

Output:

Access granted.


🧮 10. Example — Number Classification

Let’s build a program that checks whether a number is positive, negative, or zero.

num = float(input("Enter a number: "))

if num > 0:
    print("Positive number")
elif num < 0:
    print("Negative number")
else:
    print("Zero")

Output (if user enters -5):

Negative number


🧠 11. Example — Leap Year Checker

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("Leap year")
else:
    print("Not a leap year")

Output:

Leap year


🔍 12. Common Mistakes to Avoid

Missing indentation:

if True:
print("Hello")  # ❌ Wrong

Correct:

if True:
    print("Hello")


🧾 Summary

ConceptDescriptionExample
ifExecutes if condition is trueif x > 0:
if...elseExecutes one of two blocksif x > 0: ... else: ...
elifChecks multiple conditionsif...elif...else
and, or, notCombine multiple conditionsif x > 0 and y < 5:
Nested ifOne if inside anotherif x > 0: if y > 0:
Shorthand ifSingle-line conditional"Yes" if True else "No"

💡 Key Takeaways

✅ Conditional statements help programs make decisions
✅ Python uses indentation to define code blocks
✅ Use and, or, not for combining conditions
✅ Always test your logic with different inputs


🧱 Example Challenge for You

Try writing a Python program that:

  • Takes a user’s marks as input
  • Prints whether the user passed, failed, or got distinction

© Copyright 2025 Fessorpro

Terms of Service / Privacy Policy