Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Understanding Boolean Logic in Python 3

  • Difficulty Level : Basic
  • Last Updated : 23 Jul, 2021

Booleans are simple and easy to use concepts that exist in every programming language. A boolean represents an idea of “true” or “false.” While writing an algorithm or any program, there are often situations where we want to execute different code in different situations. Booleans help our code to do just that easy and effective. More often, a boolean value is returned as a result of some kind of comparison operations.
There are two Boolean keywords: True and False
Operators : Operators are special symbols in Python that is used to perform arithmetic or logical computations. The values on which operation is to be done are called operands.while the the operation is denoted by operator(eg. +, -, /, *, %, etc.)

Comparison Operators

Comparison operators are used to compare values. It returns either True or False after computing the condition. 

>Greater than – True if left operand is greater than the rightx > y
<Less than – True if left operand is less than the right\\\x < y
==Equal to – True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to – True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to – True if left operand is less than or equal to the rightx <= y


Logical Operators

There are Three Logical operators: and, or, not

andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false.not x


Truth Table

A Truth Table is a small table that allows us, to give the results for the logical operators.
and Table : It takes two operands. 

aba and b

or Table : It takes two operands. 

aba or b

not Table : It takes only one operand. 

anot a

Example 1 :Checking whether a list is empty or not. We will pass the list in the bool() function. When the list is empty, False is returned and if the list is not empty True is returned.


def check_Empty(list_name):
if __name__ == "__main__":
    # making an empty list
    my_list =[]
    # calling our function
    # making an non-empty list
    my_list1 =[1, 2, 3]
    # calling our function

Output : 


Example 2 : Printing a range of number using the while loop, the while loop will run until the condition is True.


def print_range_numbers(n):
    i = 1
    # will execute until condition is True
    while  i <= n:
        i = i + 1
if __name__ == "__main__":
    n = 3
    # calling our function

Output : 


Example 3 :With the help of boolean, we can bound our program.


def myFunction() :
    return True
if __name__ == "__main__":
    if myFunction():
        # prints YES if myFunction() returns True
        print("YES !")
        # prints NO if myFunction() returns False
        print("NO !")

Output : 


Example 4 :Check greater of two numbers with the help of conditionals. With the help of boolean we can compare the results and execute accordingly


def check_greater(num_1, num_2):
    if num_2 > num_1:
        # after evaluating condition if it return True
        # then the following line of code get executed
        print("num_2 is greater than num_1")
        print("num_2 is not greater than num_1")
if __name__ == "__main__":
    num_1 = 3
    num_2 = 5
    # passing it to our function
    check_greater(num_1, num_2)

Output : 

num_2 is greater than num_1


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!