Python | Boolean list of AND and OR operations

Python Methods and Functions

Method # 1: And operation — Using all()
The solution to this problem is quite simple, but knowledge of the application is required. The all () function performs a logical AND on the list and returns the result.

# Python3 code to demonstrate how it works
# A logical list of AND and OR operations
# AND Operation - Use all ()

  
# initialize the list

test_list = [ True , True , False , True , False ]

 
# print about original list

print ( "The original list is: " + str (test_list))

 
# Logical list of AND and OR operations
# AND Operation - Use all ()

res = all (test_list)

 
# print result

print ( "Result after performing AND among elements:" + str (res))

Output: The original list is: [True, True, False, True, False] Result after performing AND among elements: False

Method # 2: OR operation — using any()
This task can be accomplished with any () . This checks any element True in the list and returns True, in which case it returns False.

# Python3 code to demonstrate how it works
# A logical list of AND and OR operations
#OR operation - Using any ()

 
# initialize the list

test_list = [ True , True , False , True , False ]

  
# print original list

print ( " The original list is: " + str (test_list ))

 
# Boolean list of AND and OR operations
Operation # OR - Using any ()

res = any (test_list)

 
# print result

print ( "Result after performing OR among elements:" + str (res))

  Exit:

 The original list is: [True, True, False, True, False] Result after performing OR among elements: True 




Get Solution for free from DataCamp guru