Booleans in Python

Booleans in Python are one of five built-in Data Types and can either be True or False only. Formally as per Python’s Documentation Boolean Data Type is called bool which can be True/False. Moreover bool is subclass of Python’s Integer class, meaning actually True/False are integers 1/0.

You can play around with this for fun, to see Is actually bool a subclass of int class?. Just open terminal/command line on your PC and type in python3, this will open up interactive python shell. Now write isinstance(True, int) this will return True meaning, Yes True is an instance of class int. Similarly for instance(False, int) will also return True, which implies that False is also an instance of int class.
Typically this behaviour of treating bool(True/False) as integers internally is achieved by defining str and repr string formatting methods for Bool Class.

Bool was added to Python Programming Language only in 2002 before that there was no Boolean Type. Read PEP 285 — Adding a bool type by Guido for knowing more about Story of addition of Boolean to Python.

Two Boolean Values in Python

As I’ve stated above Boolean can be True or False. But next question to be asked is How to use these in programming? By using operators you can make expressions to evaluate to a boolean in Python. And then based upon these evaluations, you can design Control Flow in code.
Below is a Python Code example showing How to use Bools?

if (x > 10):
	print("x is greater than 10")
else:
	print("x is less than 10")

In the example above, x > 10 will evaluate to True if x value is 11, 12, 13 ……. and then print(“x is greater than 10”) statement will be executed, otherwise print(“x is less than 10”) will be executed by Python Interpreter.

Boolean True

Most objects in Python does evaluate to True. For example – lists, strings etc.(If not empty) will evaluate to True if passed to bool() method.
Like bool(“Computer Science Hub”) will evaluates to True, but bool(” “) will not rather it evaluates to False.

Boolean False

Python objects which doesn’t have any value evaluates to False. Most of empty objects if passed to bool() function will return False.
For example – bool(” “), bool([ ]), bool({ }), bool(()) will evaluates to False.

Python Operators Using Boolean Types

I’ve already put together an article covering Operators in Python Programming Language. That article also contains some Boolean based Python Operators as well. Click on below links to see How Boolean Types can be used for Mathematical Operations in Python?

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts