File tree Expand file tree Collapse file tree 1 file changed +19
-19
lines changed Open diff view settings
Expand file tree Collapse file tree 1 file changed +19
-19
lines changed Open diff view settings
Original file line number Diff line number Diff line change @@ -246,6 +246,25 @@ while True:
246246 print (thing)
247247```
248248
249+ ## Checking if object is iterable or not
250+
251+ There is an easy way of checking if an object in python is iterable or not. The following code will do the needful.
252+ ``` python
253+ >> > def check (A ):
254+ ... try :
255+ ... st = iter (A)
256+ ... print (' yes' )
257+ ... except TypeError :
258+ ... print (' no' )
259+ ...
260+ >> > check(25 )
261+ no
262+ >> > check([25 ,35 ])
263+ yes
264+ >> >
265+ ```
266+ 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.
267+
249268## Generators
250269
251270It's possible to create a custom iterator with a class that defines an
@@ -442,25 +461,6 @@ does the same thing as our `count()`.
442461 generator runs it to the next yield and gives us the value it yielded.
443462- [ The itertools module] ( https://docs.python.org/3/library/itertools.html )
444463 contains many useful iterator-related things.
445-
446- ## Checking if object is iterable or not
447-
448- There is an easy way of checking if an object in python is iterable or not. The following code will do the needful.
449- ``` python
450- >> > def check (A ):
451- ... try :
452- ... st = iter (A)
453- ... print (' yes' )
454- ... except :
455- ... print (' no' )
456- ...
457- >> > check(25 )
458- no
459- >> > check([25 ,35 ])
460- yes
461- >> >
462- ```
463- 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.
464464***
465465
466466If you have trouble with this tutorial please [ tell me about
You can’t perform that action at this time.
0 commit comments