All lessons

Making Decisions

8 min · Python

Run different code depending on whether something is true.

Python · Making Decisions0/5 cleared

Enemy 1 of 5

Every program you've written so far runs the same lines in the same order every single time. Real programs need to react differently depending on the situation — show a "Game Over" message only if the player's health hits zero, apply a discount only if the cart total is high enough. That's what decisions (also called conditionals) are for.

Programs make choices by checking whether something is true or false — a value Python calls a boolean. You compare values with comparison operators like > (greater than), < (less than), >= (greater than or equal), <= (less than or equal), and == (equal to). Notice == uses two equals signs — a single = is for assignment (storing a value), while == is for comparison (asking a question). Confusing the two is one of the most common beginner bugs, though Python will usually catch it for you in an if with a clear error.

An if statement runs the indented block beneath it only when its condition evaluates to true; if the condition is false, that block is skipped entirely and the program moves on. Run this:

You
Memory-Leak Rat

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: You are an adult