This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Python all(iterable) | ||
|
||
`all()` is a built in function in Python 3, to check if all items of an [_iterable_](https://docs.python.org/3/glossary.html#term-iterable) is `True`. It takes one argument, `iterable`. | ||
|
||
## Argument | ||
### iterable | ||
|
||
The `iterable` argument is the collection whose all entries are to be checked. It can typically be a `list`, `str`, `dict`, `tuple` etc. | ||
|
||
## Return Value | ||
The return value would be a boolean. If and only if **all** entries of iterable are `True`, it returns `True`. This function essentially performs a Boolean `AND` operation over all elements. | ||
|
||
If even one of them is not `True`, it would return `False`. | ||
|
||
The `all()` operation is equivalent to (not internally implemented exactly like this) | ||
|
||
```python | ||
def all(iterable): | ||
for element in iterable: | ||
if not element: | ||
return False | ||
return True | ||
``` | ||
|
||
## Code Sample | ||
|
||
```python | ||
print(all([6, 7])) #=> True | ||
print(all([6, 7, None])) #=> False Because this has None | ||
print(all([0, 6, 7])) #=> False Because this has zero | ||
print(all([9, 8, [1, 2]])) #=> True | ||
print(all([9, 8, []])) #=> False Because it has [] | ||
print(all([9, 8, [1, 2, []]])) #=> True | ||
print(all([9, 8, {}])) #=> False Because it has {} | ||
print(all([9, 8, {'engine': 'Gcloud'}])) #=> True | ||
|
||
``` | ||
:rocket: [REPL It!](https://repl.it/CL9U/0) | ||
|
||
[Documentation](https://docs.python.org/3/library/functions.html#all) |