📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

If-Else Statements in Python: Complete Guide

Quick answer: Learn Python if, elif and else statements including comparison operators, nested conditions, and the ternary conditional expression, with examples.

Basic If Statement

age = 20

if age >= 18:
    print("You are an adult")

If-Else

marks = 45

if marks >= 40:
    print("Pass")
else:
    print("Fail")

Elif for Multiple Conditions

marks = 82

if marks >= 90:
    grade = "A+"
elif marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
else:
    grade = "C"

print(grade)   # A

Python checks conditions from top to bottom and stops at the first one that is true, so order matters when conditions could overlap.

Comparison Operators Used in Conditions

OperatorMeaning
==Equal to
!=Not equal to
>, <Greater than, less than
>=, <=Greater than or equal to, less than or equal to

Combining Conditions with and, or, not

age = 25
has_id = True

if age >= 18 and has_id:
    print("Entry allowed")

if age < 18 or not has_id:
    print("Entry denied")

Nested If Statements

income = 60000
has_loan = True

if income > 50000:
    if has_loan:
        print("Eligible with existing loan review")
    else:
        print("Eligible")
else:
    print("Not eligible")

Ternary Conditional Expression

Python supports a compact one-line form for simple if-else assignments.

age = 16
status = "Adult" if age >= 18 else "Minor"
print(status)   # Minor

A Common Mistake

# Using = instead of == is a SyntaxError in a condition, catching this early
if age = 18:      # wrong: assignment, not comparison
    pass

if age == 18:     # correct
    pass

Common Interview Questions

What is the difference between elif and multiple separate if statements?

With elif, Python stops checking once one condition is true. With separate if statements, every single one is evaluated independently, even if an earlier one was already true.

What does the ternary expression evaluate to if the condition is false?

It evaluates to whatever follows the else keyword in the expression.

FAQ

Frequently Asked Questions

What is the difference between elif and a new if statement?

With elif, Python stops checking further conditions once one is true. With separate if statements, every one is evaluated independently regardless of earlier results.

What is a ternary conditional expression in Python?

A compact one-line form of if-else used for simple assignments, written as value_if_true if condition else value_if_false.

Can you combine multiple conditions in a single if statement?

Yes, using the logical operators and, or and not to combine comparisons.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles