Skip to content

Commit

Permalink
Merge pull request Akuli#12 from debdutgoswami/patch-1
Browse files Browse the repository at this point in the history
Update iters.md
  • Loading branch information
Akuli authored Oct 15, 2019
2 parents 011b01f + 5014853 commit 865f136
Showing 1 changed file with 19 additions and 1 deletion.
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

0 comments on commit 865f136

Please sign in to comment.