Disclaimer: This presentation is prepared
by trainees of baabtra as a part of
mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System
Technologies Pvt . Ltd
Week Targ
et
Achieve
d
1 23 19
2 25 23
3 30 27
4 35
Typing Speed
Jobs Applied
# Company Designation Applied
Date
Current
Status
1 Skillsearch
Limited
Junior software
engineer
7/08/2013
2
3
Iterators and
Generators in python
●
Shameena
●
shameenavayoli@gmail.com
●
www.facebook.com/shameenalatheef
●
twitter.com/shameena
●
in.linkedin.com/in/shamean
●
9567135569
Iterator is an object which allows a programmer to
traverse through all the elements of a collection
iterator is an object which implements the iterator
protocol
The iterator protocol consists of two methods.
1) __iter__()::return the iterator object
2) next():: returns the next element from a sequence.
●
An iterator is typically something that has
a next method to get the next element
from a stream
●
An iterator traverses a collection one at a
time.
●
●
All of the common containers
(list,tuple,dict) respond to iter() function by
returning an iterator over the item in the
container
●
str = "formidable"
for i in str:
print i,
print
it = iter(str)
print it.next()
print it.next()
print it.next()
print list(it)
Output:
f o r m i d a b l e
f
o
r
['m', 'i', 'd', 'a', 'b', 'l',
'e']
Generators
● generator is a special routine that can be
used to control the iteration behaviour of a
loop.
● a generator yields one value at a time
Every generator is an iterator but not vice versa.
A generator is built by calling a function that has one or
more yield expressions
A generator looks like a conventional function, with
one important difference: a generator includes
the yield statement
● When a stop iteration is raised it is handled by for
statement as a normal termination of the loop
●
Example:Example:
def printName(name):
for section in name.split(' '):
yield section
for section in printName("Guido van Rossum"):
print section
Output:
If this presentation helped you, please
visit our page facebook.com/baabtra and
like it.
Thanks in advance. 
 
www.baabtra.com | www.massbaab.com |
www.baabte.com
Contact Us
Emarald Mall (Big
Bazar Building)
Mavoor Road,
Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25
550
NC Complex,
Near Bus
Stand
Mukkam,
Kozhikode,
Kerala, India.
Ph: + 91 –
495 40 25
550
Start up Village
Eranakulam,
Kerala, India.
Email:
info@baabtra.com

Iterators and Generators

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
    Week Targ et Achieve d 1 2319 2 25 23 3 30 27 4 35 Typing Speed
  • 4.
    Jobs Applied # CompanyDesignation Applied Date Current Status 1 Skillsearch Limited Junior software engineer 7/08/2013 2 3
  • 5.
    Iterators and Generators inpython ● Shameena ● shameenavayoli@gmail.com ● www.facebook.com/shameenalatheef ● twitter.com/shameena ● in.linkedin.com/in/shamean ● 9567135569
  • 6.
    Iterator is anobject which allows a programmer to traverse through all the elements of a collection iterator is an object which implements the iterator protocol The iterator protocol consists of two methods. 1) __iter__()::return the iterator object 2) next():: returns the next element from a sequence.
  • 7.
    ● An iterator istypically something that has a next method to get the next element from a stream ● An iterator traverses a collection one at a time. ●
  • 8.
    ● All of thecommon containers (list,tuple,dict) respond to iter() function by returning an iterator over the item in the container ●
  • 9.
    str = "formidable" fori in str: print i, print it = iter(str) print it.next() print it.next() print it.next() print list(it) Output: f o r m i d a b l e f o r ['m', 'i', 'd', 'a', 'b', 'l', 'e']
  • 10.
    Generators ● generator isa special routine that can be used to control the iteration behaviour of a loop. ● a generator yields one value at a time Every generator is an iterator but not vice versa. A generator is built by calling a function that has one or more yield expressions
  • 11.
    A generator lookslike a conventional function, with one important difference: a generator includes the yield statement
  • 12.
    ● When astop iteration is raised it is handled by for statement as a normal termination of the loop ● Example:Example: def printName(name): for section in name.split(' '): yield section for section in printName("Guido van Rossum"): print section
  • 13.
  • 14.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.    www.baabtra.com | www.massbaab.com | www.baabte.com
  • 15.
    Contact Us Emarald Mall(Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com