Learn Python
Learn Python
Learn Python
Whether you are an experienced programmer or not, this website is intended for
everyone who wishes to learn the Python programming language.
You are welcome to join our group on Facebook for questions, discussions and
updates.
After you complete the tutorials, you can get certified at LearnX and add your
certification to your LinkedIn profile.
Just click on the chapter you wish to begin from, and follow the instructions. Good
luck!
Hello, World!
Variables and Types
Lists
Basic Operators
String Formatting
Basic String Operations
Conditions
Loops
Functions
Classes and Objects
Dictionaries
Modules and Packages
Numpy Arrays
Pandas Basics
Advanced Tutorials
Generators
List Comprehensions
Lambda functions
Multiple Function Arguments
Regular Expressions
Exception Handling
Sets
Serialization
Partial functions
Code Introspection
Closures
Decorators
Map, Filter, Reduce
Contributing Tutorials
Ready to take the test? Head onto LearnX and get your Python Certification!
Generators
Generators are very easy to implement, but a bit difficult to understand.
Generators are used to create iterators, but with a different approach. Generators are simple
functions which return an iterable set of items, one at a time, in a special way.
When an iteration over a set of item starts using the for statement, the generator is run. Once
the generator's function code reaches a "yield" statement, the generator yields its execution
back to the for loop, returning a new value from the set. The generator function can generate
as many values (possibly infinite) as it wants, yielding each one in its turn.
script.py
1
2
3
4
5
6
7
8
9
10
11
12
import random
def lottery():
# returns 6 numbers between 1 and 40
for i in range(6):
yield random.randint(1, 40)
# returns a 7th number between 1 and 15
yield random.randint(1, 15)
for random_number in lottery():
print("And the next number is... %d!" %
(random_number))
IPython Shell
In [1]:
Run
Powered by DataCamp
This function decides how to generate the random numbers on its own, and executes the yield
statements one at a time, pausing in between to yield execution back to the main for loop.
Exercise
Write a generator function which returns the Fibonacci series. They are calculated using the
following formula: The first two numbers of the series is always equal to 1, and each
consecutive number returned is the sum of the last two numbers. Hint: Can you use only two
variables in the generator function? Remember that assignments can be done simultaneously.
The code
script.py
1
2
3
4
a=1
b=2
a, b = b, a
print(a, b)
IPython Shell
In [1]:
Run
Powered by DataCamp
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# fill in this function
def fib():
pass #this is a null statement which does
nothing when executed, useful as a placeholder.
# testing code
import types
if type(fib()) == types.GeneratorType:
print("Good, The fib function is a generator.")
counter = 0
for n in fib():
print(n)
counter += 1
if counter == 10:
break
IPython Shell
In [1]:
SolutionRun
Powered by DataCamp