Introduction to Python Boolean Concept

In this article you will learn about Python Boolean.

Python Boolean – Before moving ahead, let’s know first a little bit about Python.

Introduction – Python is open-source and free programming language. It is easy to use and learn as compared to other languages like C ++, Java, C# and JavaScript. It is highly recommended and allows itself to get integrated with other languages like C ++, C#, etc., One of the most important key factors of Python is it is Object-Oriented Language and had been used by many giant companies for so many purposes such as handling file data, web app, Desktop application, Machine learning and so on.

Python Boolean – It is based on two boolean values that’s True and False. It is Python in-built function and used for evaluate any expression in Python, and get one of two answers, True or False.

Use of Boolean – When you compare two statements, Python evaluated or tested expression and returns the Boolean answer (True or False).

Example – Compare two conditions and get boolean values (True or False).

a = 10
b = 3

if a > b:
    print(True)
else:
    print(False)
python-boolean
As it is shown clearly that it returned True as condition is True.

Evaluate variables – The bool() function evaluates variable and values and returns True or False.

Example 1- Evaluates the variables and get True or False as Boolean value.

x = "Hello, Python"
y = 17

print (bool (x))
print (bool (y))
python-boolean
As it is shown clearly that both type of object is boolean supports therefore, it returned True.

Boolean value carries two values (True or False). It returns True in case of list, tuple, set, and dictionary and False in case of zero (0), empty string, and such values like [], {}, (), “” and False itself returns False.

Example 2- It returns both True and False as Python evaluates expression.

x = ('Hello, python')

y = [1,2,3,4]
z = {1,2,3,4}
c = ()
v = ('')
b = {}          

print(bool(x))
print(bool(y))
print(bool(z))
print(bool(c))
print(bool(v))
print(bool(b))
python-boolean
As it is shown clearly that it returned boolean values as per condition.

Functions returns Boolean – A function can have Boolean values. When you create a function and set it to Boolean then it returns True or False.

Example 1- Create a function which returns Boolean value.

def mytask():
    return True
print(mytask())
python-boolean
As it is shown clearly that function returned True as it stored boolean value.

Explanation – We defined a function as name mytask() and stored boolean value in it to return it as function is being called with the help of print() function.

Note: Built-in functions that has boolean result, that function always returns 0 or False for False and 1 or True for True unless it is defined.

Boolean based on code – A Boolean can have its value (True or False) based on code.

Example 2- Returns ‘Yes or No’ when function uses Boolean value.

def mytask():
    return True

if mytask():
    print('Yes')
else:
    print('No')
python-boolean
As it is shown clearly that it returned ‘Yes’ as condition is True.

Explanation – We defined a function as name mytask() and stored boolean value in it to return it ‘True’ and also defined an if…else condition. As function is being called it will check if…else condition and returned ‘Yes’ or ‘No’ according to stored value of mytask() function .

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Leave a Comment

Stay in the loop

codingstreets