# 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.

## Logical Operators

There are Three Logical operators: and, or, not

## 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.

or Table : It takes two operands.

not Table : It takes only one operand.

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.

## Python3

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

Output :

```False
True```

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

## Python3

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

Output :

```1
2
3```

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

## Python3

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

Output :

`YES !`

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

## Python3

 `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"``)` `    ``else``:``        ``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