Advanced

Yes/no values combine

A comparison gives a yes/no value, and yes/no values can be combined into bigger questions. That is how a program states a condition it actually wants, instead of stacking several small checks inside each other.

What you will see

False

About 2 min of reading

Try It

Build one condition from named parts.

1

Store two comparisons as yes/no values.

age = 20 school = age < 13 teen = 13 <= age < 20

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Print them. They are values, so printing works.

print(school) print(teen)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
False True
3

Combine with or to ask one question.

print(school or teen)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
True
4

Flip one with not and watch the answer change.

print(not teen)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
False
not applies to the value right after it, so not teen is one flip. To flip a whole condition, put it in brackets first, or store it in a name.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Each side is worked out to a yes/no value, the connector combines them, and the combination decides a branch.
1 age >= 13 gives yes
↓
2 age < 20 gives yes
↓
3 and combines both
↓
4 The condition is yes
Why this works
Conditions are values

An if statement does not contain a question, it contains a value that is yes or no. That means you can store the condition in a name, print it, and test it in isolation. Splitting a big condition into named parts is what makes it readable.

The three words

and, or and not are the whole toolkit for conditions. Anything more complicated is built from them. When a condition becomes hard to read, the fix is usually to name each part rather than to add new symbols.

Examples

and and or

age = 20
print(age >= 13 and age < 20)
print(age < 13 or age >= 20)

and needs both sides yes. or needs one side yes. not flips one side. Those three words build every condition you will ever need.

Chained comparison

age = 20
print(13 <= age < 20)

This reads the way maths does, and it checks age once against both bounds. Writing age >= 13 and age < 20 means the same thing, and the chained form is shorter to read.

Short circuit

print(True or 1 / 0 == 1)

The left side is already yes, so the right side never runs and no error happens. and and or stop as soon as the answer is known, which is what makes a guard before a risky call work.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Changing a value kind on purpose
Previous Lesson
Changing a value kind on purpose
Next Lesson
A name can hold nothing
A name can hold nothing →