Python 3 — Making Decisions with Conditionals

🐍 Python 3 🟢 Lesson 5 📅 July 2026

Conditionals allow your code to make decisions. Without them, your code is just a simple recipe. By checking conditions, your program can choose to run specific code blocks while completely skipping others.

1 Comparison Operators

Before making a decision, you must compare values. Python returns a Boolean value (True or False) after evaluations:

OperatorMeaningExample
==Equal to5 == 5 (True)
!=Not equal to5 != 3 (True)
> / <Greater/Less than10 > 12 (False)
>= / <=Greater/Less or equal10 >= 10 (True)
2 The if, elif, else Structure

Python checks the conditions sequentially. The moment it finds a condition that evaluates to True, it runs that block of code and skips the rest:

Python 3 — Conditionals ▶ Run Code
temperature = 28

if temperature > 30:
    print("It is extremely hot outside!")
elif temperature >= 20:
    print("The weather is warm and nice.")
else:
    print("It is cold outside.")

Notice the colons (:) at the end of each condition, and the indentation before the print functions. In Python, **indentation is mandatory** and defines which code belongs to which block.

3 Logical Operators

You can check multiple conditions at the same time using logical operators:

  • and: Returns True only if **both** conditions are true.
  • or: Returns True if **at least one** condition is true.
  • not: Inverts the condition (turns true to false, and vice-versa).
Python 3 — Logical Conditions ▶ Run Code
has_licence = True
age = 19

if age >= 18 and has_licence:
    print("You are cleared to drive!")
else:
    print("You cannot drive.")
⚠️ IndentationError Warning:

In Python, failing to indent or mixing tabs with spaces causes an IndentationError. Always use 4 spaces for your indents. Our Compiler handles this formatting for you automatically when you press tab!

4 Coding Challenge

Write a grading program: declare a variable called 'score'. If the score is 90 or more, print "Grade A". If it is 80 or more, print "Grade B". If 70 or more, print "Grade C". Otherwise, print "Grade F".