Boolean in python

Hello everyone

In this tutorial, we are going to learn about booleans in python in a detailed manner with some easy examples.

Python Boolean type is one of the built-in data types provided by Python, which represents one of the two values i.e True or False.

Generally, it is used represent the truth values of the expressions.

python Booleans

Boolean represents one of the two values : True or False

Boolean values :

You can evaluate any expression in Python, get one of the two answers  i.e True or False.

when you compare two values, Python returns the boolean answer.

Example:

print(5 > 4)
print(5 == 4)

Output :

True
False

Evaluate values and variables :

we can evaluate values and variables using Python bool() function. This method  is used to return or convert a value to a boolean value i.e., True or False using the standard truth testing procedure.

syntax:

bool([x])

Example :

Evaluate two variables:

x = "Python"
y = 1
print(bool(x))
print(bool(y))
Output :

True
True

Most values are True :

Any value is evaluated  to True if it has some content. Any string is True, except empty strings. Any number is True, except 0. Any list, tuple, set, and dictionary are True, except empty ones.

Example :

bool(123)
bool("car")
bool(["rose", "lilly"])

Output :

True
True
True

Functions can return a boolean :

You can always create a function that return boolean value. And also you can execute the expression based on boolean answer of a function.

Python has many in-built function that returns Boolean value, like isinstance() function.

Example :

def Function() :
   return True

print(Function())

Output :

True

example using isinstance() function:

to check whether value is integer or not

a = 10
print(isinstance(x, int))

Output :

True

 

Leave a Comment

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

Scroll to Top