Range(1,1001)=[1,...,1000] you don't wanna start with 0.
most of python tools are easy to define and use.
I had problems with lambda and yield too when I began with python but good lebgeeks helped me out.
Actually lambda is pretty easy tool in python yet so useful,
Let's say you want to reduce a list while summing its elements you would need to call reduce(function,list)
and you would need to define a function add(x,y): return x+y but this function is not so important. That's why we use lambda on the go to define less important functions inside the reduce function. ex: reduce(lambda x,y: x+y, list) instead of:
def add.... Reduce(add, list).
As for yield I also faced problems with it and got helped here :].
Let's say you want to write a function that detects even numbers from 1 to 20 and adds them to a list:
def f():
li=[]
for i in xrange(1,21): #xrange is range equivalent but it is more compact because it calls its elements only
if i%2==0: # when needed and does not store them.
li.append(i)
return li
here is an equivalent example using yield function
def f2():
for i in xrange(1,21):
if i%2==0:
yield i
Like you noticed, yield tells the function to initialize a generator object(like a list) and if the condition is fulfilled the yield var will add var to the generator and the function will return the generator after the loop ends, you don't need to tell the function to return the generator.
Generators are objects that can be transformed into lists easily to manipulate them and do things like summing, reducing... Doable to lists as follows : for this example listevennumbers=list(f2())
If you have any problem let me know i'd be more than happy to help :]