New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials Python Python If/Else

Python If/Else

Python
Quick Summary

Decision-making is the core of programming. Python's if/elif/else statements let your code make choices — like checking if a user is old enough to vote, or grading students based on marks. This lesson covers conditional logic, comparison operators, and the elegant one-liner ternary syntax.

Python uses if, elif, and else for decisions. Indentation (spaces) is mandatory!

Important

  • Python uses elif (not "else if")
  • Indentation (4 spaces) defines code blocks — no curly braces!
  • Use == for comparison, = for assignment
Example — PYTHON
# Simple condition
age = 18
if age >= 18:
    print("You can vote! ✅")
else:
    print("Too young ❌")

# Multiple conditions
marks = 85
if marks >= 90:
    print("Grade: A+")
elif marks >= 75:
    print("Grade: A")
elif marks >= 60:
    print("Grade: B")
else:
    print("Grade: C")

# One-liner
status = "Adult" if age >= 18 else "Minor"
print(status)
Result
You can vote! ✅
Grade: A
Adult

Key Takeaways

  • Python uses if, elif, else for branching logic
  • Indentation (4 spaces) defines code blocks — no curly braces needed
  • == compares values, = assigns values — don't confuse them
  • One-liner: x = "yes" if condition else "no" (ternary operator)
  • You can chain multiple elif blocks for complex decisions

Real-World Usage

Every app uses conditions: login systems check if passwords match, e-commerce sites apply discounts based on cart value, games decide if a player wins or loses, and ATMs check if you have sufficient balance. Mastering if/else is mastering logic itself.

Interview Questions — Python If/Else

5 questions commonly asked in interviews

Q1 What are conditions in Python? Beginner

Conditions allow a program to make decisions based on True or False expressions.

Q2 What is if statement in Python? Beginner

if statement executes code only if the condition is true.

Q3 What is elif? Beginner

elif allows checking multiple conditions sequentially.

Q4 What is else? Beginner

else executes when all previous conditions are false.

Q5 What is a ternary operator? Intermediate

A short form of if-else expression in one line.

Frequently Asked Questions

Common doubts about Python If/Else

Yes, using and, or, not operators.

Yes, indentation defines code blocks in Python.

Yes, one if can be inside another.

It checks equality of values.

== checks value equality, is checks object identity.

Test Your Knowledge

5 questions · Earn 50 XP

0 / 5
Q1 Which keyword is used for condition?
Q2 Which keyword checks multiple conditions?
Q3 Else runs when?
Q4 Logical AND is?
Q5 Which compares value?

More on Python If/Else

Cheatsheet, tips, resources & what to learn next

Quick Cheatsheet

If
if x > 5:
    print("Yes")
If Else
if x > 5:
    print("Yes")
else:
    print("No")
Elif
if x > 5:
    pass
elif x == 5:
    pass
Ternary
print("Yes") if x > 5 else print("No")
Logical
if x > 5 and y < 10:

Pro Tips

1

Use proper indentation.

2

Avoid deep nested conditions.

3

Use elif for multiple conditions.

4

Use logical operators for complex checks.

5

Keep conditions readable.

Up Next

Python Loops Python Functions Python Lists Python Strings Python Dictionaries