any(): checks if at least one element in an iterable (such as a list, tuple, or set) is true

any(my_iterable)
my_iterable : iterable input
Return Boolean value True or False based on this table.

Difference between any() and all()

Elements of the iterable any()
Output
all()
Output
All are TrueTrueTrue
All are FalseFalseFalse
One value is True ( Others False)TrueFalse
One value is False ( Others True)TrueFalse
Empty FalseTrue
Read more on all() here.

Empty List

list=[]
print(any(list)) # False

All elements are False

list=[0,False]
print(any(list)) # False

One element is True

list=[False,True]
print(any(list))  # True

Using string

url='plus2net.com'
print(any(url))    # True
url='False'
print(any(url))    # True
We will use Dictionary

Output depends on Keys of the Dictionary ( Not Values ). If any one of the key is True then output is True. If all keys are False then output is False.
my_dict={0:'A',1:'B'}
print(any(my_dict)) # True 
my_dict={0:'A',0:'B'}
print(any(my_dict)) # False
my_dict={0:True,0:False}
print(any(my_dict)) # False
my_dict={1:True,2:False}
print(any(my_dict)) # True
We will use tuple
my_tuple=(True,1,12)
print(any(my_tuple)) # True
my_tuple=(True,0)
print(any(my_tuple)) # True
my_tuple=(0,0)
print(any(my_tuple)) # False
my_tuple=(0,1)
print(any(my_tuple)) # True
Using if else
a,b,c=0,0,1
if any((a,b,c)):
  print("Yes")
else:
  print("No") 
all() Iterator in and not in Membership operators
Variables in Python Keywords in Python
All Built in Functions in Python

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer