GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — New chapter about generators · python043/full-speed-python@335f921

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • chapters
    • generators.tex
    • introduction.tex
  • full-speed-python.tex
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎chapters/generators.tex‎

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
\chapter{Generators}\label{generators}
2+
3+
If you read the previous chapter, you know that iterators are objects that are regularly used with "for" loops. In other others, iterators are objects that implement the iteration protocol. A Python generator is a convenient way to implement an iterator. Instead of a class, a generator is a function which returns a value each time the "yield" keyword is used. Here's an example of a generator to count the values between two numbers:
4+
5+
\begin{lstlisting}
6+
def myrange(a, b):
7+
while a < b:
8+
yield a
9+
a += 1
10+
\end{lstlisting}
11+
12+
Like iterators, generators can be used with the "for" loop:
13+
14+
\begin{lstlisting}
15+
>>> for value in myrange(1, 4):
16+
... print(value)
17+
...
18+
1
19+
2
20+
3
21+
\end{lstlisting}
22+
23+
Under the hood, generators behave similarly to iterators:
24+
25+
\begin{lstlisting}
26+
>>> seq = myrange(1,3)
27+
>>> next(seq)
28+
1
29+
>>> next(seq)
30+
2
31+
>>> next(seq)
32+
Traceback (most recent call last):
33+
File "<stdin>", line 1, in <module>
34+
StopIteration
35+
\end{lstlisting}
36+
37+
The interesting thing about generators is the "yield" keyword. The "yield" keyword works much like the "return" keyword, but unlike "return", it allows the function to eventually resume its execution. In other words, each time the next value of a generator is needed, Python wakes up the function and resumes its execution from the "yield" line as if the function had never exited.
38+
39+
Generator functions can use other functions inside. For instance, it is very common to use the "range" function to iterate over a sequence of numbers:
40+
41+
\begin{lstlisting}
42+
def squares(n):
43+
for value in range(n):
44+
yield value * value
45+
\end{lstlisting}
46+
47+
\section{Exercises with generators}
48+
49+
\begin{enumerate}
50+
51+
\item Implement a generator called "squares" to yield the square of all numbers from $a$ to $b$. Test it with a "for" loop and print each of the yielded values.
52+
53+
\item Create a generator to yield all the even numbers from 1 to $n$.
54+
55+
\item Create another generator to yield all the odd numbers from 1 to $n$.
56+
57+
\item Implement a generator that returns all numbers from $n$ down to 0.
58+
59+
\item Create a generator to return the fibonnaci sequence starting from the first element up to $n$. The first numbers of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
60+
61+
\item Implement a generator that returns all consecutive pairs of numbers from 0 to $n$, such as (0, 1), (1, 2), (2, 3)...
62+
63+
\end{enumerate}
64+
65+
66+
Collapse file

‎chapters/introduction.tex‎

Copy file name to clipboardExpand all lines: chapters/introduction.tex
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ \chapter{Introduction}\label{introduction}
44

55
These exercises have been used extensively in my web development and distributed computing classes at the Superior School of Technology of Setúbal. With these exercises, most students are up to speed with Python in less than a month. In fact, students of the distributed computing course, taught in the second year of the software engineering degree, become familiar with Python's syntax in two weeks and are able to implement a distributed client-server application with sockets in the third week.
66

7-
This book is divided in the following chapters: in chapter \ref{installation} I will provide the basic installation instructions and execution of the Python interpreter. In chapter \ref{basic-datatypes} we will talk about the most basic data types, namely numbers and strings. In chapter \ref{functions} we will start tinkering with functions, and in chapter \ref{loops} the topic is about the for and the while "loops". In chapter \ref{dictionaries} we will work with dictionaries and in chapter \ref{classes} we will work with some exercises about classes and object oriented programming. In chapter \ref{iterators}, we will explore the topic of Python iterators.
7+
This book is divided in the following chapters: in chapter \ref{installation} I will provide the basic installation instructions and execution of the Python interpreter. In chapter \ref{basic-datatypes} we will talk about the most basic data types, namely numbers and strings. In chapter \ref{functions} we will start tinkering with functions, and in chapter \ref{loops} the topic is about the for and the while "loops". In chapter \ref{dictionaries} we will work with dictionaries and in chapter \ref{classes} we will work with some exercises about classes and object oriented programming. In chapter \ref{iterators}, we will explore the topic of Python iterators and in chapter \ref{generators} we will talk about Python generators.
88

99
Please note that this book is a work in progress and, as such, may contain a few spelling errors that may be corrected in the future. However it is made available now as it is so it can be useful to anyone who wants to use it. I sincerely hope you can get something good through it.
1010

Collapse file

‎full-speed-python.tex‎

Copy file name to clipboardExpand all lines: full-speed-python.tex
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@
6060
\include{chapters/dictionaries}
6161
\include{chapters/classes}
6262
\include{chapters/iterators}
63+
\include{chapters/generators}
6364

6465
\end{document}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.