From 505eeb11cf1d4e4fdf87fe44853e5f32cb0f9828 Mon Sep 17 00:00:00 2001 From: Arijit Layek Date: Sat, 30 Apr 2016 13:58:12 +0000 Subject: [PATCH 1/3] Add Python any() --- python-any.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 python-any.md diff --git a/python-any.md b/python-any.md new file mode 100644 index 0000000000..84a036aa69 --- /dev/null +++ b/python-any.md @@ -0,0 +1,37 @@ +# Python any(iterable) + +`any()` is a built in function in Python 3, to check if any of the 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 entries are to be checked. It can typically be a `list`, `str`, `dict`, `tuple` etc., even a `file object`. + +## Return Value +The return value would be a boolean. If and only if **all** entries of iterable are `False`, or the `iterable` is empty; it returns `False`. This function essentially performs a Boolean `OR` operation over all elements. + +If even one of them is `True`, it would return `True`. + +The `any()` operation is equivalent to (internally, may not be implemented exactly like this) + +```python +def any(iterable): + for element in iterable: + if element: + return True + return False +``` + +## Code Sample + +```python +print(any([])) #=> False +print(any({})) #=> False +print(any([6, 7])) #=> True +print(any([6, 7, None])) #=> True +print(any([0, 6, 7])) #=> True +print(any([9, 8, [1, 2]])) #=> True +``` +:rocket: [REPL It!](https://repl.it/CL9c/0) + +[Documentation](https://docs.python.org/3/library/functions.html#any) From 8d058872dfba7fb1282cfcc87e2e67b65e012d5d Mon Sep 17 00:00:00 2001 From: Arijit Layek Date: Sat, 30 Apr 2016 14:08:41 +0000 Subject: [PATCH 2/3] Fix built in --- python-any.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-any.md b/python-any.md index 84a036aa69..72e63862c0 100644 --- a/python-any.md +++ b/python-any.md @@ -1,6 +1,6 @@ # Python any(iterable) -`any()` is a built in function in Python 3, to check if any of the items of an [_iterable_](https://docs.python.org/3/glossary.html#term-iterable) is `True`. It takes one argument, `iterable`. +`any()` is a built-in function in Python 3, to check if any of the items of an [_iterable_](https://docs.python.org/3/glossary.html#term-iterable) is `True`. It takes one argument, `iterable`. ## Argument ### iterable From 8dcc98267b3c8f1dda5581702067fae17561fc9f Mon Sep 17 00:00:00 2001 From: Arijit Layek Date: Sat, 30 Apr 2016 14:46:36 +0000 Subject: [PATCH 3/3] Add some fixes --- python-any.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-any.md b/python-any.md index 72e63862c0..bcd7a4e0a1 100644 --- a/python-any.md +++ b/python-any.md @@ -1,4 +1,4 @@ -# Python any(iterable) +# Python `any(iterable)` `any()` is a built-in function in Python 3, to check if any of the items of an [_iterable_](https://docs.python.org/3/glossary.html#term-iterable) is `True`. It takes one argument, `iterable`. @@ -34,4 +34,4 @@ print(any([9, 8, [1, 2]])) #=> True ``` :rocket: [REPL It!](https://repl.it/CL9c/0) -[Documentation](https://docs.python.org/3/library/functions.html#any) +[Official Docs](https://docs.python.org/3/library/functions.html#any)