Beginner · Text and decisions · topic 7 of 50 · 20 min
Booleans and comparisons
Explanation
True and False
A boolean (type bool) has only two possible values: True and False. They are written with a capital first letter. Booleans are the answers to yes-or-no questions, and programs use them to decide what to do.
Comparison operators
A comparison operator compares two values and gives back True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | equal to | 5 == 5 | True |
| != | not equal to | 5 != 3 | True |
| < | less than | 2 < 1 | False |
| > | greater than | 10 > 3 | True |
| <= | less than or equal to | 18 <= 18 | True |
| >= | greater than or equal to | 17 >= 18 | False |
age = 20
print(age >= 18)
print(age == 21)
print(age != 21)
print(10 == 10.0)and, or, not
and gives True only when both sides are True. or gives True when at least one side is True. not flips a boolean: not True is False.
marks = 82
attendance = 70
print(marks >= 75 and attendance >= 75)
print(marks >= 75 or attendance >= 75)
print(not attendance >= 75)Chained comparisons and in
Python lets you chain comparisons like in maths: 18 <= age <= 60 means "age is at least 18 and at most 60". The word in checks whether one string appears inside another.
age = 35
print(18 <= age <= 60)
print("chai" in "masala chai")
print("Tea" in "masala tea")Comparing strings
Strings are compared letter by letter, like words in a dictionary. Capital letters come before small letters, so "Zebra" < "apple" is True. == on strings needs an exact match, including capitals and spaces.
Truthy and falsy values
Every value can act as True or False. A falsy value is one that counts as False: 0, 0.0, the empty string "", None and False itself. Everything else is truthy, meaning it counts as True. The function bool() shows you which.
print(bool(0), bool(""), bool(None))
print(bool(5), bool("0"), bool(" "))Watch the lesson
Booleans and comparisons · 1/6
In this lesson you will learn about booleans, the values True and False, and how to compare things in Python.
Prefer a video by a teacher? Find free videos on “python booleans comparison operators and or not tutorial” on YouTube ↗
In real life: Checking scholarship eligibility
A state scholarship is given to students who scored at least 75% and whose family income is at most Rs 2,50,000 a year. Meera scored 81% and her family earns Rs 1,80,000. Two comparisons joined with and give the answer.
percent = 81
family_income = 180000
eligible = percent >= 75 and family_income <= 250000
print("Eligible for scholarship:", eligible)Practice (0/3 done)
Write a function can_vote(age) that returns True if age is 18 or more, and False otherwise.
Write a function is_working_age(age) that returns True when age is from 18 to 60, both included.
An online shop gives free delivery when the order is Rs 499 or more, OR when the customer is a member. Write free_delivery(amount, is_member) that returns True or False.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.