Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 9 Online
OpenStudy (anonymous):

Hey, can someone explain to me what a python generator is and how to use it?I need to know it to solve a problem.Thanks

OpenStudy (anonymous):

refer http://wiki.python.org/moin/Generators

OpenStudy (anonymous):

Here's a fibonacci generator. def fib(n): a,b = 0,1 for _ in xrange(n): yield b a,b = b, a+b And a test print fib(10) print list(fib(10)) for i in fib(10): print i, And the output <generator object fib at 0x0298F490> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 1 1 2 3 5 8 13 21 34 55

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!