Video of previous lectures:
Donald Knuth is a Computer Scientist
Main contributions: theoretical and practical development of the analysis of the computational complexity of algorithms, The Art of Computer Programming series of monographs, TeX typesetting system for writing academic documents
The Art of Computer Programming is an ongoing work (3 volumes and an half out of 7 have been published so far)
Definition of functions:def <func_name>(<parameter_1>, ...)
It is the mechanism for implementing functions in Python, i.e. a mechanism for listing a sequence of instructions under a particular name
Built-in functions: made available by the programming language itself, e.g. len()
or list()
User-defined functions: written by a user of the language for addressing some specific requirements or tasks that are not addressable by means of one built-in function directly, like the algorithm implemented in the previous lecture
def add_one(n):
return n + 1
result = add_one(41)
print(result)
print(<object_1>, <object_2>, ...)
allows one to print on screen one or more values (that can be referred by a variable)
Module: a Python file (extension .py) that contains the definition of variables, functions, and even runnable code
Package: a mechanism to expose one or more Python modules, functions, variables
from <package> import <module or function or variable>
Example:
from collections import deque
The first volume of Knuth's series of books is entirely dedicated to a comprehensive introduction of all the basic data structures
A data structure is a way in which we can organise the information to process and returned by a computer, so as it can be accessed and modified in an efficient and computational manner
Broadly speaking, it is a sort of bucket where we can place some information, that provides some methods to add and retrieve pieces of such information
Among the simplest data structures, there are those ones that organise their item in a specific order and allow the repeatability of the values they contain
Order: the sequence in which the items are added to these data structures matters
Repeatability: the same value can appear twice or more times in the same data structure
A list is a countable sequence of ordered and repeatable items
Countable: it is possible to know the length of the list (i.e. how many items it contains) – in Python, we can use the function len(<countable_object>)
Ordered: the items are placed in the list in a specific order, which is preserved
Repeatable: the items may appear more than one time in the list
Create a new list: list()
Add new item: <list>.append(<item>)
Remove item: <list>.remove(<item>)
Add items from another list: <list>.extend(<another_list>)
my_first_list = list() my_first_list.append(34) my_first_list.append(15) my_first_list.append("Silvio") my_first_list.remove(34) my_first_list.extend(my_first_list)
my_first_list =
34
15
"Silvio"
15
"Silvio"
A stack is a is a kind of list seen from a particular perspective, i.e. from bottom to top, and with a specific set of operations
The items follow a last in first out strategy (LIFO) for addition and removal
The last item inserted in the structure is placed in the top of the stack and, thus, it is also the first one that will be removed when requested
For removing the item in the middle of the stack one has to remove all the items that have been added after such middle item
Create a new stack: deque()
Add new item: <stack>.append(<item>)
Remove (and return) latest item: <stack>.pop()
Add items from another stack: <stack>.extend(<another_stack>)
my_first_stack = deque() my_first_stack.append(34) my_first_stack.append(15) my_first_stack.extend(my_first_stack) my_first_stack.append("Silvio") my_first_stack.pop()
my_first_stack =
A queue is a is a kind of list seen from a particular perspective, i.e. from left to right, and with a specific set of operations
The items follow a first in first out strategy (FIFO) for addition and removal
The first item is placed in the left-most part of the queue and, thus, it is also the first one that will be removed when requested
For removing the item in the middle of the queue one has to remove all the items that have been added before such middle item
Create a new queue: deque()
Add new item: <queue>.append(<item>)
Remove (and return) first item: <queue>.popleft()
Add items from another queue: <queue>.extend(<another_queue>)
my_first_queue = deque() my_first_queue.append(34) my_first_queue.append(15) my_first_queue.append("Silvio") my_first_queue.popleft() my_first_queue.extend(my_first_queue)
my_first_queue =
34
15
"Silvio"
15
"Silvio"
From How To Code in Python: