All and any in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Any and All are two built ins provided in python used for successive And/Or.

    Any
    Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables.
    It short circuit the execution i.e. stop the execution as soon as the result is known.

    Syntax : any(list of iterables)

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

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

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

    Output :

    False
    True
    True
    

    All
    Returns true if all of the items are True (or if the iterable is empty). All can be thought of as a sequence of AND operations on the provided iterables. It also short circuit the execution i.e. stop the execution as soon as the result is known.

    Syntax : all(list of iterables)

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

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

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

    Output :

    True
    False
    False
    

    Practical Examples

    list1 = []

    list2 = []

    for i in range(1,11):

        list1.append(4*i) 

    for i in range(0,10):

        list2.append(list1[i]%5==0)

    print('See whether at least one number is divisible by 5 in list 1=>')

    print(any(list2))

    Output:

    See whether at least one number is divisible by 5 in list 1=>
    True
    

    list1=[]

    list2=[]

    for in range(1,21):

        list1.append(4*i-3)

    for i in range(0,20):

        list2.append(list1[i]%2==1)

    print('See whether all numbers in list1 are odd =>')

    print(all(list2))

    Output:

    See whether all numbers in list1 are odd =>
    True
    

    Truth table :-

    All and any in python

    This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    What does all and any do in Python?

    The Python any() and all() functions evaluate the items in a list to see which are true. The any() method returns true if any of the list items are true, and the all() function returns true if all the list items are true.

    What is a any () in Python?

    Python any() Function The any() function returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.

    How do I use any in a list Python?

    In this tutorial, we will learn about the Python any() function with the help of examples. The any() function returns True if any element of an iterable is True . ... any() Return Value..

    What are 3 types of list in Python?

    Python Collections (Arrays).
    List is a collection which is ordered and changeable. Allows duplicate members..
    Tuple is a collection which is ordered and unchangeable. ... .
    Set is a collection which is unordered, unchangeable*, and unindexed. ... .
    Dictionary is a collection which is ordered** and changeable..