Skip to content

Beginner · Text and decisions · topic 7 of 50 · 20 min

Booleans and comparisons

Explanation

A boolean is a value that is either True or False. Comparisons like age >= 18 or name == "Ravi" give a boolean. You can combine them with and, or and not, and every value in Python also counts as either true-like or false-like.

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.

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than2 < 1False
>greater than10 > 3True
<=less than or equal to18 <= 18True
>=greater than or equal to17 >= 18False
python
age = 20
print(age >= 18)
print(age == 21)
print(age != 21)
print(10 == 10.0)
Shows True, False, True, True. An int and a float with the same value are equal.
Think of it like this: A comparison is like the guard at a cinema hall checking tickets. For each person he asks one question, "Is this ticket for this show?", and the answer is only yes or no. True is yes, False is no.

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.

python
marks = 82
attendance = 70
print(marks >= 75 and attendance >= 75)
print(marks >= 75 or attendance >= 75)
print(not attendance >= 75)
Shows False (attendance is too low), True (marks are enough) and True.

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.

python
age = 35
print(18 <= age <= 60)
print("chai" in "masala chai")
print("Tea" in "masala tea")
Shows True, True, False. in is case-sensitive, so Tea with a capital T is not found.

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.

python
print(bool(0), bool(""), bool(None))
print(bool(5), bool("0"), bool(" "))
Shows False False False, then True True True. "0" and " " are not empty, so they are truthy.
Careful: One = stores a value. Two == compare values. Writing if marks = 100: is a SyntaxError. To check equality always write marks == 100.
Tip: Do not write x == True. If x is already a boolean, just use x itself, and use not x instead of x == False.

Watch the lesson

Booleans and comparisons · 1/6

True or False

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.

python
percent = 81
family_income = 180000
eligible = percent >= 75 and family_income <= 250000
print("Eligible for scholarship:", eligible)
Shows Eligible for scholarship: True.

Practice (0/3 done)

Task 1 · Code · Python

Write a function can_vote(age) that returns True if age is 18 or more, and False otherwise.

Task 2 · Code · Python

Write a function is_working_age(age) that returns True when age is from 18 to 60, both included.

Task 3 · Code · Python

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

Question 1

1.What does 10 == 10.0 give?

Question 2

2.Which of these values is falsy?

Question 3

3.What does not (5 > 3) give?

Question 4

4.Which line checks whether marks is equal to 100?

Question 5

5.What does "tea" in "Masala Tea" give?

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.

← Previous: f-strings and string methods