Python Booleans – A data type to find two possible outcomes

In the world of computer science, Boolean is a data type that can only have two possible values either True or False.

In this article, we are going to look at the Python Booleans, we will understand how to declare a boolean and the bool() function. We will also learn the operations you can perform on booleans.

What are Python Booleans?

The Python Booleans is a basic data structure which holds either False or True values.

A lot of functions only require a boolean value to return, for example, we used the isdisjoint(), issuperset() and issubset() functions. The result is only supposed to be True or False.

Declaring a Boolean in Python

Booleans can be declared just like an integer. We assign either True or False to an identifier and we don’t use quotes like in a string.

Code:

Start = False
Run = True
print( type(Start) )

Output:

<class ‘bool’>

Python bool() function

The bool() function is used to convert a value into a boolean value ( True or False). We can convert integer, strings, list, map into boolean values.

Code:

print( bool(0) )
print( bool(10) )
print( bool(‘’))
print( bool(‘hello’))
print( bool({1,2,3}))
print( bool(()))

Output:

False
True
False
True
True
False

Operations on Python Booleans

False is equivalent to zero and True is equivalent to 1 so we can perform various operations on booleans.

1. Arithmetic Operators in Python

1.1 Addition

Let’s add two boolean values.

True + True 2
False + True 1
True + False 1
False + False 0

1.2. Subtraction

True – True 0
False – True -1
True – False 1
False – False 0

1.3. Multiplication

True * True 1
False * True 0

 

True * False 0
False * False 0

1.4. Division

As we know, division by zero gives us an error so dividing anything by False will also give us an exception.

Code:

print(True/True)
print( True/False)

Output:

1.0
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
ZeroDivisionError: division by zero

1.5. Modulus, exponentiation and Floor division

Code:

print( False%True)
print( True**False)
print( True//True)

Output:

0
1
1

2. Relational Operators in Python

All the relational operators (<,>, <=, >=, !=, ==) return boolean values after comparing.

Try playing with relational operators on your own and see what output you will get.

Code:

print( True>=True or False>True )
print( False<True and False!=True and False==True)

Output:

True
False

3. Bitwise Operators in Python

Bitwise operations are performed bit by bit on binary values. In the case of boolean values, they are treated as a single bit 1 as True and 0 as False.

Bitwise operators include &, |, ^, ~, <<, >>.

Let’s see some bitwise operations performed on booleans:

True & False False
True | False True
True ^ False True
~True -2
~False -1
True<<True 2

4. Logical Operators in Python

Logical operators (‘and’ and ‘or’) also work the same on boolean values.

Code:

print(False or True)
print(False and False)

Output:

True
False

5. Identity Operators in Python

At last, we will apply the boolean operations on identity operators ‘is’ and ‘is not’.

Code:

True is True
False is 0

Output:

True
False

Summary

In this article, we talked about the concept of Python booleans. We saw how you can create the boolean data type, the bool() function and all the operations you can perform with them.

So this was all about TechVidvan’s Python booleans article.

TechVidvan Team

The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today’s tech industry.