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 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.
Operator | Meaning | Example |
---|---|---|
> | Greater than – True if left operand is greater than the right | x > y |
< | Less than – True if left operand is less than the right\\\ | x < y |
== | Equal to – True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to – True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to – True if left operand is less than or equal to the right | x <= y |
Logical Operators
There are Three Logical operators: and, or, not
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True 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.
a | b | a and b |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
or Table : It takes two operands.
a | b | a or b |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
not Table : It takes only one operand.
a | not a |
---|---|
False | True |
True | False |
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