Python Booleans: Check the truth value

Image credit: Real Python

Boolean Values

In a programming language, we use boolean values to check the truth value of the expression.

As a programming language python also uses boolean values.

We all use boolean when we comparing the two values or variable.

Boolean: in and if statement

When we run condition then a message or value print-based upon the True or False.

Example

Most Values are True

• Any value is evaluated to true if it is some sort of content.

• Strings are true, except empty string.

• Any number is True, except 0.

• Any list, tuple, set and dictionary are true, except’s empty ones.

Some Values are False

There are (),{},[],” “, the number 0 and the value None. And of course, the value False evaluated to False.

Example

• One more values or object evaluated to false, if you have an object that is made from a class with __len__ function that returns 0 or False.

Example

Function can return a Boolean

You can create a function that can return a boolean value.

• Code based on function can return a boolean answer.

Example

• Python has many built-in values that return a boolean value.

• Eg, the isinstance() function can determine if an object is a certain data type.

LIKE ME