free stats

Python Boolean

Last Update : 29 Sep, 2023 Python, Programming

In this tutorial, you will learn about Python boolean data type.

The boolean data type can be identified as a fundamental data type that represents the truth value of an expression in a computer program.

True and False are the two possible values that are available in the boolean data type.

Boolean values are keywords in Python. Therefore, you must write with an initial letter as a capital letter. (True and False). Any other case, such as true or false, will result in a NameError.

We can use booleans in programming to control the flow of code,  perform logical operations and make decisions.

How to create boolean variables?

An example of creating a Python boolean variable is as follows.

is_active = True
is_correct = False


Boolean Operations in Python

Boolean data types are most commonly used in Python for logical operations and comparisons. The following are the primary Boolean operations in Python.

Comparison Operators

You can use Python comparison operators to compare two values and return a Boolean result. Following are the common comparison operators included in Python.

  • "==" ( Equal to )
  • "!=" ( Not equal to )
  • "<" ( Less than )
  • ">" ( Greater than )
  • "<=" ( Less than or equal to )
  • ">=" ( Greater than or equal to )

You can compare two numbers as follows.

x = 50
y = 100
result = x < y

This program produces the following results -:



Logical Operators

You can use logical operators to combine multiple Boolean values or expressions. The primary logical operators in Python includes are "and", "or" and "not".

  • "and" -: Returns True when both operands are True.
  • "or" -: Returns True when at least one operand is True.
  • "not" -: Returns the opposite Boolean value of the operand.

The following example shows the usage of logical operators.

a = True
b = False

result1 = a and b
result2 = a or b
result3 = not a


This program produces the following results -:



Boolean Functions

There are built-in functions in Python to work with Boolean values. For example, the bool() function is used to convert other data types into Boolean values. This function returns True for non-zero numbers or non-empty sequences such as lists or strings and False otherwise.

The following example shows the usage of Boolean functions.

x = 124
y = []

result1 = bool(x)
result2 = bool(y)


This program produces the following results -:


Boolean data types are used extensively in programming for various purposes such as Control Structures, Data Filtering, Flagging and state Management, etc.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.