Module 7 of 15 · Step 1 of 11

Conditions

A condition lets a program choose a path. Python checks a true-or-false expression and runs only the matching indented block.

By the end of this lesson, you can:
  • Write if, else, and elif
  • Indent a branch
  • Compare values at boundaries
  • Combine requirements with and
Conditions lesson progress0%

Course progress: 0% · 0 of 11 steps completed in this module

Learn · Run code only when a condition is true
Concept 1 of 4

Run code only when a condition is true

An if line ends with a colon. The indented instructions below it belong to that branch. When the condition is False, Python skips that block.

Pattern
if condition:
    instruction
Code
age = 18
if age >= 18:
    print("Adult")
Output
Adult

Why it works

  1. age stores 18.
  2. >= includes 18, so the condition is True.
  3. The indented print runs once.

Common mistake

Not this
if age >= 18
print("Adult")
Try this
if age >= 18:
    print("Adult")

The colon starts the block and indentation marks which instruction belongs to it.