Updated April 15, 2023
Introduction to Logical Operators in Python
Logical operators in programming help to achieve and attest several logical complexities in program control flow and logic design; the three major logical operators in python are AND, OR, and NOT. All these Logical operators in python are explained below briefly.
Types of Logical Operators with Examples
Below are some of the logical operators of python:
1. AND Operator
In python programming, the reserved keyword ‘ AND ‘ is used to achieve the logical and operation. The AND keyword works in such a manner that the below-given operation will take place only when both the statements given in the AND condition are true.
Code:
Value_verified = 5
if Value_verified > 1 and Value_verified < 10 :
print( " \n \n Hello World ! \n " )
else:
print( " \n \n END OF PROGRAM \n \n " )
Output:
Explanation: This program is used to confirm whether the specified integer lies amid a precise range of values. The assortment required to be verified is designed by means of AND operator. The range’s starting and ending values are checked as greater than and lesser than values ( Value_verified > 1 and Value_verified < 10 ). When the value verified falls within this range, then the console message ” Hello World ! ” is printed. In case if the above conditions do not succeed, then the console statement ” END OF PROGRAM ” is printed.
Code:
class Boolean_value_check:
check_element = 0
def __init__(self, i):
self.check_element = i
# Boolean check method is called
def __bool__(self):
return True if self.check_element > 0 else False
Check_value1 = -10
Check_value2 = 10
print( ' \n First Value : ' , Check_value1 )
print( ' Second Value : ' , Check_value2 )
check_value1_Boolean = Boolean_value_check(Check_value1)
check_value2_Boolean = Boolean_value_check(Check_value2)
if check_value1_Boolean and check_value2_Boolean:
print( 'Both First value and Second value are positive \n \n ')
else:
print( ' Both First value and Second value are not positive \n \n ')
Output:
Explanation: This program is allowed to confirm whether the given twosome of number is a positive integer or not. The keyed-in inputs are pushed into variable instances. The __bool__function always returns a Boolean value( TRUE or FALSE). The negative integer check is achieved inside this function by checking whether the received arguments are greater than zero. When the values are greater than zero, then they are obliviously positive integers and the Boolean value TRUE is returned for them, and vice versa happens for negative integers. When together values stand to be true, then the console message stating ” both values are positive ” is printed. When one or both are negative, then the console message that ” both are not positive ” is printed.
2. OR Operator
The logical operation ‘OR’ means very similar to the grammatical meaning of OR; The operator works in such a manner that if anyone among the given statements is itself TRUE, then it performs the below-stated operation. In other words, When This operation will be used when the operation is expected to happen in the case where even one of the conditions stands true.
Code:
class Boolean_value_check:
check_element = 0
def __init__(self, i):
self.check_element = i
# Boolean check method is called
def __bool__(self):
return True if self.check_element > 0 else False
Check_value1 = -10
Check_value2 = 10
print( ' \n First Value : ' , Check_value1 )
print( ' Second Value : ' , Check_value2 )
check_value1_Boolean = Boolean_value_check(Check_value1)
check_value2_Boolean = Boolean_value_check(Check_value2)
if check_value1_Boolean and check_value2_Boolean:
print( 'Both First value and Second value are positive \n \n ')
elif check_value1_Boolean or check_value2_Boolean:
print( ' Either First value or Second value is positive \n \n ')
else:
print( ' Both values are negative \n \n ')
Output:
Explanation: This program is allowed to confirm whether the given twosome of number is a positive integer or not. The keyed-in inputs are pushed into variable instances. The __bool__function always returns a Boolean value( TRUE or FALSE). The negative integer check is achieved inside this function by checking whether the received arguments are greater than zero. When the values are greater than zero, then they are obliviously positive integers and the Boolean value TRUE is returned for them, and vice versa happens for negative integers.
When together values stand to be true, the console message stating ” both values are positive ” is printed. In addition to the above program in AND operator use, here one more condition is added to verify whether one among the given values are positive; this is achieved by means of an OR operator with the below condition check,
elif check_value1_Boolean or check_value2_Boolean:
According to this check, if one among the passed values is positive, then it prints the message in the console stating ” Either First value or Second value is positive” when one or both are negative, then the console message that ” Both values are negative”.
3. NOT Operator
The last logical operator in python is the NOT operator, The word ‘NOT’ is reserved for this logical operation. According to this operator, the given operation will be performed only when the mentioned condition stays as false,
Code:
check_element = 23
list_elements = [1,3,23,55,67,2,44,6789,333,4,557,214,6,9,6]
print(" \n Element Verified : " ,check_element)
print(" Listed Verfied with : " ,list_elements)
if check_element not in list_elements :
print(" \n Check element Not found in list \n")
else:
print(" \n Check element found in list ")
Output:
Explanation: This program is used to check whether the given number is part of a specified list. This check is achieved by using the NOT logical operator by means of the below condition,
if check_element not in list_elements
If the element is found in the given list, then the console message ” Check element found in the list ” is printed; else, the console message ” Check element Not found in the list” is printed.
Recommended Articles
We hope that this EDUCBA information on “Logical Operators in Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.