diff --git a/.gitignore b/.gitignore index b001411..16195f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,5 @@ -# Latex -*.aux -*.log -*.gz -*.out -*.toc +# macOS +.DS_Store # Releases *.pdf diff --git a/Makefile b/Makefile index dacc7fb..7c023c0 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,26 @@ +chapters = \ + chapters/introduction.md \ + chapters/installation.md \ + chapters/basic-datatypes.md \ + chapters/lists.md \ + chapters/functions.md \ + chapters/loops.md \ + chapters/dictionaries.md \ + chapters/classes.md \ + chapters/iterators.md \ + chapters/generators.md \ + chapters/coroutines.md \ + chapters/async.md \ + +options = metadata.yaml --highlight-style tango --number-sections + all: epub pdf epub: - pandoc -o full-speed-python.epub \ - full-speed-python.tex \ - chapters/installation.tex \ - chapters/basic-datatypes.tex \ - chapters/functions.tex \ - chapters/loops.tex \ - chapters/dictionaries.tex \ - chapters/classes.tex \ + pandoc -o full-speed-python.epub $(options) $(chapters) pdf: - pdflatex full-speed-python.tex + pandoc -o full-speed-python.pdf -H tex/preamble.tex $(options) $(chapters) clean: rm *.epub *.pdf diff --git a/README.md b/README.md index 04b4638..73f4009 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,19 @@ -# At full speed with Python +# Full Speed Python -This book aims to teach the basics of the python programming language using a practical approach. Its method is quite basic though: after a very simple introduction to each topic, the reader is invited to learn by solving the proposed exercises. +### About -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 at full 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. +This book aims to teach the Python programming language using a practical approach. Its method is quite simple: after a short introduction to each topic, the reader is invited to learn more by solving the proposed exercises. -This book is made available in github (https://github.com/joaoventura/full-speed-python) so I appreciate any pull requests to correct misspellings or to suggest new exercises or clarification of the current content. +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. -The generated files (pdf and epub) can be found at https://github.com/joaoventura/full-speed-python/releases/. +The source of this book is available on github (https://github.com/joaoventura/full-speed-python). I welcome any pull requests to correct misspellings, suggest new exercises or to provide clarification of the current content. + +### How to get the ebook? + +Pdf and epub files can be downloaded from: https://jventura.gumroad.com/l/fullspeedpython. The ebooks are free (as in free beer!), but if you want to support my work and allow me to improve it, you can set any other price. + +## Building + +Run `make pdf` or `make epub` to build the books. + +This project uses [pandoc](http://pandoc.org/) to build the books. The pdf file is built with pdflatex, so you may need to download a latex distribution. diff --git a/chapters/async.md b/chapters/async.md new file mode 100644 index 0000000..60d738a --- /dev/null +++ b/chapters/async.md @@ -0,0 +1,117 @@ +# Asynchronous programming + +So far we have been doing *synchronous programming*. Synchronous program execution is quite simple: a program starts at the first line, then each line is executed until the program reaches the end. Each time a function is called, the program waits for the function to return before continuing to the next line. + +In asynchronous programming, the execution of a function is usually non-blocking. In other words, each time you call a function it returns immediately. However, that function does not necessarily gets executed right way. Instead, there is usually a mechanism (called the "scheduler") which is responsible for the future execution of the function. + +The problem with asynchronous programming is that a program may end before any asynchronous function starts. A common solution for this is for asynchronous functions to return "futures" or "promises". These are objects that represent the state of execution of an async function. Finally, asynchronous programming frameworks typically have mechanisms to block or wait for those async functions to end based on those "future" objects. + +Since Python 3.6, the "asyncio" module combined with the *async* and *await* keyword allows us to implement what is called *co-operative multitasking programs*. In this type of programming, a coroutine function voluntarily yields control to another coroutine function when idle or when waiting for some input. + +Consider the following asynchronous function that squares a number and sleeps for one second before returning. Asynchronous functions are declared with **async def**. Ignore the **await** keyword for now: + +```Python +import asyncio + +async def square(x): + print('Square', x) + await asyncio.sleep(1) + print('End square', x) + return x * x + +# Create event loop +loop = asyncio.get_event_loop() + +# Run async function and wait for completion +results = loop.run_until_complete(square(1)) +print(results) + +# Close the loop +loop.close() +``` + +The event loop () is, among other things, the Python mechanism that schedules the execution of asynchronous functions. We use the loop to run the function until completion. This is a synchronizing mechanism that makes sure the next print statement doesn't execute until we have some results. + +The previous example is not a good example of asynchronous programming because we don't need that much complexity to execute only one function. However, imagine that you would need to execute the `square(x)` function three times, like this: + +```python +square(1) +square(2) +square(3) +``` + +Since the `square()` function has a sleep function inside, the total execution time of this program would be 3 seconds. However, given that the computer is going to be idle for a full second each time the function is executed, why can't we start the next call while the previous is sleeping? Here's how we do it: + +```Python +# Run async function and wait for completion +results = loop.run_until_complete(asyncio.gather( + square(1), + square(2), + square(3) +)) +print(results) +``` + +Basically, we use ``asyncio.gather(*tasks)`` to inform the loop to wait for all tasks to finish. Since the coroutines will start at almost the same time, the program will run for only 1 second. Asyncio **gather()** won't necessarily run the coroutines by order although it will return an ordered list of results. + +```Python +$ python3 python_async.py +Square 2 +Square 1 +Square 3 +End square 2 +End square 1 +End square 3 +[1, 4, 9] +``` + +Sometimes results may be needed as soon as they are available. For that we can use a second coroutine that deals with each result using ``asyncio.as_completed()``: + +```Python +(...) + +async def when_done(tasks): + for res in asyncio.as_completed(tasks): + print('Result:', await res) + +loop = asyncio.get_event_loop() +loop.run_until_complete(when_done([ + square(1), + square(2), + square(3) +])) +``` + +This will print something like: + +```Python +Square 2 +Square 3 +Square 1 +End square 3 +Result: 9 +End square 1 +Result: 1 +End square 2 +Result: 4 +``` + +Finally, async coroutines can call **other async coroutine functions** with the **await** keyword: + +```Python +async def compute_square(x): + await asyncio.sleep(1) + return x * x + +async def square(x): + print('Square', x) + res = await compute_square(x) + print('End square', x) + return res +``` + +## Exercises with asyncio + +1. Implement an asynchronous coroutine function to add two variables and sleep for the duration of the sum. Use the asyncio loop to call the function with two numbers. + +2. Change the previous program to schedule the execution of two calls to the sum function. diff --git a/chapters/basic-datatypes.md b/chapters/basic-datatypes.md new file mode 100644 index 0000000..61a69d9 --- /dev/null +++ b/chapters/basic-datatypes.md @@ -0,0 +1,90 @@ +# Numbers and strings + +In this chapter we will work with the most basic datatypes, numbers and strings. Start your Python REPL and write the following: + +```Python +>>> a = 2 +>>> type(a) + +>>> b = 2.5 +>>> type(b) + +``` + +Basically, you are declaring two variables (named "a" and "b") which will hold some numbers: variable "a" is an integer number while variable "b" is a real number. We can now use our variables or any other numbers to do some calculations: + +```Python +>>> a + b +4.5 +>>> (a + b) * 2 +9.0 +>>> 2 + 2 + 4 - 2/3 +7.333333333333333 +``` + +Python also has support for string datatypes. Strings are sequences of characters (like words) and can be defined using single or double quotes: + +```Python +>>> hi = "hello" +>>> hi +'hello' +>>> bye = 'goodbye' +>>> bye +'goodbye' +``` + +You can add strings to concatenate them but you can not mix different datatypes, such as strings and integers. + +```Python +>>> hi + "world" +'helloworld' +>>> "Hello" + 3 +Traceback (most recent call last): + File "", line 1, in +TypeError: must be str, not int +``` + +However, multiplication works as repetition: + +```Python +>>> "Hello" * 3 +'HelloHelloHello' +``` + +## Exercises with numbers + +1. Try the following mathematical calculations and guess what is happening: \((3 / 2)\), \((3 // 2)\), \((3 \% 2)\), \((3**2)\). + + Suggestion: check the Python library reference at . + +2. Calculate the average of the following sequences of numbers: (2, 4), (4, 8, 9), (12, 14/6, 15) + +3. The volume of a sphere is given by (4/3 * pi * r^3). Calculate the volume of a sphere of radius 5. Suggestion: create a variable named "pi" with the value of 3.1415. + +4. Use the modulo operator (%) to check which of the following numbers is even or odd: (1, 5, 20, 60/7). + + Suggestion: the remainder of \(x/2\) is always zero when \(x\) is even. + +5. Find some values for \(x\) and \(y\) such that \(x < 1/3 < y\) returns "True" on the Python REPL. Suggestion: try \(0 < 1/3 < 1\) on the REPL. + +## Exercises with strings + +Using the Python documentation on strings (), solve the following exercises: + +1. Initialize the string "abc" on a variable named "s": + + 1. Use a function to get the length of the string. + + 2. Write the necessary sequence of operations to transform the string "abc" in "aaabbbccc". Suggestion: Use string concatenation and string indexes. + +2. Initialize the string "aaabbbccc" on a variable named "s": + + 1. Use a function that allows you to find the first occurence of "b" in the string, and the first occurence of "ccc". + + 2. Use a function that allows you to replace all occurences of "a" to "X", and then use the same function to change only the first occurence of "a" to "X". + +3. Starting from the string "aaa bbb ccc", what sequences of operations do you need to arrive at the following strings? You can use the "replace" function. + + 1. "AAA BBB CCC" + + 2. "AAA bbb CCC" diff --git a/chapters/basic-datatypes.tex b/chapters/basic-datatypes.tex deleted file mode 100644 index 5428d73..0000000 --- a/chapters/basic-datatypes.tex +++ /dev/null @@ -1,146 +0,0 @@ -\chapter{Basic datatypes}\label{basic-datatypes} - -In this chapter we will work with the most basic datatypes, numbers, strings and lists. Start your Python REPL and write the following on it: - -\begin{lstlisting} ->>> a = 2 ->>> type(a) - ->>> b = 2.5 ->>> type(b) - -\end{lstlisting} - -Basically, you are declaring two variables (named "a" and "b") which will hold some numbers: variable "a" is an integer number while variable "b" is a real number. We can now use our variables or any other numbers to do some calculations: - -\begin{lstlisting} ->>> a + b -4.5 ->>> (a + b) * 2 -9.0 ->>> 2 + 2 + 4 - 2/3 -7.333333333333333 -\end{lstlisting} - -Python also has support for string datatypes. Strings are sequences of characters (like words) and can be defined using single or double quotes: - -\begin{lstlisting} ->>> hi = "hello" ->>> hi -'hello' ->>> bye = 'goodbye' ->>> bye -'goodbye' -\end{lstlisting} - -You can add strings to concatenate them but you can not mix different datatypes, such as strings and integers. - -\begin{lstlisting} ->>> hi + "world" -'helloworld' ->>> "Hello" + 3 -Traceback (most recent call last): - File "", line 1, in -TypeError: must be str, not int -\end{lstlisting} - -However, multiplication seems to work as repetition: - -\begin{lstlisting} ->>> "Hello" * 3 -'HelloHelloHello' -\end{lstlisting} - -Finally, Python also supports the list datatype. Lists are data structures that allows us to group some values. Lists can have values of several types and you can also mix different types within the same list although usually all values are usually of the same datatype. - -Lists are created by starting and ending with square brackets and separated by commas. The values in a list can be accessed by its position where 0 is the index of the first value: - -\begin{lstlisting} ->>> l = [1, 2, 3, 4, 5] ->>> l[0] -1 ->>> l[1] -2 -\end{lstlisting} - -Can you access the number 4 in the previous list? - -Sometimes you want just a small portion of a list, a sublist. Sublists can be retrieved using a technique called \textit{slicing}, which consists on using the start and end indexes on the sublist: - -\begin{lstlisting} ->>> l = ['a', 'b', 'c', 'd', 'e'] ->>> l[1:3] -['b', 'c'] -\end{lstlisting} - -Finally, there's also some arithmetic that you can do on lists, like adding two lists together or repeating the contents of a list. - -\begin{lstlisting} ->>> [1,2] + [3,4] -[1, 2, 3, 4] ->>> [1,2] * 2 -[1, 2, 1, 2] -\end{lstlisting} - - -\section{Exercises with numbers} - -\begin{enumerate} - -\item Try the following mathematical calculations and guess what is happening: $(3 / 2)$, $(3 // 2)$, $(3 \% 2)$, $(3**2)$. - -Suggestion: check the Python library reference at \url{https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex}. - -\item Calculate the average of the following sequences of numbers: (2, 4), (4, 8, 9), (12, 14/6, 15) - -\item The volume of a sphere is given by $4/3\pi r^3$. Calculate the volume of a sphere of radius 5. Suggestion: create a variable named "pi" with the value of 3.1415. - -\item Use the module operator (\%) to check which of the following numbers is even or odd: (1, 5, 20, 60/7). - -Suggestion: the remainder of $x/2$ is always zero when $x$ is even. - -\item Find some values for $x$ and $y$ such that $x < 1/3 < y$ returns "True" on the Python REPL. Suggestion: try $0 < 1/3 < 1$ on the REPL. - -\end{enumerate} - -\section{Exercises with strings} - -Using the Python documentation on strings (\url{https://docs.python.org/3/library/string.html}), solve the following exercises: - -\begin{enumerate} - -\item Initialize the string "abc" on a variable named "s": -\begin{enumerate} -\item Use a function to get the length of the string. -\item Write the necessary sequence of operations to transform the string "abc" in "aaabbbccc". Suggestion: Use string concatenation and string indexes. -\end{enumerate} - -\item Initialize the string "aaabbbccc" on a variable named "s": -\begin{enumerate} -\item Use a function that allows you to find the first occurence of "b" in the string, and the first occurence of "ccc". -\item Use a function that allows you to replace all occurences of "a" to "X", and then use the same function to change only the first occurence of "a" to "X". -\end{enumerate} - -\item Starting from the string "aaa bbb ccc", what sequences of operations do you need to arrive at the following strings? You can find the "replace" function. -\begin{enumerate} -\item "AAA BBB CCC" -\item "AAA bbb CCC" -\end{enumerate} - -\end{enumerate} - -\section{Exercises with lists} - -Create a list named "l" with the following values ([1, 4, 9, 10, 23]). Using the Python documentation about lists (\url{https://docs.python.org/3.5/tutorial/introduction.html#lists}) solve the following exercises: - -\begin{enumerate} - -\item Using list slicing get the sublists [4, 9] and [10, 23]. - -\item Append the value 90 to the end of the list "l". Check the difference between list concatenation and the "append" method. - -\item Calculate the average value of all values on the list. You can use the "sum" and "len" functions. - -\item Remove the sublist [4, 9]. - -\end{enumerate} diff --git a/chapters/classes.md b/chapters/classes.md new file mode 100644 index 0000000..2cffc7b --- /dev/null +++ b/chapters/classes.md @@ -0,0 +1,73 @@ +# Classes + +In object oriented programming (OOP), a class is a structure that allows to group together a set of properties (called attributes) and functions (called methods) to manipulate those properties. Take the following class that defines a person with properties "name" and "age" and the "greet" method. + +```Python +class Person: + + def __init__(self, name, age): + self.name = name + self.age = age + + def greet(self): + print("Hello, my name is %s!" % self.name) +``` + +Most classes will need the constructor method ("\_\_init\_\_") to initialize the class’s attributes. In the previous case the constructor of the class receives the person’s name and age and stores that information in the class’s instance (referenced by the *self* keyword). Finally, "greet" method prints the name of the person as stored in a specific class instance (object). + +Class instances are used through the instantiation of objects. Here’s how we can instantiate two objects: + +```Python +>>> a = Person("Peter", 20) +>>> b = Person("Anna", 19) + +>>> a.greet() +Hello, my name is Peter! +>>> b.greet() +Hello, my name is Anna! + +>>> print(a.age) # We can also access the attributes of an object +20 +``` + +## Exercises with classes + +Use the Python documentation on classes at to solve the following exercises. + +1. Implement a class named "Rectangle" to store the coordinates of a rectangle given the top-left corner (x1, y1) and the bottom-right corner (x2, y2). + +2. Implement the class constructor with the parameters (x1, y1, x2, y2) and store them in the class instance using the "self" keyword. + +3. Implement the "width()" and "height()" methods which return, respectively, the width and height of a rectangle. Create two objects, instances of "Rectangle" to test the calculations. + +4. Implement the method "area" to return the area of the rectangle (width\*height). + +5. Implement the method "circumference" to return the perimeter of the rectangle (2\*width + 2\*height). + +6. Do a print of one of the objects created to test the class. Implement the "\_\_str\_\_" method such that when you print one of the objects it print the coordinates as (x1, y1)(x2, y2). + +## Class inheritance + +In object oriented programming, inheritance is one of the forms in which a subclass can inherit the attributes and methods of another class, allowing it to rewrite some of the super class’s functionalities. For instance, from the "Person" class above we could create a subclass to keep people with 10 years of age: + +```Python +class TenYearOldPerson(Person): + + def __init__(self, name): + super().__init__(name, 10) + + def greet(self): + print("I don't talk to strangers!!") +``` + +The indication that the "TenYearOldPerson" class is a subclass of "Person" is given on the first line. Then, we rewrote the constructor of the subclass to only receive the name of the person, but we will eventually call the super class’s constructor with the name of the 10-year-old and the age hardcoded as 10. Finally we reimplemented the "greet" method. + +## Exercises with inheritance + +Use the "Rectangle" class as implemented above for the following exercises: + +1. Create a "Square" class as subclass of "Rectangle". + +2. Implement the "Square" constructor. The constructor should have only the x1, y1 coordinates and the size of the square. Notice which arguments you’ll have to use when you invoke the "Rectangle" constructor when you use "super". + +3. Instantiate two objects of "Square", invoke the area method and print the objects. Make sure that all calculations are returning correct numbers and that the coordinates of the squares are consistent with the size of the square used as argument. diff --git a/chapters/classes.tex b/chapters/classes.tex deleted file mode 100644 index cff7c1f..0000000 --- a/chapters/classes.tex +++ /dev/null @@ -1,81 +0,0 @@ -\chapter{Classes}\label{classes} - -In object oriented programming (OOP), a class is a structure that allows to group together a set of properties (called attributes) and functions (called methods) to manipulate those properties. Take the following class that defines a person with properties "name" and "age" and the "greet" method. - -\begin{lstlisting} -class Person: - - def __init__(self, name, age): - self.name = name - self.age = age - - def greet(self): - print("Hello, my name is %s!" % self.name) -\end{lstlisting} - -Most classes will need the constructor method ("\_\_init\_\_") to initialize the class's attributes. In the previous case the constructor of the class receives the person's name and age and stores that information in the class's instance (referenced by the \textit{self} keyword). Finally, "greet" method prints the name of the person as stored in a specific class instance (object). - -Class instances are used through the instantiation of objects. Here's how we can instantiate two objects: - -\begin{lstlisting} ->>> a = Person("Peter", 20) ->>> b = Person("Anna", 19) - ->>> a.greet() -Hello, my name is Peter! ->>> b.greet() -Hello, my name is Anna! - ->>> print(a.age) # We can also access the attributes of an object -20 -\end{lstlisting} - -\section{Exercises with classes} - -Use the Python documentation on classes at \url{https://docs.python.org/3/tutorial/classes.html} to solve the following exercises. - -\begin{enumerate} - -\item Implement a class named "Rectangle" to store the coordinates of a rectangle given by (x1, y1) and (x2, y2). - -\item Implement the class constructor with the parameters (x1, y1, x2, y2) and store them in the class instances using the "self" keyword. - -\item Implement the "width()" and "height()" methods which return, respectively, the width and height of a rectangle. Create two objects, instances of "Rectangle" to test the calculations. - -\item Implement the method "area" to return the area of the rectangle (width*height). - -\item Implement the method "circumference" to return the perimeter of the rectangle (2*width + 2*height). - -\item Do a print of one of the objects created to test the class. Implement the "\_\_str\_\_" method such that when you print one of the objects it print the coordinates as (x1, y1)(x2, y2). - -\end{enumerate} - -\section{Class inheritance} - -In object oriented programming, inheritance is one of the forms in which a subclass can inherit the attributes and methods of another class, allowing it to rewrite some of the super class's functionalities. For instance, from the "Person" class above we could create a subclass to keep people with 10 years of age: - -\begin{lstlisting} -class TenYearOldPerson(Person): - - def __init__(self, name): - super().__init__(name, 10) - - def greet(self): - print("I don't talk to strangers!!") -\end{lstlisting} - -The indication that the "TenYearOldPerson" class is a subclass of "Person" is given on the first line. Then, we rewrote the constructor of the subclass to only receive the name of the person, but we will eventually call the super class's constructor with the name of the 10-year-old and the age hardcoded as 10. Finally we reimplemented the "greet" method. - -\section{Exercises with inheritance} - -Use the "Rectangle" class as implemented above for the following exercises: - -\begin{enumerate} - -\item Create a "Square" class as subclass of "Rectangle". - -\item Implement the "Square" constructor. The constructor should have only the x1, y1 coordinates and the size of the square. Notice which arguments you'll have to use when you invoce the "Rectangle" constructor when you use "super". - -\item Instantiate two objects of "Square", invoke the area method and print the objects. Make sure that all calculations are returning correct numbers and that the coordinates of the squares are consistent with the size of the square used as argument. - -\end{enumerate} diff --git a/chapters/coroutines.md b/chapters/coroutines.md new file mode 100644 index 0000000..e7382ad --- /dev/null +++ b/chapters/coroutines.md @@ -0,0 +1,144 @@ +# Coroutines + +Python coroutines are similar to generators but instead of producing data, coroutines are mostly used as data consumers. In other words, coroutines are functions that are resumed everytime a value is sent using the `send` method. + +The trick with coroutines is the use of the `yield` keyword on the right side of an assignment expression. Here's an example of a coroutine that just prints the values that are sent to it: + +```Python +def coroutine(): + print('My coroutine') + while True: + val = yield + print('Got', val) + +>>> co = coroutine() +>>> next(co) +My coroutine +>>> co.send(1) +Got 1 +>>> co.send(2) +Got 2 +>>> co.send(3) +Got 3 +``` + +The initial call to `next` is required to move the coroutine forward. You can see that it executes the print statement. Eventually, the function reaches the `yield` expression where it will wait to be resumed. Then, everytime a value is sent (with `send`), the coroutine function resumes from the `yield`, copies the value to **val** and prints it. + +Coroutines can be closed with the `close()` method. + +```Python +>>> co.close() +>>> co.send(4) +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` + +## Exercises with coroutines + +1. Create a coroutine named "square" that prints the square of any sent value. + +2. Implement the "minimize" coroutine that keeps and prints the minimum value that is sent to the function. + +## Pipelines + +Coroutines can be used to implement data pipelines where one coroutine will send data to the next coroutine in the pipeline. Coroutines push data into the pipeline using the `send()` method. + +![](images/coroutine_pipeline.png) + +Here's an example of a small pipeline where the values sent to the producer coroutine are squared and sent to the consumer coroutine for printing: + +```Python +def producer(consumer): + print("Producer ready") + while True: + val = yield + consumer.send(val * val) + +def consumer(): + print("Consumer ready") + while True: + val = yield + print('Consumer got', val) +``` + +As above, coroutines must be "primed" with `next` before any value can be sent. + +```Python +>>> cons = consumer() +>>> prod = producer(cons) +>>> next(prod) +Producer ready +>>> next(cons) +Consumer ready + +>>> prod.send(1) +Consumer got 1 +>>> prod.send(2) +Consumer got 4 +>>> prod.send(3) +Consumer got 9 +``` + +Also, with coroutines, data can be sent to multiple destinations.The following example implements two consumers where the first only prints numbers in 0..10 and the second only print numbers in 10..20: + +```Python +def producer(consumers): + print("Producer ready") + try: + while True: + val = yield + for consumer in consumers: + consumer.send(val * val) + except GeneratorExit: + for consumer in consumers: + consumer.close() + +def consumer(name, low, high): + print("%s ready" % name) + try: + while True: + val = yield + if low < val < high: + print('%s got' % name, val) + except GeneratorExit: + print("%s closed" % name) +``` + +As before, coroutines must be "primed" before any value can be sent. + +```Python +>>> con1 = consumer('Consumer 1', 00, 10) +>>> con2 = consumer('Consumer 2', 10, 20) +>>> prod = producer([con1, con2]) + +>>> next(prod) +Producer ready +>>> next(con1) +Consumer 1 ready +>>> next(con2) +Consumer 2 ready + +>>> prod.send(1) +Consumer 1 got 1 +>>> prod.send(2) +Consumer 1 got 4 +>>> prod.send(3) +Consumer 1 got 9 +>>> prod.send(4) +Consumer 2 got 16 + +>>> prod.close() +Consumer 1 closed +Consumer 2 closed +``` + +The data is sent to all consumers but only the second executes the print statement. Notice the use of the `GeneratorExit` exception. Sometimes it can be useful to catch the exception and inform the downstream coroutines that the pipeline is no longer useful. + +![](images/consumers_pipeline.png) + +## Exercises with coroutine pipelines + +1. Implement a producer-consumer pipeline where the values squared by the producer are sent to two consumers. One should store and print the minimum value sent so far and the other the maximum value. + +2. Implement a producer-consumer pipeline where the values squared by the producer are dispatched to two consumers, one at a time. The first value should be sent to consumer 1, the second value to consumer 2, third value to consumer 1 again, and so on. Closing the producer should force the consumers to print a list with the numbers that each one obtained. diff --git a/chapters/dictionaries.md b/chapters/dictionaries.md new file mode 100644 index 0000000..1738237 --- /dev/null +++ b/chapters/dictionaries.md @@ -0,0 +1,106 @@ +# Dictionaries + +In this chapter we will work with Python dictionaries. Dictionaries are data structures that indexes values by a given key (key-value pairs). The following example shows a dictionary that indexes students ages by name. + +```Python +ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, + "Thomas": 10, + "Bob": 10, + "Joseph": 11, + "Maria": 12, + "Gabriel": 10, +} + +>>> print(ages["Peter"]) +10 +``` + +It is possible to iterate over the contents of a dictionary using "items", like this: + +```Python +>>> for name, age in ages.items(): +... print(name, age) +... +Peter 10 +Isabel 11 +Anna 9 +Thomas 10 +Bob 10 +Joseph 11 +Maria 12 +Gabriel 10 +``` + +However, dictionary keys don’t necessarily need to be strings but can be any [immutable](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) object: + +```Python +d = { + 0: [0, 0, 0], + 1: [1, 1, 1], + 2: [2, 2, 2], +} + +>>> d[2] +[2, 2, 2] +``` + +And you can also use other dictionaries as values: + +```Python +students = { + "Peter": {"age": 10, "address": "Lisbon"}, + "Isabel": {"age": 11, "address": "Sesimbra"}, + "Anna": {"age": 9, "address": "Lisbon"}, +} + +>>> students['Peter'] +{'age': 10, 'address': 'Lisbon'} +>>> students['Peter']['address'] +'Lisbon' +``` + +This is quite useful to structure hierarchical information. + +## Exercises with dictionaries + +Use the Python documentation at to solve the following exercises. + +Take the following Python dictionary: + + ages = { + "Peter": 10, + "Isabel": 11, + "Anna": 9, + "Thomas": 10, + "Bob": 10, + "Joseph": 11, + "Maria": 12, + "Gabriel": 10, + } + +1. How many students are in the dictionary? Search for the "len" function. + +2. Implement a function that receives the "ages" dictionary as parameter and returns the average age of the students. Traverse all items on the dictionary using the "items" method as above. + +3. Implement a function that receives the "ages" dictionary as parameter and returns the name of the oldest student. + +4. Implement a function that receives the "ages" dictionary and a number "n" and returns a new dict where each student is \(n\) years older. For instance, *new_ages(ages, 10)* returns a copy of "ages" where each student is 10 years older. + +## Exercises with sub-dictionaries + +Take the following dictionary: + + students = { + "Peter": {"age": 10, "address": "Lisbon"}, + "Isabel": {"age": 11, "address": "Sesimbra"}, + "Anna": {"age": 9, "address": "Lisbon"}, + } + +1. How many students are in the "students" dict? Use the appropriate function. + +2. Implement a function that receives the students dict and returns the average age. + +3. Implement a function that receives the students dict and an address, and returns a list with names of all students whose address matches the address in the argument. For instance, invoking "find_students(students, ’Lisbon’)" should return Peter and Anna. diff --git a/chapters/dictionaries.tex b/chapters/dictionaries.tex deleted file mode 100644 index b65b74b..0000000 --- a/chapters/dictionaries.tex +++ /dev/null @@ -1,118 +0,0 @@ -\chapter{Dictionaries}\label{dictionaries} - -In this chapter we will work with Python dictionaries. Dictionaries are data structures that indexes values by a given key (key-value pairs). The following example shows a dictionary that indexes students ages by name. - -\begin{lstlisting} -ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, - "Thomas": 10, - "Bob": 10, - "Joseph": 11, - "Maria": 12, - "Gabriel": 10, -} - ->>> print(ages["Peter"]) -10 -\end{lstlisting} - -It is possible to iterate over the contents of a dictionary using "items", like this: - -\begin{lstlisting} ->>> for name, age in ages.items(): -... print(name, age) -... -Peter 10 -Isabel 11 -Anna 9 -Thomas 10 -Bob 10 -Joseph 11 -Maria 12 -Gabriel 10 -\end{lstlisting} - -However, keys don't need to be necessarily strings and integers but can be any objects: - -\begin{lstlisting} -d = { - 0: [0, 0, 0], - 1: [1, 1, 1], - 2: [2, 2, 2], -} - ->>> d[2] -[2, 2, 2] -\end{lstlisting} - -Even more, you can use other dictionaries as values: - -\begin{lstlisting} -students = { - "Peter": {"age": 10, "address": "Lisbon"}, - "Isabel": {"age": 11, "address": "Sesimbra"}, - "Anna": {"age": 9, "address": "Lisbon"}, -} - ->>> students['Peter'] -{'age': 10, 'address': 'Lisbon'} ->>> students['Peter']['address'] -'Lisbon' -\end{lstlisting} - -This is quite useful to structure hierarchical information. - -\section{Exercises with dictionaries} - -Use the Python documentation at \url{https://docs.python.org/3/library/stdtypes.html#mapping-types-dict} to solve the following exercises. - -Take the following Python dictionary: - -\begin{verbatim} -ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, - "Thomas": 10, - "Bob": 10, - "Joseph": 11, - "Maria": 12, - "Gabriel": 10, -} -\end{verbatim} - -\begin{enumerate} - -\item How many students are in the dictionary? Search for the "len" function. - -\item Implement a function that receives the "ages" dictionary as parameter and return the average age of the students. Traverse all items on the dictionary using the "items" method as above. - -\item Implement a function that receives the "ages" dictionary as parameter and returns the name of the oldest student. - -\item Implement a function that receives the "ages" dictionary and a number "n" and returns a new dict where each student is $n$ years older. For instance, \textit{new\_ages(ages, 10)} returns a copy of "ages" where each student is 10 years older. - -\end{enumerate} - -\section{Exercises with sub-dictionaries} - -Take the following dictionary: - -\begin{verbatim} -students = { - "Peter": {"age": 10, "address": "Lisbon"}, - "Isabel": {"age": 11, "address": "Sesimbra"}, - "Anna": {"age": 9, "address": "Lisbon"}, -} -\end{verbatim} - -\begin{enumerate} - -\item How many students are in the "students" dict? Use the appropriate function. - -\item Implement a function that receives the students dict and returns the average age. - -\item Implement a function that receives the students dict and an address, and returns a list with the name of all students which address matches the address in the argument. For instance, invoking "find\_students(students, 'Lisbon')" should return Peter and Anna. - -\end{enumerate} diff --git a/chapters/functions.md b/chapters/functions.md new file mode 100644 index 0000000..f518191 --- /dev/null +++ b/chapters/functions.md @@ -0,0 +1,113 @@ +# Modules and functions + +In this chapter we will talk about modules and functions. A function is a block of code that is used to perform a single action. A module is a Python file containing variables, functions and many more things. + +Start up your Python REPL and let’s use the "math" module which provides access to mathematical functions: + +```Python +>>> import math +>>> math.cos(0.0) +1.0 +>>> math.radians(275) +4.799655442984406 +``` + +Functions are sequences of instructions that are executed when the function is invoked. The following defines the "do\_hello" function that prints two messages when invoked: + +```Python +>>> def do_hello(): +... print("Hello") +... print("World") +... +>>> do_hello() +Hello +World +``` + +Make sure that you insert a tab before both print expressions in the previous function. Tabs and spaces in Python are relevant and define that a block of code is somewhat dependent on a previous instruction. For instance, the print expressions are "inside" the "do\_hello" function therefore must have a tab. + +Functions can also receive parameters and return values (using the "return" keyword): + +```Python +>>> def add_one(val): +... print("Function got value", val) +... return val + 1 +... +>>> value = add_one(1) +Function got value 1 +>>> value +2 +``` + +## Exercises with the math module + +Use the Python documentation about the math module () to solve the following exercises: + +1. Find the greatest common divisor of the following pairs of numbers: (15, 21), (152, 200), (1988, 9765). + +2. Compute the base-2 logarithm of the following numbers: 0, 1, 2, 6, 9, 15. + +3. Use the "input" function to ask the user for a number and show the result of the sine, cosine and tangent of the number. Make sure that you convert the user input from string to a number (use the int() or the float() function). + +## Exercises with functions + +1. Implement the "add2" function that receives two numbers as arguments and returns the sum of the numbers. Then implement the "add3" function that receives and sums 3 parameters. + +2. Implement a function that returns the greatest of two numbers given as parameters. Use the "if" statement to compare both numbers: . + +3. Implement a function named "is\_divisible" that receives two parameters (named "a" and "b") and returns true if "a" can be divided by "b" or false otherwise. A number is divisible by another when the remainder of the division is zero. Use the modulo operator ("%"). + +4. Create a function named "average" that computes the average value of a list passed as parameter to the function. Use the "sum" and "len" functions. + +## Recursive functions + +In computer programming, a recursive function is simply a function that calls itself. For instance take the factorial function. + +\begin{equation} +f(x)=\begin{cases} + 1, & \text{if $x=0$}.\\ + x \times f(x-1), & \text{otherwise}. +\end{cases} +\end{equation} + +As an example, take the factorial of 5: + +\begin{equation} +\begin{split} +5! &= 5 \times 4! \\ + &= 5 \times 4 \times 3! \\ + &= 5 \times 4 \times 3 \times 2! \\ + &= 5 \times 4 \times 3 \times 2 \times 1 \\ + &= 120 +\end{split} +\end{equation} + +Basically, the factorial of 5 is 5 times the factorial of 4, etc. Finally, the factorial of 1 (or of zero) is 1 which breaks the recursion. In Python we could write the following recursive function: + +```Python +def factorial(x): + if x == 0: + return 1 + else: + return x * factorial(x-1) +``` + +The trick with recursive functions is that there must be a "base" case where the recursion must end and a recursive case that iterates towards the base case. In the case of factorial we know that the factorial of zero is one, and the factorial of a number greater that zero will depend on the factorial of the previous number until it reaches zero. + +## Exercises with recursive functions + +1. Implement the factorial function and test it with several different values. Cross-check with a calculator. + +2. Implement a recursive function to compute the sum of the \(n\) first integer numbers (where \(n\) is a function parameter). Start by thinking about the base case (the sum of the first 0 integers is?) and then think about the recursive case. + +3. The Fibonnaci sequence is a sequence of numbers in which each number of the sequence matches the sum of the previous two terms. Given the following recursive definition implement \(fib(n)\). + + \begin{equation} + fib(n)=\begin{cases} + 0, & \text{if $n=0$}.\\ + 1, & \text{if $n=1$}.\\ + fib(n-1) + fib(n-2), & \text{otherwise}. + \end{cases} + \end{equation} + + Check your results for the first numbers of the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... diff --git a/chapters/functions.tex b/chapters/functions.tex deleted file mode 100644 index 0d1f9a2..0000000 --- a/chapters/functions.tex +++ /dev/null @@ -1,125 +0,0 @@ -\chapter{Modules and functions}\label{functions} - -In this chapter we will talk about modules and functions. A function is a block of code that is used to perform a single action. A module is a Python file containing variables, functions and many more things. - -Start up your Python REPL and let's use the "math" module which provides access to mathematical functions: - -\begin{lstlisting} ->>> import math ->>> math.cos(0.0) -1.0 ->>> math.radians(275) -4.799655442984406 -\end{lstlisting} - -Functions are sequences of instructions that are executed when the function is invoked. The following defines the "do\_hello" function that prints two messages when invoked: - -\begin{lstlisting} ->>> def do_hello(): -... print("Hello") -... print("World") -... ->>> do_hello() -Hello -World -\end{lstlisting} - -Make sure that you insert a tab before both print expressions in the previous function. Tabs and spaces in Python are relevant and define that a block of code is somewhat dependent on a previous instruction. For instance, the print expressions are "inside" the "do\_hello" function therefore must have a tab. - -Functions can also receive parameters a return values (using the "return" keyword): - -\begin{lstlisting} ->>> def add_one(val): -... print("Function got value", val) -... return val + 1 -... ->>> value = add_one(1) -Function got value 1 ->>> value -2 -\end{lstlisting} - -\section{Exercises with the math module} - -Use the Python documentation about the math module (\url{https://docs.python.org/3/library/math.html}) to solve the following exercises: - -\begin{enumerate} - -\item Find the greatest common divisor of the following pairs of numbers: (15, 21), (152, 200), (1988, 9765). - -\item Compute the base-2 logarithm of the following numbers: 0, 1, 2, 6, 9, 15. - -\item Use the "input" function to ask the user for a number and show the result of the sine, cosine and tangent of the number. Make sure that you convert the user input from string to a number (use the int() or the float() function). - -\end{enumerate} - -\section{Exercises with functions} - -\begin{enumerate} - -\item Implement the "add2" function that receives two numbers as arguments and returns the sum of the numbers. Then implement the "add3" function that receives and sums 3 parameters. - -\item Implement a function that returns the greatest of two numbers given as parameters. Use the "if" statement to compare both numbers: \url{https://docs.python.org/3/tutorial/controlflow.html#if-statements}. - -\item Implement a function named "is\_divisable" that receives two parameters (named "a" and "b") and returns true if "a" can be divided by "b" or false otherwise. A number is divisable by another when the remainder of the division is zero. Use the modulo operator ("\%"). - -\item Create a function named "average" that computes the average value of a list passed as parameter to the function. Use the "sum" and "len" functions. - -\end{enumerate} - -\section{Recursive functions} - -In computer programming, a recursive function is simply a function that calls itself. For instance take the factorial function. - -\begin{equation} -f(x)=\begin{cases} - 1, & \text{if $x=0$}.\\ - x \times f(x-1), & \text{otherwise}. -\end{cases} -\end{equation} - -As an example, take the factorial of 5: - -\begin{equation} -\begin{split} -5! &= 5 \times 4! \\ - &= 5 \times 4 \times 3! \\ - &= 5 \times 4 \times 3 \times 2! \\ - &= 5 \times 4 \times 3 \times 2 \times 1 \\ - &= 120 -\end{split} -\end{equation} - -Basically, the factorial of 5 is 5 times the factorial of 4, etc. Finally, the factorial of 1 (or of zero) is 1 which breaks the recursion. In Python we could write the following recursive function: - -\begin{lstlisting} -def factorial(x): - if x == 0: - return 1 - else: - return x * factorial(x-1) -\end{lstlisting} - -The trick with recursive functions is that there must be a "base" case where the recursion must end and a recursive case that iterates towards the base case. In the case of factorial we know that the factorial of zero is one, and the factorial of a number greater that zero will depend on the factorial of the previous number until it reaches zero. - -\section{Exercises with recursive functions} - -\begin{enumerate} - -\item Implement the factorial function and test it with several different values. Cross-check with a calculator. - -\item Implement a recursive function to compute the sum of the $n$ first integer numbers (where $n$ is a function parameter). Start by thinking about the base case (the sum of the first 0 integers is?) and then think about the recursive case. - -\item The Fibonnaci sequence is a sequence of numbers in which each number of the sequence matches the sum of the previous two terms. Given the following recursive definition implement $fib(n)$. - -\begin{equation} -fib(n)=\begin{cases} - 0, & \text{if $x=0$}.\\ - 1, & \text{if $x=1$}.\\ - fib(n-1) + fib(n-2), & \text{otherwise}. -\end{cases} -\end{equation} - -Check your results for the first numbers of the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... - -\end{enumerate} diff --git a/chapters/generators.md b/chapters/generators.md new file mode 100644 index 0000000..da266c3 --- /dev/null +++ b/chapters/generators.md @@ -0,0 +1,59 @@ +# Generators + +If you read the previous chapter, you know that iterators are objects that are regularly used with "for" loops. In other words, 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: + +```Python +def myrange(a, b): + while a < b: + yield a + a += 1 +``` + +Like iterators, generators can be used with the "for" loop: + +```Python +>>> for value in myrange(1, 4): +... print(value) +... +1 +2 +3 +``` + +Under the hood, generators behave similarly to iterators: + +```Python +>>> seq = myrange(1,3) +>>> next(seq) +1 +>>> next(seq) +2 +>>> next(seq) +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` + +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. + +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: + +```Python +def squares(n): + for value in range(n): + yield value * value +``` + +## Exercises with generators + +1. 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. + +2. Create a generator to yield all the even numbers from 1 to \(n\). + +3. Create another generator to yield all the odd numbers from 1 to \(n\). + +4. Implement a generator that returns all numbers from \(n\) down to 0. + +5. 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, ... + +6. Implement a generator that returns all consecutive pairs of numbers from 0 to \(n\), such as (0, 1), (1, 2), (2, 3)... diff --git a/chapters/installation.md b/chapters/installation.md new file mode 100644 index 0000000..7d58003 --- /dev/null +++ b/chapters/installation.md @@ -0,0 +1,41 @@ +# Installation + +In this chapter we will install and run the Python interpreter in your local computer. + +## Installing on Windows + +1. Download the latest Python 3 release for Windows on and execute the installer. At the time of writing, this is Python 3.6.4. + +2. Make sure that the "Install launcher for all users" and "Add Python to PATH" settings are selected and choose "Customize installation". + + ![Windows installation](images/python_windows.jpg){width=62%} + +3. In the next screen "Optional Features", you can install everything, but it is essential to install "pip" and "pylauncher (for all users)". Pip is the Python package manager that allows you to install several Python packages and libraries. + +4. In the Advanced Options, make sure that you select "Add Python to environment variables". Also, I suggest that you change the install location to something like C:\\Python36\\ as it will be easier for you to find the Python installation if something goes wrong. + + ![Windows installation](images/python_windows2.jpg){width=62%} + +5. Finally, allow Python to use more than 260 characters on the file system by selecting "Disable path length limit" and close the installation dialog. + + ![Windows installation](images/python_windows3.jpg){width=62%} + +6. Now, open the command line (cmd) and execute "python" or "python3". If everything was correctly installed, you should see the Python REPL. The REPL (from Read, Evaluate, Print and Loop) is a environment that you can use to program small snippets of Python code. Run *exit()* to leave the REPL. + + ![Python REPL](images/python_windows4.jpg){width=62%} + +## Installing on macOS + +You can download the latest macOS binary releases from . Make sure you download the latest Python 3 release (3.6.4 at the time of writing). You can also use Homebrew, a package manager for macOS (). To install the latest Python 3 release with Homebrew, just do "`brew install python3`" on your terminal. Another option is to use the MacPorts package manager () and command "`port install python36`". + +![Python REPL](images/python_macos.png){width=62%} + +Finally, open the terminal, execute `python3` and you should see the Python REPL as above. Press Ctrl+D or write `exit()` to leave the REPL. + +## Installing on Linux + +To install Python on Linux, you can download the latest Python 3 source releases from or use your package manager (apt-get, aptitude, synaptic and others) to install it. To make sure you have Python 3 installed on your system, run `python3 --version` in your terminal. + +Finally, open the terminal, execute `python3` and you should see the Python REPL as in the following image. Press Ctrl+D or write `exit()` to leave the REPL. + +![Python REPL](images/python_linux.png){width=62%} diff --git a/chapters/installation.tex b/chapters/installation.tex deleted file mode 100644 index de6a143..0000000 --- a/chapters/installation.tex +++ /dev/null @@ -1,63 +0,0 @@ -\chapter{Installation}\label{installation} - -In this chapter we will install and run the Python interpreter in your local computer. - -\section{Installing on Windows} - -\begin{enumerate} - -\item Download the latest Python 3 release for Windows on \url{https://www.python.org/downloads/windows/} and execute the installer. At the time of writing, this is Python 3.6.4. - -\item Make sure that the "Install launcher for all users" and "Add Python to PATH" settings are selected and choose "Customize installation". - -\begin{figure}[H] - \centering - \includegraphics[width=0.6\textwidth]{images/python_windows.jpg} -\end{figure} - -\item In the next screen "Optional Features", you can install everything, but it is essential to install "pip" and "pylauncher (for all users)". Pip is the Python package manager that allows you to install several Python packages and libraries. - -\item In the Advanced Options, make sure that you select "Add Python to environment variables". Also, I suggest that you change the install location to something like C:{\textbackslash}Python36{\textbackslash} as it will be easier for you to find the Python installation if something goes wrong. - -\begin{figure}[H] - \centering - \includegraphics[width=0.6\textwidth]{images/python_windows2.jpg} -\end{figure} - -\item Finally, allow Python to use more than 260 characters on the file system by selecting "Disable path length limit" and close the installation dialog. - -\begin{figure}[H] - \centering - \includegraphics[width=0.6\textwidth]{images/python_windows3.jpg} -\end{figure} - -\item Now, open the command line (cmd) and execute "python" or "python3". If everything was correctly installed, you should see the Python REPL. The REPL (from Read, Evaluate, Print and Loop) is a environment that you can use to program small snippets of Python code. Execute \textit{exit()} to exit. - -\begin{figure}[H] - \centering - \includegraphics[width=0.62\textwidth]{images/python_windows4.jpg} -\end{figure} - -\end{enumerate} - -\section{Installing on macOS} - -You can download the latest macOS binary releases from \url{https://www.python.org/downloads/mac-osx/}. Make sure you download the latest Python 3 release (3.6.4 at the time of writing). You can also use Homebrew, a package manager for macOS (\url{https://brew.sh/}). To install the latest Python 3 release with Homebrew, just do "\texttt{brew install python3}" on your terminal. - -\begin{figure}[H] - \centering - \includegraphics[width=0.62\textwidth]{images/python_macos.png} -\end{figure} - -Finally, open the terminal, execute $python3$ and you should see the Python REPL as above. Press Ctrl+D or write $exit()$ to leave the REPL. - -\section{Installing on Linux} - -For Linux, you can download the latest Python 3 binary releases from \url{https://www.python.org/downloads/linux/} or use your package manager to install it. To make sure you have Python 3 installed on your system, run $python3$ in your terminal. - -\begin{figure}[H] - \centering - \includegraphics[width=0.62\textwidth]{images/python_linux.png} -\end{figure} - -Finally, open the terminal, execute $python3$ and you should see the Python REPL as above. Press Ctrl+D or write $exit()$ to leave the REPL. diff --git a/chapters/introduction.md b/chapters/introduction.md new file mode 100644 index 0000000..40c507b --- /dev/null +++ b/chapters/introduction.md @@ -0,0 +1,13 @@ +# Introduction + +This book aims to teach the Python programming language using a practical approach. Its method is quite simple: after a short introduction to each topic, the reader is invited to learn more by solving the proposed exercises. + +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. + +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. + +The source of this book is available on github (). I welcome any pull requests to correct misspellings, suggest new exercises or to provide clarification of the current content. + +All the best, + +João Ventura - Adjunct Professor at the Escola Superior de Tecnologia de Setúbal. diff --git a/chapters/introduction.tex b/chapters/introduction.tex deleted file mode 100644 index f95b087..0000000 --- a/chapters/introduction.tex +++ /dev/null @@ -1,15 +0,0 @@ -\chapter{Introduction}\label{introduction} - -This book aims to teach the basics of the Python programming language using a practical approach. Its method is quite basic though: after a very simple introduction to each topic, the reader is invited to learn by solving the proposed exercises. - -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 at full 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. - -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, numbers and strings. In chapter \ref{functions} we will start tinkering with functions, and in chapter \ref{loops} the topic is about "loops". In chapter \ref{dictionaries} we will work with dictionaries and finally, in chapter \ref{classes} we will finish the book with some exercises about classes and object oriented programming. - -Please note that this book is a work in progress and as such may contain quite a few spelling errors that may be corrected in the future. However it is made available 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. - -This book is made available in github (check it at \url{https://github.com/joaoventura/full-speed-python}) so I appreciate any pull requests to correct misspellings or to suggest new exercises or clarification of the current content. - -Best wishes, - -João Ventura - Adjunct Professor at the Escola Superior de Tecnologia de Setúbal diff --git a/chapters/iterators.md b/chapters/iterators.md new file mode 100644 index 0000000..49facb4 --- /dev/null +++ b/chapters/iterators.md @@ -0,0 +1,101 @@ +# Iterators + +As we saw previously, in Python we use the "for" loop to iterate over the contents of objects: + +```Python +>>> for value in [0, 1, 2, 3, 4, 5]: +... print(value*value) +... +0 +1 +4 +9 +16 +25 +``` + +Objects that can be used with a "for" loop are called iterators. An iterator is, therefore, an object that follows the iteration protocol. + +The built-in function "iter" can be used to build iterator objects, while the "next" function can be used to gradually iterate over their content: + +```Python +>>> my_iter = iter([1, 2, 3]) +>>> my_iter + +>>> next(my_iter) +1 +>>> next(my_iter) +2 +>>> next(my_iter) +3 +>>> next(my_iter) +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` + +If there are no more elements, the iterator raises a "StopIteration" exception. + +## Iterator classes + +Iterators can be implemented as classes. You just need to implement the "\_\_next\_\_" and "\_\_iter\_\_" methods. Here’s an example of a class that mimics the "range" function, returning all values from "a" to "b": + +```Python +class MyRange: + + def __init__(self, a, b): + self.a = a + self.b = b + + def __iter__(self): + return self + + def __next__(self): + if self.a < self.b: + value = self.a + self.a += 1 + return value + else: + raise StopIteration +``` + +Basically, on every call to "next" it moves forward the internal variable "a" and returns its value. When it reaches "b", it raises the StopIteration exception. + +```Python +>>> myrange = MyRange(1, 4) +>>> next(myrange) +1 +>>> next(myrange) +2 +>>> next(myrange) +3 +>>> next(myrange) +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` + +But most important, you can use the iterator class in a "for" loop: + +```Python +>>> for value in MyRange(1, 4): +... print(value) +... +1 +2 +3 +``` + +## Exercises with iterators + +1. Implement an iterator class to return the square of all numbers from "a" to "b". + +2. Implement an iterator class to return all the even numbers from 1 to \(n\). + +3. Implement an iterator class to return all the odd numbers from 1 to \(n\). + +4. Implement an iterator class to return all numbers from \(n\) down to 0. + +5. Implement an iterator class to return the fibonnaci sequence from the first element up to \(n\). You can check the definition of the fibonnaci sequence in the function’s chapter. These are the first numbers of the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + +6. Implement an iterator class to return all consecutive pairs of numbers from 0 until \(n\), such as (0, 1), (1, 2), (2, 3)... diff --git a/chapters/lists.md b/chapters/lists.md new file mode 100644 index 0000000..11fd299 --- /dev/null +++ b/chapters/lists.md @@ -0,0 +1,81 @@ +# Lists + +Python lists are data structures that group sequences of elements. Lists can have elements of several types and you can also mix different types within the same list although all elements are usually of the same datatype. + +Lists are created using square brackets and the elements separated by commas. The elements in a list can be accessed by their positions where 0 is the index of the first element: + +```Python +>>> l = [1, 2, 3, 4, 5] +>>> l[0] +1 +>>> l[1] +2 +``` + +Can you access the number 4 in the previous list? + +Sometimes you want just a small portion of a list, a sublist. Sublists can be retrieved using a technique called *slicing*, which consists on defining the start and end indexes: + +```Python +>>> l = ['a', 'b', 'c', 'd', 'e'] +>>> l[1:3] +['b', 'c'] +``` + +Finally, arithmetic with lists is also possible, like adding two lists together or repeating the contents of a list. + +```Python +>>> [1,2] + [3,4] +[1, 2, 3, 4] +>>> [1,2] * 2 +[1, 2, 1, 2] +``` + +## Exercises with lists + +Create a list named "l" with the following values ([1, 4, 9, 10, 23]). Using the Python documentation about lists () solve the following exercises: + +1. Using list slicing get the sublists \[4, 9\] and \[10, 23\]. + +2. Append the value 90 to the end of the list "l". Check the difference between list concatenation and the "append" method. + +3. Calculate the average value of all values on the list. You can use the "sum" and "len" functions. + +4. Remove the sublist [4, 9]. + +## List comprehensions + +List comprehensions are a concise way to create lists. It consists of square brackets containing an expression followed by the "for" keyword. The result will be a list whose results match the expression. Here’s how to create a list with the squared numbers of another list. + +```Python +>>> [x*x for x in [0, 1, 2, 3]] +[0, 1, 4, 9] +``` + +Given its flexibility, list comprehensions generally make use of the "range" function which returns a range of numbers: + +```Python +>>> [x*x for x in range(4)] +[0, 1, 4, 9] +``` + +Sometimes you may want to filter the elements by a given condition. The "if" keyword can be used in those cases: + +```Python +>>> [x for x in range(10) if x % 2 == 0] +[0, 2, 4, 6, 8] +``` + +The example above returns all even values in range 0..10. More about list comprehensions can be found at . + +## Exercises with list comprehensions + +1. Using list comprehensions, create a list with the squares of the first 10 numbers. + +2. Using list comprehensions, create a list with the cubes of the first 20 numbers. + +3. Create a list comprehension with all the even numbers from 0 to 20, and another one with all the odd numbers. + +4. Create a list with the squares of the even numbers from 0 to 20, and sum the list using the "sum" function. The result should be 1140. First create the list using list comprehensions, check the result, then apply the sum to the list comprehension. + +5. Make a list comprehension that returns a list with the squares of all even numbers from 0 to 20, but ignore those numbers that are divisible by 3. In other words, each number should be divisible by 2 and not divisible by 3. Search for the "and" keyword in the Python documentation. The resulting list is \[4, 16, 64, 100, 196, 256\]. diff --git a/chapters/loops.md b/chapters/loops.md new file mode 100644 index 0000000..187d28b --- /dev/null +++ b/chapters/loops.md @@ -0,0 +1,122 @@ +# Iteration and loops + +In this chapter we are going to explore the topics of iteration and loops. Loops are used in computer programming to automate repetitive tasks. + +In Python the most common form of iteration is the "for" loop. The "for" loop allows you to iterate over all items of a list such that you can do whatever you want with each item. For instance, let’s create a list and print the square value of each element. + +```Python +>>> for value in [0, 1, 2, 3, 4, 5]: +... print(value * value) +... +0 +1 +4 +9 +16 +25 +``` + +It’s quite easy but very powerful\! The "for" loop is the basis of many things in programming. For instance, you already know about the "sum(list)" function which sums all the elements of a list, but here’s an example using the "for" loop: + +```Python +>>> mylist = [1,5,7] +>>> sum = 0 +>>> for value in mylist: +... sum = sum + value +... +>>> print(sum) +13 +``` + +Basically, you create the variable "sum" and keep adding each value as it comes from the list. + +Sometimes, instead of the values of a list, you may need to work with the indexes themselves, i.e., not with the values, but the positions where they are in the list. Here’s an example that iterates over a list and returns the indexes and the values for each index: + +```Python +>>> mylist = [1,5,7] +>>> for i in range(len(mylist)): +... print("Index:", i, "Value:", mylist[i]) +... +Index: 0 Value: 1 +Index: 1 Value: 5 +Index: 2 Value: 7 +``` + +You can see that we are not iterating over the list itself but iterating over the "range" of the length of the list. The range function returns a special list: + +```Python +>>> list(range(3)) +[0, 1, 2] +``` + +So, when you use "range" you are not iterating over "mylist" but over a list with some numbers that you’ll use as indexes to access individual values on "mylist". More about the range function in the Python docs at . + +Sometimes you may need both things (indexes and values), and you can use the "enumerate" function: + +```Python +>>> mylist = [1,5,7] +>>> for i, value in enumerate(mylist): +... print("Index:", i, "Value:", value) +... +Index: 0 Value: 1 +Index: 1 Value: 5 +Index: 2 Value: 7 +``` + +Remember that the first value on a Python list is always at index 0. + +Finally, we also have the "while" statement that allows us to repeat a sequence of instructions while a specified condition is true. For instance, the following example starts "n" at 10 and **while "n" is greater than 0**, it keeps subtracting 1 from "n". When "n" reaches 0, the condition "n \> 0" is false, and the loop ends: + +```Python +>>> n = 10 +>>> while n > 0: +... print(n) +... n = n-1 +... +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 +``` + +Notice that it never prints 0... + +## Exercises with the for loop + +For this section you may want to consult the Python docs at . + +1. Create a function "add" that receives a list as parameter and returns the sum of all elements in the list. Use the "for" loop to iterate over the elements of the list. + +2. Create a function that receives a list as parameter and returns the maximum value in the list. As you iterate over the list you may want to keep the maximum value found so far in order to keep comparing it with the next elements of the list. + +3. Modify the previous function such that it returns a list with the first element being the maximum value and the second being the index of the maximum value in the list. Besides keeping the maximum value found so far, you also need to keep the position where it occurred. + +4. Implement a function that returns the reverse of a list received as parameter. You may create an empty list and keep adding the values in reversed order as they come from the original list. Check what you can do with lists at . + +5. Make the function "is\_sorted" that receives a list as parameter and returns True if the list is sorted in ascending order. For instance \[1, 2, 2, 3\] is ordered while \[1, 2, 3, 2\] is not. Suggestion: you have to compare a number in the list with the next one, so you can use indexes or you need to keep the previous number in a variable as you iterate over the list. + +6. Implement the function "is\_sorted\_dec" which is similar to the previous one but all items must be sorted by decreasing order. + +7. Implement the "has\_duplicates" function which verifies if a list has duplicate values. You may have to use two "for" loops, where for each value you have to check for duplicates on the rest of the list. + +## Exercises with the while statement + +1. Implement a function that receives a number as parameter and prints, in decreasing order, which numbers are even and which are odd, until it reaches 0. + + >>> even_odd(10) + Even number: 10 + Odd number: 9 + Even number: 8 + Odd number: 7 + Even number: 6 + Odd number: 5 + Even number: 4 + Odd number: 3 + Even number: 2 + Odd number: 1 diff --git a/chapters/loops.tex b/chapters/loops.tex deleted file mode 100644 index 099bda2..0000000 --- a/chapters/loops.tex +++ /dev/null @@ -1,133 +0,0 @@ -\chapter{Iteration and loops}\label{loops} - -In this chapter we are going to explore the topics of iteration and loops. Loops are used in computer programming to automate repetitive tasks. - -In Python the most common form of iteration is the "for" loop. The "for" loop allows you to iterate over all items of a list such that you can do whatever you want with each item. For instance, let's create a list and print the square value of each element. - -\begin{lstlisting} ->>> for value in [0, 1, 2, 3, 4, 5]: -... print(value * value) -... -0 -1 -4 -9 -16 -25 -\end{lstlisting} - -It's quite easy but very powerful! The "for" loop is the basis of many things in programming. For instance, you already know about the "sum(list)" function which sums all the elements of a list, but here's an example using the "for" loop: - -\begin{lstlisting} ->>> mylist = [1,5,7] ->>> sum = 0 ->>> for value in mylist: -... sum = sum + value -... ->>> print(sum) -13 -\end{lstlisting} - -Basically, you create the variable "sum" and keep adding each value as it comes from the list. - -Sometimes, instead of the values of a list, you may need to work with the indexes themselves, i.e., not with the values, but the positions where they are in the list. Here's an example that iterates over a list and returns the indexes and the values for each index: - -\begin{lstlisting} ->>> mylist = [1,5,7] ->>> for i in range(len(mylist)): -... print("Index:", i, "Value:", mylist[i]) -... -Index: 0 Value: 1 -Index: 1 Value: 5 -Index: 2 Value: 7 -\end{lstlisting} - -You can see that we are not iterating over the list itself but iterating over the "range" of the length of the list. The range function returns a special list: - -\begin{lstlisting} ->>> list(range(3)) -[0, 1, 2] -\end{lstlisting} - -So, when you use "range" you are not iterating over "mylist" but over a list with some numbers that you'll use as indexes to access individual values on "mylist". More about the range function in the Python docs at \url{https://docs.python.org/3/tutorial/controlflow.html#the-range-function}. - -Sometimes you may need both things (indexes and values), and you can use the "enumerate" function: - -\begin{lstlisting} ->>> mylist = [1,5,7] ->>> for i, value in enumerate(mylist): -... print("Index:", i, "Value:", value) -... -Index: 0 Value: 1 -Index: 1 Value: 5 -Index: 2 Value: 7 -\end{lstlisting} - -Remember that the first value on a Python list is always at index 0. - -Finally, we also have the "while" statement that allows us to repeat a sequence of instructions while a specified condition is true. For instance, the following example starts "n" at 10 and \textbf{while "n" is greater than 0}, it keeps subtracting 1 from "n". When "n" reaches 0, the condition "n > 0" is false, and the loop ends: - -\begin{lstlisting} ->>> n = 10 ->>> while n > 0: -... print(n) -... n = n-1 -... -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 -\end{lstlisting} - -Notice that it never prints 0... - -\section{Exercises with the for loop} - -For this section you may want to consult the Python docs at \url{https://docs.python.org/3/tutorial/controlflow.html#for-statements}. - -\begin{enumerate} - -\item Create a function "add" that receives a list as parameter and returns the sum of all elements in the list. Use the "for" loop to iterate over the elements of the list. - -\item Create a function that receives a list as parameter and returns the maximum value in the list. As you iterate over the list you may want to keep the maximum value found so far in order to keep comparing it with the next elements of the list. - -\item Modify the previous function such that it returns a list with the first element being the maximum value and the second being the index of the maximum value in the list. Besides keep the last maximum value found so far, you need to keep also the position where it occured. - -\item Implement a function that returns the reverse of a list received as parameter. You may create an empty list and keep adding the values in reversed order as they come from the original list. Check what you can do with lists at \url{https://docs.python.org/3/tutorial/datastructures.html#more-on-lists}. - -\item Make the function "is\_sorted" that receives a list as parameter and returns True if the list is sorted by increasing order. For instance [1, 2, 2, 3] is ordered while [1, 2, 3, 2] is not. Suggestion: you have to compare a number in the list with the next one, so you can use indexes or you need to keep the previous number in a variable as you iterate over the list. - -\item Implement the function "is\_sorted\_dec" which is similar to the previous one but all items must be sorted by decreasing order. - -\item Implement the "has\_duplicates" function which verifies if a list has duplicate values. You may have to use two "for" loops, where for each value you have to check for duplicates on the rest of the list. - -\end{enumerate} - - -\section{Exercises with the while statement} - -\begin{enumerate} - -\item Implement a function that receives a number as parameter and prints, in decreasing order, which numbers are even and which are odd, until it reaches 0. - -\begin{lstlisting} ->>> even_odd(10) -Even number: 10 -Odd number: 9 -Even number: 8 -Odd number: 7 -Even number: 6 -Odd number: 5 -Even number: 4 -Odd number: 3 -Even number: 2 -Odd number: 1 -\end{lstlisting} - -\end{enumerate} diff --git a/diagrams/consumers_pipeline.txt b/diagrams/consumers_pipeline.txt new file mode 100644 index 0000000..95f9cd3 --- /dev/null +++ b/diagrams/consumers_pipeline.txt @@ -0,0 +1,13 @@ + +-------------+ + send() | | + +---->+ CONSUMER #1 | + | | | ++----------+ +----------+ | +-------------+ +| | send() | +--+ +| SOURCE +------->+ PRODUCER | +| | | +--+ ++----------+ +----------+ | +-------------+ + | | | + +---->+ CONSUMER #2 | + send() | | + +-------------+ diff --git a/diagrams/coroutine_pipeline.txt b/diagrams/coroutine_pipeline.txt new file mode 100644 index 0000000..b478032 --- /dev/null +++ b/diagrams/coroutine_pipeline.txt @@ -0,0 +1,9 @@ ++----------+ +-----------+ +-----------+ +| | send() | | send() | | +| SOURCE +------->+ COROUTINE +------->+ COROUTINE | +| | | | | | ++----------+ +-----------+ +-----------+ + + + + diff --git a/diagrams/instructions.txt b/diagrams/instructions.txt new file mode 100644 index 0000000..6ec9f8d --- /dev/null +++ b/diagrams/instructions.txt @@ -0,0 +1,3 @@ +1. Draw diagrams with http://asciiflow.com/ +2. Convert to png with http://ditaa.sourceforge.net/ +3. java -jar ditaa0_9.jar XXX.txt --scale 2 \ No newline at end of file diff --git a/full-speed-python.tex b/full-speed-python.tex deleted file mode 100644 index 8c94697..0000000 --- a/full-speed-python.tex +++ /dev/null @@ -1,63 +0,0 @@ -\documentclass[12pt, a4paper, oneside]{book} - -% Fonts -\usepackage[]{opensans} -\usepackage[T1]{fontenc} -\usepackage[utf8]{inputenc} - -% Paragraphs -\setlength{\parindent}{0pt} -\setlength{\parskip}{6pt plus 2pt minus 1pt} - -% Margins -\usepackage[margin=1in]{geometry} - -% Images -\usepackage{graphicx} -\usepackage{float} - -% Source code -\usepackage{listings} -\usepackage{xcolor} - -% Source code formatting -\lstset{ - backgroundcolor=\color[rgb]{0.95,0.95,0.95}, - basicstyle=\footnotesize, - language=Python, - aboveskip=1em, - belowskip=1em, -} - -% Headers and footers -\pagestyle{plain} - -% Urls -\usepackage{hyperref} - -% Equations -\usepackage{amsmath} - - -\begin{document} - -\title{At full speed with Python} -\author{João Ventura} -\date{v0.1} - -\maketitle - -{ - \setcounter{tocdepth}{2} - \tableofcontents -} - -\include{chapters/introduction} -\include{chapters/installation} -\include{chapters/basic-datatypes} -\include{chapters/functions} -\include{chapters/loops} -\include{chapters/dictionaries} -\include{chapters/classes} - -\end{document} diff --git a/images/consumers_pipeline.png b/images/consumers_pipeline.png new file mode 100644 index 0000000..ecfeaa4 Binary files /dev/null and b/images/consumers_pipeline.png differ diff --git a/images/coroutine_pipeline.png b/images/coroutine_pipeline.png new file mode 100644 index 0000000..cff6b63 Binary files /dev/null and b/images/coroutine_pipeline.png differ diff --git a/metadata.yaml b/metadata.yaml new file mode 100644 index 0000000..dbbb6ec --- /dev/null +++ b/metadata.yaml @@ -0,0 +1,12 @@ +--- +title: Full Speed Python +subtitle: v0.4.3 +author: João Ventura +lang: en-US + +# Variables for Latex +documentclass: book +classoption: oneside +fontsize: 12pt +toc: true +--- diff --git a/tex/preamble.tex b/tex/preamble.tex new file mode 100644 index 0000000..9c18140 --- /dev/null +++ b/tex/preamble.tex @@ -0,0 +1,19 @@ +% Paper and margins +\usepackage[a4paper, margin=1in]{geometry} + +% Fonts +\usepackage[]{opensans} +\usepackage[T1]{fontenc} +\usepackage[utf8]{inputenc} + +% Images +\usepackage{graphicx} +\usepackage{float} + +% All images should be where they are defined +\makeatletter +\def\fps@figure{H} +\makeatother + +% Headers and footers +\pagestyle{plain}