Skip to content
dreamcode
dreamcode
Map
If & Else
Lesson 9 of 77
+15 XP on finish
PYTHON BASICSChapter 2 · Conditionals and logic

Branching paths

An if statement runs a block of code only if a condition is True. An optional else block runs instead if the condition is False. Indent the code inside each block using spaces.

Worked example

How it reads

  • if sky == "clear": checks if the variable matches "clear"
  • The indented lines run only if their corresponding condition is met
  • else: captures any case where the condition was False
Cloud tip: Python uses indentation (4 spaces) to group blocks of code. Always make sure your indentation matches.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Change only the value of temp so the program takes the other branch and prints Warm sky.

Press Run to check your work.