Make decisions with if
Choose which statements run with conditions and indentation.
- 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.
1temperature = 18
2if temperature > 20:
3 print("warm")
4else:
5 print("cool")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.
Positive, negative, or zero
Read an integer and print positive, negative, or zero according to its value.
- Positive value
- Negative value
- Zero
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…