Making decisions in Python

Module 3 · Making decisions in Python · Lesson 3 of 7

Making use of answers

Here's the quietly powerful part: the answer to a comparison is just a value — a boolean. And anything that's a value can go into a variable. So you can ask a question once, store the True/False answer, and use it as many times as you like.

is_adult = age >= 18
age >= 18 (age = 20)evaluates to ↓True
is_adult
True

Python works out the right-hand side first — age >= 18 becomes True — then stores that boolean in is_adult, exactly like any other assignment. Naming the answer also makes your code read like English: is_adult, has_won, is_empty.

Run it yourselfruns in your browser · Python
main.py

Storing a boolean is nice, but the real point of an answer is to act on it — to run one piece of code when it's True and skip it otherwise. That's the if statement, and it's next.