Boolean in Python

Theory

The bool Data Type in Python

Comparison operators return a special boolean type: bool. A boolean value can be either True or False.

Boolean values

  • True — logical truth.
  • False — logical falsehood.

Example:

x = 5
y = 3

result = x > y
print(result)        # True
print(type(result))  # <class 'bool'>

Conversion between bool and int

  • True converts to 1.
  • False converts to 0.
  • 0 converts to False.
  • Any nonzero integer converts to True.
print(int(True))   # 1
print(int(False))  # 0

print(bool(0))     # False
print(bool(42))    # True

Conversion between bool and str

  • An empty string "" converts to False.
  • Any non-empty string converts to True.
print(bool(""))       # False
print(bool("Hello"))  # True
print(bool("0"))      # True  # non-empty string is always True

When booleans are used

  • In if, elif, while conditions.
  • Returned by comparison and logical operators (<, ==, !=, and, or, not).
  • As flags to represent yes/no, on/off, success/failure states in programs.

Quick practice

  1. Check if the square of a number is greater than 50, and print the boolean result.
  2. Convert True and False to integers and print them.
  3. Ask the user for a string and print whether it is empty (False) or not (True).
Show sample solutions
# 1) Square greater than 50?
x = int(input())
print(x * x > 50)

# 2) Convert booleans to integers
print(int(True), int(False))

# 3) Check empty string
s = input("Enter text: ")
print(bool(s))

Key takeaways: bool is a basic type with only two values: True and False. It plays a central role in controlling program flow, and Python offers straightforward conversions between booleans and other types.

Problems

FAQ

Find quick answers to common questions about our lessons, pricing, scheduling, and how Exact Science can help your child excel.
Where do you hold your classes?
We hold our classes online or on-site on Saturdays at our branch in Pimlico Academy, London.
You can find our timetable here.
What do you need to start learning online?
For lessons you only need a computer or phone with a microphone, camera and Internet access. Wherever you are - in London, Nottingham, New York or Bali - online lessons will be at hand.
When can I take the trial lesson?
You can get acquainted with the school at any time convenient for you. To do this, just leave a request and sign up for a lesson.
What should I expect from the trial lesson?
The trial lesson is a 30-minute online session designed to get a sense of how your child approaches mathematical thinking and problem solving. (In practice, it often runs a bit longer if the student is engaged!)

We typically explore a range of fun and challenging problems drawn from competitions. We adapt the difficulty based on how the student responds, aiming to make it both accessible and stimulating.

After the session, we’ll have a quick conversation with the parent to share observations and suggest a personalised path forward.
I can't attend class, what should I do?
It is OK, it happens! Students have the opportunity to cancel a lesson up to 8 hours before the scheduled time without loss of payment. So you can reschedule it for a convenient time, and the teacher will have the opportunity to
I don't have much free time, will I have time to study?
Learning can take place at your own pace. We will select a convenient schedule and at any time we will help you change the schedule, take a break or adjust the program.
How long is one lesson?
All classes last 1 hour.

Meet our team

Our teachers will tell you how to prepare for exams, help you cope with difficult tasks and win the Olympiad

They will tell you about the pitfalls of exams and the most common mistakes, and explain how to avoid them
George Ionitsa
Founder &
Maths and Coding Coach

What our students and parents say about learning with us

"Olympiad Maths Lessons helped me a lot to get the Gold medal in Junior Maths Challenge"
St. Paul's Student
"Thanks to the 'Data Science' and 'Coding in Python' lessons I got accepted to my dream university."
Michael
Data Science Student
Warwick University
"Great courses, which thoroughly explained topics beyond the capability of the GCSE answer sheet. Thanks so much."
Ivan
GCSE Student in Dubai
"Financial Mathematics! Best course to understand Python and Mathematics behind Finance!"
Gleb
VC Investor
"We got silver in PMC! Thanks George!"
Mum of St. Paul's Student
Prepare for the Primary Maths Challenge
"My daughter took a batch of 10 classes with George to understand Python with Turtle. I found George extremely knowledgeable and accessible."
Dad of Latymer School Student
Python with Turtle