×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python all() Function

By IncludeHelp Last updated : December 07, 2024

Python all() Function

The all() function is a library function in Python, it 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

The following is the syntax of all() function:

all(iterable)

Parameter(s):

The following are the parameter(s):

Return Value

The return type of all() function is <class 'bool'>, it returns a Boolean value either True or False.

Example 1

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 

Example 2

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.