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

Everything you need to know about FIPS 140-3 on Ubuntu | Videos

FIPS 140 is a highly demanding security standard that’s mandatory for almost all high-security and…

10 minutes ago

A CISO’s preview of open source and cybersecurity trends in 2026 and beyond

Open source has come a long way. Recently I was watching a keynote address by…

5 days ago

Canonical Kubernetes officially included in Sylva 1.5

Sylva 1.5 becomes the first release to include Kubernetes 1.32, bringing the latest open source…

5 days ago

Canonical expands total coverage for Ubuntu LTS releases to 15 years with Legacy add-on

Expansion ensures business continuity without forcing major upgrades Today, Canonical announced the expansion of the…

6 days ago

Announcing YARD-Lint: Keep Your Ruby Documentation Solid

TL;DR: YARD-Lint catches documentation issues, just like RuboCop for code. Star it and use it…

6 days ago

Canonical releases FIPS-enabled Kubernetes

Deploy a FedRAMP-ready kubernetes cluster and application suite, with FIPS 140-3 crypto and DISA-STIG hardening…

1 week ago