Related Articles
Understanding Boolean Logic in Python 3
• Last Updated : 01 Aug, 2020

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.

OperatorMeaningExample
>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`

OperatorMeaningExample
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
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

`or `Table : It takes two operands.

aba or b
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

`not `Table : It takes only one operand.

anot a
FalseTrue
TrueFalse

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):``    ``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 : Prinitng 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:``        ``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.

 `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

 `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`

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up