In this article we will explore Python boolean data type.
Table of Contents
- Introduction
- Create a boolean in Python
- Boolean expressions in Python
- Use booleans in control structures in Python
- Conclusion
Introduction
In Python, boolean is a subtype of numeric data type, and it represents one of two values: True or False (which can also be represented by integers 1 and 0).
Learning data types in each programming language is essential to understand the code and programs.
Boolean data type is widely used in many mathematical, statistical, data science, and machine learning solutions built in Python.
Create boolean in Python
In Python, you can create a boolean in 3 different ways:
- By assigning a True or False value to a variable
- By using a Python bool() constructor
- By using a boolean expression.
Assign True or False value to a variable
Let’s create two variables and print them out:
#Create booleans
is_sunny = True
is_raining = False
#Print variables:
print(is_sunny)
print(is_raining)
and you should get:
True
False
Use a Python bool() constructor
In Python, booleans can be also created by using a bool() constructor.
The bool(argument) constructor takes a specified argument and returns its boolean value.
It returns:
- True if argument is either any number (except zero), or True, or a string
- False if argument is empty, False, or None
Let’s look at a few examples of bool() with True arguments:
#Number
t1 = bool(5)
#True
t2 = bool(True)
#String
t3 = bool('Python')
#Print variables
print(t1, t2, t3)
and you should get:
True True True
Now, let’s look at a few examples of bool() with False arguments:
#Empty
f1 = bool()
#Empty list
f2 = bool([])
#False
f3 = bool(False)
#None
f4 = bool(None)
#Zero
f5 = bool(0)
#Print variables
print(f1, f2, f3, f4, f5)
and you should get:
False False False False False
Boolean expressions in Python
In Python, you can also create booleans using boolean expressions.
A boolean expression is simply an expression that evaluates to either True or False.
For example, we can compare two numbers:
#Create two numbers x and y
x = 5
y = 3
#Evaluate using boolean expression
is_x_greater_than_y = x > y
#Print boolean
print(is_x_greater_than_y)
and you should get:
True
Here are the comparison operations that can be used to evaluate a boolean expression:
Operation | Result (boolean) |
x > y | x is greater than y |
x >= y | x is greater than or equal to y |
x < y | x is less than y |
x =< y | x is less than or equal to y |
x == y | x is equal to y |
x != y | x is not equal to y |
Use booleans in control structures in Python
In Python, booleans are often used in control structures that determine the flow of execution of a program.
The simplest example is using booleans in Python if statements:
#Create two numbers x and y
x = 5
y = 3
#Create a control structure
if x > y:
print('x is greater than y')
else:
print('x is not greater than y')
and you should get:
x is greater than y
Conclusion
In this article we explored Python boolean data type including its use in boolean expressions and control structures.
As your next step in learning Python consider reading about Python data types and data structures in the following articles: