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!

Ubuntu Server Admin

Recent Posts

Cut data center energy costs with bare metal automation

Data centers are popping up everywhere. With the rapid growth of AI, cloud services, streaming…

23 hours ago

Build the future of *craft: announcing Starcraft Bounties!

Our commitment to building a thriving open source community is stronger than ever. We believe…

23 hours ago

NodeJS 18 LTS EOL extended from April 2025 to May 2032 on Ubuntu

The clock was ticking: Node.js 18’s upstream End of Life (EOL) The OpenJS Foundation is…

23 hours ago

Native integration now available for Pure Storage and Canonical LXD

June 25th, 2025 – Canonical, the company behind Ubuntu, and Pure Storage, the IT pioneer…

2 days ago

Revolutionizing Web Page Creation: How Structured Content is Slashing Design and Development Time

Co-authored with Julie Muzina A year ago, during our Madrid Engineering Sprint, we challenged ourselves…

3 days ago

Ubuntu Weekly Newsletter Issue 897

Welcome to the Ubuntu Weekly Newsletter, Issue 897 for the week of June 15 –…

4 days ago