Any and All Operator In Python

Any operator

This operator return True if any of the items in the list is True. It return False if empty or all the items are False.

Example #1:-

print(any([False,False, False]))
Python

Output :-

False
Markup

Example #2:-

print(any([False, True, False]))
Python

Output :-

True
Markup

Example #3 :- Program using any operator

list1 = []
list2 = []
 
for i in range(10):
    list1.append(4*i)
     
for i in range(10):
    c = list1[i] % 5 == 0
    list2.append(c)
 
print(list2)
print(any(list2))
Python

Output :-

[True, False, False, False, False, True, False, False, False, False]  
                                                                        
True
Markup

 

Explanation :- Its return True because in the list its find one item is True

All operator

This all operator return True if all the items of the list are True, if any one of them is False then it will return False.

Example #4:-

print(all([True, True, True]))
 
print(all([True, False, True]))
Python

Output :-

True
False
Markup

 Example #5 :-

list1 = []
list2 = []
 
for i in range(21):
    list1.append(4*i-3)
     
for i in range(20):
    c = list1[i] % 2 == 1
    list2.append(c)
 
print(list2)
 
print(any(list2))
Python

 Output :-

[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
                      
True
Markup

 Explanation :- Its return True because all the items in the list are True.

I hope You understand the concept of the Any and All operator in python. If you have any query or suggestion please do a comment.

Thank You.

Comments

Popular posts from this blog

⚠️How to create a virus in termux app ?

List in Python

How To Install Metasploit In Termux App ?