Skip to content

Update iters.md #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion advanced/iters.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,25 @@ while True:
print(thing)
```

## Checking if object is iterable or not

There is an easy way of checking if an object in python is iterable or not. The following code will do the needful.
```python
>>> def check(A):
... try:
... st = iter(A)
... print('yes')
... except TypeError:
... print('no')
...
>>> check(25)
no
>>> check([25,35])
yes
>>>
```
Here you can observe that the 25 is an integer, so it is not iterable, but [25,35] is a list which is iterable so it outputs no and yes respectively.

## Generators

It's possible to create a custom iterator with a class that defines an
Expand Down Expand Up @@ -442,7 +461,6 @@ does the same thing as our `count()`.
generator runs it to the next yield and gives us the value it yielded.
- [The itertools module](https://docs.python.org/3/library/itertools.html)
contains many useful iterator-related things.

***

If you have trouble with this tutorial please [tell me about
Expand Down