Home » Python

all() function with example in Python

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

Python all() function

all() function is used to check whether all elements of an iterable are True or not. It accepts an iterable container and returns True if all elements are True, else it returns False.

Syntax:

    all(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
    False

Python code to check whether all elements of an iterable are true or not (printing return values)

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

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

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

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

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

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

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

Output

True
False
False
True
True
True 

Python code to check whether all elements of an iterable are true or not (checking using condition)

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

list1 = [10, 20, 30, 40]
list2 = [10, 20, 30, 0]

# checking with condition
if all(list1)==True:
    print("list1 has all true elements")
else:
    print("list1 does not has all true elements")

if all(list2)==True:
    print("list2 has all true elements")
else:
    print("list2 does not has all true elements")    

Output

list1 has all true elements
list2 does not has all true elements
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




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.