← Python from Scratch
Lesson 18 of 78 · 10 XP
The boolean type
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
bool is a type with two values: True and False. Comparisons produce them:
5 > 3 # True
5 == 3 # False — the equality check uses two equals signs
5 != 3 # True — "not equal"
Logical operators combine conditions:
age = 20
age >= 18 and age < 65 # True — both conditions hold
age < 18 or age > 65 # False — neither holds
not True # False
Python can chain comparisons, just like in maths: 18 <= age < 65.
Any value can be turned into a bool. These count as false: 0, 0.0, "" (an empty string), [], {}, None. Everything else is true:
bool("") # False
bool("text") # True
bool(0) # False
Task. Write a function in_range(x, a, b) that returns True if x lies in the range from a to b inclusive, and False otherwise.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.