Python If...Else

Make decisions with if, elif and else.

✍️ 3 exercises + code challenge in this lesson

Python uses if, elif ("else if") and else for conditions. Each condition ends with a colon and its block is indented.

  • Combine conditions with and, or, not.
  • Short form: result = "Pass" if marks >= 50 else "Fail"
  • Use pass for an empty block.

Key Takeaways: Python If...Else

Mastering Python If...Else helps you write clean and reliable Python code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

Python If...Else Code Example

Python
marks = 78

if marks >= 85:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 50:
    grade = "C"
else:
    grade = "F"

print("Grade:", grade)
print("Result:", "Pass" if marks >= 50 else "Fail")

age, has_ticket = 20, True
if age >= 18 and has_ticket:
    print("Welcome to the event!")
Free sign-in required · Google login available

Python If...Else Practice Exercises

Check what you learned in “Python If...Else”.

/ 3 solved
Fill in the blank

Python writes "else if" as one keyword.

if x > 10:
    print("big")
 x > 5:
    print("medium")
✓ Correct! Not quite. Try again.
Multiple choice

Which keyword creates an empty block?

✓ Correct! Not quite. Try again.
Code challenge

Print "Even" if n is even, otherwise "Odd".

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services