Python Booleans – What Is It?

We will study Python Booleans using examples in this chapter in the hope that it will meet the learning objectives.

Booleans can have one of two values: True or False

Python Booleans - Learn with Examples

Python Booleans Values

A True or False expression is commonly used in programming.

The Python Boolean expression evaluates to True or False depending on the expression.

Python returns the Boolean answer when two values are compared:


print(15 > 8) print(15 == 7) print(15 < 6)

Python returns True or False when you apply a condition in an if statement:

Determine whether the if condition is True or False by printing:


mr = 150 ex = 54 if mr > ex: print("ex is greater than mr") else: print("ex is not greater than mr")

Boolean keywords in Python

A built-in name is not a keyword. Python treats them as regular variables. The built-in value will be overridden if you assign a value to them.

Contrary to this, True and False are not built-ins – These are keywords.

Because they’re expressions, they can be used wherever other expressions can be used, like 1 + 1.

True and False can’t be assigned a value, but variables can have Boolean values:


True = 5 False = 6print(True) print(False)

The above example will result in an error in Python.

Evaluate Values and Variables

You can evaluate any value using Python’s Booleans bool() function, and it will return True or False.

Strings and numbers are evaluated as follows:


print(bool("Mr-Examples")) print(bool(25))

Determine the following two variables:


mr = "Elon" ex = 25 print(bool(mr)) print(bool(ex))

Functions And Booleans

When it comes to Python Booleans, you can create functions that return Boolean values:

You can print the result of a function by typing:


def myFunction() : return True print(myFunction())

A function’s Boolean answer can be used to execute code:

In the case that the function returns True, print “True”; otherwise, print “False”:


def mrxFunction() : return Trueif mrxFunction(): print("True") else: print("False")

There are a number of built-in functions in Python that produce boolean values, for example isinstance(), which can be used to determine if an object is of a particular data type.

An integer can be checked by checking the following:


mr = 110 print(isinstance(mr, int))


False values

The only values that evaluate to False are empty values, such as (), [], [], “”, the number 0, and None.

Booleans in Python evaluate to False when False is assigned.

False will be returned if:


print(bool(False)) print(bool(None)) print(bool(())) print(bool([])) print(bool({})) print(bool(0)) print(bool(""))

Objects that are derived from classes that have len functions that return 0 or False may also evaluate to False:


class myclass(): def __len__(self): return 0 myobj = myclass() print(bool(myobj))

Almost All Values Are True

In Python, almost any value that has some type of content is evaluated to True.

Except for empty strings, any string is True.

Except for 0, any number is True.

Lists, tuples, sets, and dictionaries are all True, except empty ones.

Returns True when the following conditions are met:


print(bool("xyz")) print(bool(91)) print(bool(["Tesla", "Google", "Youtube"]))

Python Booleans Importance

Although Python Booleans posses are very important aspect of python programming. Here are few points that will illustrate the importance of the Python Booleans:

  1. Booleans allow for decision-making in Python by evaluating conditions and determining whether certain code blocks should be executed based on the truth or falsehood of those conditions.
  2. Booleans are used to control the flow of loops in Python. They help determine whether a loop should continue iterating or terminate based on the truth value of a condition.
  3. Booleans are used to compare values and perform logical operations. They enable evaluating expressions, checking for equality, inequality, greater than or less than relationships, and combining conditions using logical operators such as “and,” “or,” and “not.”
  4. Booleans can be used to indicate error conditions or exceptional cases in Python. They help in detecting errors, validating inputs, and handling exceptional situations in code execution.
  5. Booleans are vital for writing tests and assertions. They allow for checking the expected behavior of code, verifying conditions, and validating the correctness of program output.
Don’t hesitate to leave your reaction. We value your input!


We value your feedback.

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!

Leave a Reply

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