Categories: TutorialsUbuntu

Python bool() Function in Linux

Introduction

In Python, the bool() function is used to convert a value to a boolean value (True or False) to check the truth according to the standard.

If the object has a value of None, False or equal to 0, the bool() function will return false. Except for all of the above, the bool() function will return true.

The following article is a detailed guide on how to use the bool() function in Python as we go through the section below.

Example

x = []

print(bool(x))

y = 1

print(bool(y))

Output:

False

True

Definition

The bool() function can return 2 values: True and False.

If object is empty like (), [], {}, the function will return False.

The
Sponsored
syntax

bool(object)

Parameter Values:

object: any object. For example: number, string, list…

Sponsored

More example

Example 1: Basic bool() function

x = 1

y = 2

print(bool(x == y))

Output:

False

Example 2: Parity check program

def check(x):

return (bool(x % 2 == 0))

x = 4

if (check(x)):

print("Correct")

else:

print("Incorrect")

Output:

Correct

Conclusion

Hope you understand our tutorial on how to use the bool() function in Python.

Thanks for reading!

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

4 hours ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

4 hours ago

How to Fix ‘Please Install All Available Updates’ Error When Upgrading Ubuntu 18.04 to 20.04 LTS

One particularly frustrating error that many users face when trying to upgrade from Ubuntu 18.04 …

4 hours ago

How to fix “Release is not valid yet” Error in Docker Containers

In the world of containerization, time synchronization issues can create unexpected roadblocks when working with…

4 hours ago

How to fix “Externally Managed Environment” Pip Errors on Ubuntu

If you’ve recently upgraded to Ubuntu 23.04 or newer, you might have encountered a frustrating…

4 hours ago

Ubuntu now officially supports NVIDIA Jetson: powering the future of AI at the edge

Canonical announces the General Availability of Ubuntu for the NVIDIA® Jetson Orin™ for edge AI…

11 hours ago