Loading
0x20Lesson 3 of 10

Make decisions with if

Choose which statements run with conditions and indentation.

12 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Compare values and write an if/else branch.

An if statement runs a block only when its condition is true. else handles the other case, while elif checks another condition. A colon begins each block, and indentation marks which statements belong to it.

main.py
1temperature = 18
2if temperature > 20:
3    print("warm")
4else:
5    print("cool")
Output
cool

Comparison operators include ==, !=, <, <=, > and >=. Use and, or and not to combine conditions. Consistent four-space indentation is the standard Python style.

Key takeaways

  • Compare values and write an if/else branch.

  • Read the output to check what your program did.

  • Keep indentation consistent in Python blocks.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Python

Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.

Exercise 1

Positive, negative, or zero

+25 XP

Read an integer and print positive, negative, or zero according to its value.

  • Positive value
  • Negative value
  • Zero
main.py
Loading editor…

Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…