Home » Python

any() function with example in Python

Python any() function: Here, we are going to learn about the any() function in Python with example.
Submitted by IncludeHelp, on April 01, 2019

Python any() function

any() function is used to check whether any of the elements of an iterable is True or not. It accepts an iterable container and returns True, if one or more than one elements are true, else it returns False.

Syntax:

    any(iterable)

Parameter: iterable – an iterable container like list, tuple, dictionary.

Return value: bool – a Boolean value

Example:

    Input:
    val = [10, 20, 30, 40]
    print(all(val))
    val = [10, 20, 0, 40]
    print(all(val))

    Output:
    True
    True

Python code to check whether any of the elements of an iterable is true or not (printing return values)

# python code to demonstrate example
# of any() function

val = [10, 20, 30, 40]  #list with any true values
print(any(val))

val = [10, 20, 0, 40]  #list with a flase value
print(any(val))

val = [0, 0, 0, 0.0]  #list with any false values
print(any(val))

val = [10.20, 20.30, 30.40]  #list with any true(float) values
print(any(val))

val = [] #an empty list
print(any(val))

val = ["Hello", "world", "000"] #list with any true values
print(any(val))

Output

True 
True 
False
True 
False
True

Python code to check whether any of the elements of an iterable is true or not (checking using condition)

# python code to demonstrate example
# of any() function

list1 = [10.23, 0, 0.0, "Hello"]
list2 = [0, 0, 0, 0.0]

# checking with condition
if any(list1)==True:
    print("list1 has any true element(s)")
else:
    print("list1 does not has any true element(s)")

if any(list2)==True:
    print("list2 has any true element(s)")
else:
    print("list2 does not has any true element(s)")    

Output

list1 has any true element(s)
list2 does not has any true element(s)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.