September 2014
Intermediate to advanced
368 pages
10h 14m
English
When many people with experience in another language start learning Python, they are taken
aback by the difference in for loop notation. That is to say, instead of
writing:
# Other languagesfor(i=0;i<N;i++){do_work(i);}
they are instead introduced to a new function called range or xrange:
# Pythonforiinrange(N):do_work(i)
These two functions provide insight into the paradigm of programming using
generators. In order to fully understand generators, let us first make simple
implementations of the range and xrange functions:
defrange(start,stop,step=1):numbers=[]whilestart<stop:numbers.append(start)start+=stepreturnnumbersdefxrange(start,stop,step=1):whilestart<stop:yieldstart#![]()
start+=stepforiinrange(1,10000):passforiinxrange(1,10000):pass
The first thing to note is that the ...
Read now
Unlock full access