Python all() Function – Easy Explanation with Examples
Description Tag- Built-in Python all() Function that helps to check truthy in an iterable–With easy explanation, with various examples. It checks whether all values are True or not.
Introduction
Python is a plausible and easy programming language. We instruct a computer to do a task through Python. Python has built-in functions that help to save time and write less code.
In this blog post, we will explore the built-in Python all() function.
What we will learn about Python's all() function:
● What the Python all() function is
● How it works
● Examples codes
What is the all() Function?
Python all() is a built-in function that checks whether all values in the iterable (List, Tuples, Set, or Dictionary) are True or not.
❖ If all value is True, all() gives True
❖ If all value is not True(Even one is False), all() gives False
How Does all() Work?
The all() function checks every item, one after one, in the iterable.
❖ If it finds any item False, it stops and returns False
❖ If it checks everything and finds all True, it returns True
Syntax of all()
We write all() in Python like below.
all(iterable)
Example 1: Using all() with True and False Value
Example 1: All values are True
Code:
values = [True, True, True]
print(all(values))
Output:
True
Explanation:
Because all values in the example codes are True, the output is True.
Example 2: One value is False
values = [True, False, True]
print(all(values))
Output:
False
Explanation:
Because one value is False, the output is False.
Example 2: Using all() with Numbers
Python considers 0 as False and any number as True.
Example:
numbers = [5, 2, 1, 7]
print(all(numbers))
Output:
True
Explanation:
As we can see in the above code, all numbers are non-zero, so the output is True.
Example with 0:
numbers = [5, 3, 0, 8]
print(all(numbers))
Output:
False
Explanation:
In the above code, there is a 0 value & 0 is False, so the result is False.
Example 3: Using all() with Strings
Python considers an empty string(‘’’’) as False and a non-empty string (‘‘Hello World’’) as True.
Example:
words = ["apple", "guava", "pineapple"]
print(all(words))
Output:
True
Explanation:
As all strings have words inside them. So output is True.
Example with empty string:
words = ["orange", "", "grapes"]
print(all(words))
Output:
False
Explanation:
In the above example code, one string is empty, sothe result is False.
Example 4: all() with Conditions
In this example, we will take a real life example. We will check if all students passed or not.
Example:
marks = [82, 75, 68, 38]
result = all(mark > 33 for mark in marks)
print(result)
Output:
True
Explanation:
All marks are more than 33, so the output is True & everyone has passed.
Example, if someone failed:
marks = [65, 50, 28, 76]
result = all(mark > 33 for mark in marks)
print(result)
Output:
False
Explanation:
One student got less than 33, so the result is False, which means one of them failed.
Real-Life Example Codes:
Homework Example
Imagine you check the homework of students:
homework_done = [True, True, True, True]
print(all(homework_done))
Output:
True
As output is True, it means all students did their homework.
If one student did not do the homework:
homework_done = [True, True, False, True]
print(all(homework_done))
Output:
False
As output is False, it means one student did not do their homework.
What if the List is Empty?
Example:
items = []
print(all(items))
Output:
True
Why?
There is nothing False in the list, so Python gives output True.
Difference Between all() and any()
|
Function |
Meaning |
|
all() |
Everything must be True |
|
any() |
At least one must be True |
Example:
values = [True, False, False]
print(all(values))
Output:
False
Example:
values = [True, False, False]
print(any(values))
Output:
True
Tips when coding to avoid mistakes:
❖ Don’t forget brackets
❖ Don’t Mix True and False spelling, like true or false
❖ Don’t use wrong data inside an iterable
❖ Please check values carefully when typing
Practice Questions
- What would be the output?
print(all([False, True, True]))
- What would be the output?
print(all([9, 2, 11]))
- What would be the output?
print(all(["", "Guava"]))
Try Yourself Below Small Exercises:-
● Create a list of numbers that have 6 values.
● Use all() to check if all numbers are greater than 7
● Change numbers and see the output
Conclusion: -
● all() checks everything one by one in List, Set, Tuple, or Dictionary.
● Returns True if all values are True. And have no problem.
● Returns False if even one value is False.
● Works with lists, numbers, and strings.
Also Check:-
Visit the Python all() Function quiz page.
Download the Python all() Function worksheet to practice.
Other Tip:-
Python has many built-in helpful functions, like below:
- max()
- sum()
- any()
Please click on any of the above functions if interested in exploring them.