What is Python all() function and how it works for beginners?
In this post, we’ll learn about Python all() function and how it works for beginners with real-life examples to make learning easier.
Table of Contents
Introduction
Python all() function returns true if the object is iterable. It takes iterable as a single argument and returns a boolean value.
Syntax
all(iterable)
Examples
Checking what the all()
the function returns when used with list, tuples, sets, and dictionaries.
# on list mylist = [1,2,3] x = all(mylist) print("On list {}\n".format(x)) # on empty list mylist = [] x = all(mylist) print("On empty list {}\n".format(x)) # on tuples myTuple = ("this", "is", "tuple") x = all(myTuple) print("On empty list {}\n".format(x)) # on empty tuples myTuple = () x = all(myTuple) print("On empty list {}\n".format(x)) # on sets mySet = {"this", "is", "sets"} x = all(mySet) print("On empty set {}\n".format(x)) # on empty sets mySet = {} x = all(mySet) print("On empty set {}\n".format(x)) # on dictionary myDict = {"name" : "james", "age" : 12} x = all(myDict) print("On Dictionary {}\n".format(x)) # on empty dictionary myDict = {} x = all(myDict) print("On empty Dictionary {}\n".format(x)) # PYTHON OUTPUT On list : True On empty list : True On Tuple : True On empty Tuple : True On set : True On empty set : True On Dictionary : True On empty Dictionary : True
The all()
function also works on strings.
myStr = "Welcome to Python" x = all(myStr) print("On String {}\n".format(x)) # on empty string myStr = "" x = all(myStr) print("On empty String {}\n".format(x)) # PYTHON OUTPUT On String True On empty String True
On range()
function.
x = all(range(0,6)) print("On range function 1 : {}\n".format(x)) x = all(range(1,6)) print("On range function 2 : {}\n".format(x)) # PYTHON OUTPUT On range function 1 : False On range function 2 : True
You can see that for range(0,6)
the all()
function returns False
and for range(1,6)
returns True
Let us look at another example using a list, tuples, etc.
On iterable objects with the item as False
or numeric zero number.
# on list mylist = [0,1,False] x = all(mylist) print("On list : {}\n".format(x)) # on tuples myTuple = (0, False) x = all(myTuple) print("On Tuple : {}\n".format(x)) # on sets mySet = {0, False} x = all(mySet) print("On set : {}\n".format(x)) # on dictionary myDict = {0 : "james", 0 : 12} x = all(myDict) print("On Dictionary : {}\n".format(x)) # on string myStr = "0" x = all(myStr) print("On String {}\n".format(x)) # PYTHON OUTPUT On list : False On Tuple : False On set : False On Dictionary : False On String True
So by these examples, we come to know that all()
function result may change if one of its items is 0 or False.
If items are not zero nor False
than it returns True
and for zero as a string, it returns True
.
Conclusion
You can reach at the end of our post on Python all() function and how it works?. we appreciate you for going through this post.
If you find anything difficult with our way of explanation please leave us a comment and if you have enjoyed reading our post then help
us grow by sharing this post link.
Related Posts
- Python Super() Function for Beginners
- Python File Handling Beginners – Open, Read, and Close File Functions




