You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: laboratory/docs/chapter/03.md
+75-27Lines changed: 75 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,48 +4,93 @@
4
4
Similar to lists, but their values can’t be modified
5
5
6
6
```py
7
-
numbers = (10,30,50,70)
7
+
numbers = (10,30,50,70)
8
8
print(numbers[2])
9
9
10
-
#ERROR: assignment is not allowed
10
+
# TypeError: 'tuple' object does not support item assignment
11
11
numbers[2] =55
12
12
```
13
13
14
-
## Loops: "while" and "for"
14
+
A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:
15
15
16
-
* Loops are used in order to repeat similar actions
17
-
* We can't loop forever this will make our program crash
18
-
* Our loops need a rule/condition that defines when they need to stop
16
+
```py
17
+
empty = ()
18
+
singleton ='hello', # <-- note trailing comma
19
+
len(empty)
20
+
# 0
21
+
len(singleton)
22
+
# ('hello',)
23
+
```
19
24
20
-
### The "while" statement
25
+
## The "for" statement
26
+
27
+
Python's for statement iterates over the items of any sequence (e.g., strings, lists, tuples), in the order that they appear in the sequence.
28
+
29
+
```py
30
+
# What is the thickness of a sheet of paper folded 10 times?
31
+
32
+
times = [1,2,3,4,5,6,7,8,9,10]
33
+
thickness =0.1
34
+
for i in times:
35
+
thickness *=2
36
+
print(thickness)
37
+
# 102.4
38
+
```
39
+
40
+
If you do need to iterate over a sequence of numbers, the built-in function `range()` comes in handy. It generates arithmetic progressions.
41
+
42
+
```py
43
+
# What is the thickness of a sheet of paper folded 39 times?
44
+
45
+
thickness =0.1
46
+
for i inrange(39):
47
+
thickness *=2
48
+
print(thickness)
49
+
# 54975581388.8 cm = 549755.813888 Km
50
+
# Which is more than the distance between the Earth and the Moon (384400 Km)
51
+
```
52
+
53
+
The given end point is never part of the generated sequence; `range(10)` generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the 'step'):
21
54
22
55
```py
23
-
a =0
24
-
while a <10 :
25
-
print(a)
26
-
a +=1
56
+
list(range(5, 10))
57
+
# [5, 6, 7, 8, 9]
58
+
59
+
list(range(0, 10, 3))
60
+
# [0, 3, 6, 9]
61
+
62
+
list(range(-10, -100, -30))
63
+
# [-10, -40, -70]
27
64
```
28
65
29
-
### The "for" statement
66
+
### The "while" statement
67
+
68
+
The while statement is used for repeated execution as long as an expression is true.
30
69
31
-
* Repeat the inner code for a number of times
32
-
* We use it when we already know how many times/repetitions we need to execute our code
33
-
* In python a for loop is done its execution when it finishes iterating a given sequence
70
+
We generally use this loop when we don't know the number of times to iterate beforehand.
34
71
35
72
```py
36
-
for a inrange(0,10):
37
-
print(a)
73
+
distance_earth_moon =38440000000
74
+
thickness =0.1
75
+
times =0
76
+
while thickness < distance_earth_moon:
77
+
thickness *=2
78
+
times +=1
79
+
print(times)
80
+
# 39
38
81
```
39
82
83
+
Make sure that the while loop condition becomes false at some point, otherwise, the loop will continue forever (or until the PC crashes or the laptop battery runs out)
84
+
40
85
## Exercises
41
86
42
87
### 1st Exercise
43
88
Let's pretend we have a list containing the titles of all the Computational Thinking lessons. The list is ordered following the established schedule. Below is the list:
44
89
```py
45
-
l_schedule= ["Introduction to the course","Introduction to Computational Thinking","Algorithms","Laboratory","Computability","Programming languages","Organising information: ordered structures","Laboratory","Brute-force algorithms","Laboratory","Organising information: unordered structures","Laboratory","Recursion","Laboratory","Divide and conquer algorithms","Laboratory","Dynamic programming algorithms","Organising information: trees","Backtracking algorithms","Organising information: graphs","Greedy algorithms"]
90
+
ctp_lessons= ["Introduction to the course","Introduction to Computational Thinking","Algorithms","Laboratory","Computability","Programming languages","Organising information: ordered structures","Laboratory","Brute-force algorithms","Laboratory","Organising information: unordered structures","Laboratory","Recursion","Laboratory","Divide and conquer algorithms","Laboratory","Dynamic programming algorithms","Organising information: trees","Backtracking algorithms","Organising information: graphs","Greedy algorithms"]
46
91
```
47
92
48
-
**1.a)** Define a function named <codeclass="py">lab_lessons()</code> which takes <codeclass="py">l_schedule</code> as a parameter and returns the total number of <codeclass="py">"Laboratory"</code> lessons.
93
+
**1.a)** Define a function named <codeclass="py">lab_lessons()</code> which takes <codeclass="py">ctp_lessons</code> as a parameter and returns the total number of <codeclass="py">"Laboratory"</code> lessons.
**1.b)** Define a function named <codeclass="py">all_before_lab()</code> which takes <codeclass="py">l_schedule</code> as a parameter and returns a list containing all the lessons scheduled before the first <codeclass="py">"Laboratory"</code> lesson.
107
+
**1.b)** Define a function named <codeclass="py">all_before_lab()</code> which takes <codeclass="py">ctp_lessons</code> as a parameter and returns a list containing all the lessons scheduled before the first <codeclass="py">"Laboratory"</code> lesson.
**1.c)** Let's pretend we have a new list representing an extended version of the <codeclass="py">l_schedule</code>, such that it embeds information about the date, time and the duration (in hours) of the each lesson. We call the new list <codeclass="py">l_schedule_extended</code>. Each element is represented as a tuple: <codeclass="py">([HOURS],[DATE],[TIME],[TITLE])</code>. For instance, the third lesson "Algorithms" will have the corresponding tuple: <codeclass="py">(2,"15/10/21","12:30-14:30","Algorithms")</code>. Here we have the entire <codeclass="py">l_schedule_extended</code>:
124
+
**1.c)** Let's pretend we have a new list representing an extended version of the <codeclass="py">ctp_lessons</code>, such that it embeds information about the date, time and the duration (in hours) of the each lesson. We call the new list <codeclass="py">ctp_lessons_extended</code>. Each element is represented as a tuple: <codeclass="py">([HOURS],[DATE],[TIME],[TITLE])</code>. For instance, the third lesson "Algorithms" will have the corresponding tuple: <codeclass="py">(2,"15/10/21","12:30-14:30","Algorithms")</code>. Here we have the entire <codeclass="py">ctp_lessons_extended</code>:
80
125
81
126
```py
82
-
l_schedule_extended = [(2,"11/10/21","09:30-11:30","Introduction to the course"),(2,"13/10/21","09:30-11:30","Introduction to Computational Thinking"),(2,"15/10/21","12:30-14:30","Algorithms"),(2,"18/10/21","09:30-11:30","Laboratory"),(2,"20/10/21","09:30-11:30","Computability"),(2,"22/10/21","12:30-14:30","Programming languages"),(2,"25/10/21","09:30-11:30","Organising information: ordered structures"),(2,"27/10/21","09:30-11:30","Laboratory"),(2,"29/10/21","12:30-14:30","Brute-force algorithms"),(2,"08/11/21","09:30-11:30","Laboratory"),(2,"10/11/21","09:30-11:30","Organising information: unordered structures"),(2,"15/11/21","09:30-11:30","Laboratory"),(2,"17/11/21","09:30-11:30","Recursion"),(2,"22/11/21","09:30-11:30","Laboratory"),(2,"24/11/21","09:30-11:30","Divide and conquer algorithms"),(2,"29/11/21","09:30-11:30","Laboratory"),(2,"01/12/21","09:30-11:30","Dynamic programming algorithms"),(2,"06/12/21","09:30-11:30","Organising information: trees"),(2,"13/12/21","09:30-11:30","Backtracking algorithms"),(2,"15/12/21","09:30-11:30","Organising information: graphs"),(2,"20/12/21","09:30-11:30","Greedy algorithms")]
127
+
ctp_lessons_extended = [(2,"12/10/22","09:30-11:30","Introduction to the course"),(2,"14/10/22","12:30-14:30 ","Introduction to Computational Thinking"),(2,"17/10/22","09:30-11:30","Algorithms"),(2,"19/10/22","09:30-11:30","Laboratory"),(2,"21/10/22","12:30-14:30","Computability"),(2,"24/10/22","09:30-11:30","Programming languages"),(2,"26/10/22","09:30-11:30","Laboratory"),(2,"28/10/22","12:30-14:30","Organising information: ordered structures"),(2,"09/11/22","09:30-11:30","Brute-force algorithms"),(2,"11/11/22","12:30-14:30","Laboratory"),(2,"14/11/22","09:30-11:30","Organising information: unordered structures"),(2,"16/11/22","09:30-11:30","Laboratory"),(2,"21/11/22","09:30-11:30","Recursion"),(2,"23/11/22","09:30-11:30","Divide and conquer algorithms"),(2,"28/11/22","09:30-11:30","Laboratory"),(2,"30/11/22","09:30-11:30","Dynamic programming algorithms"),(2,"05/12/22","09:30-11:30","Laboratory"),(2,"12/12/22","09:30-11:30","Organising information: trees"),(2,"14/12/22","09:30-11:30","Backtracking algorithms"),(2,"19/12/22","09:30-11:30","Organising information: graphs"),(2,"21/12/22","09:30-11:30","Greedy algorithms")]
83
128
```
84
129
85
-
Define a function <codeclass="py">max_lessons_hours()</code> which takes <codeclass="py">l_schedule_extended</code> and a number <codeclass="py">max_hours</code> as parameters, and returns a list containing only the titles of all the lessons which could be attended with a maximum number of hours = <codeclass="py">max_hours</code>, starting from the first lesson of the year.
130
+
Define a function <codeclass="py">max_lessons_hours()</code> which takes <codeclass="py">ctp_lessons_extended</code> and a number <codeclass="py">max_hours</code> as parameters, and returns a list containing only the titles of all the lessons which could be attended with a maximum number of hours = <codeclass="py">max_hours</code>, starting from the first lesson of the year.
**1.d)** One of the students attending the course, Tim Berners Lee, thinks he knows enough about the materials taught in the course, so he will take a break and go for a vacation on 12/11/21 for 2 weeks. Right after that, when he gets back he will attend again the lectures starting from the first available one according to the schedule until the end of the course. Write down a portion of code that prints the titles of the lectures that Tim Berners Lee will attend at the end of the course.
149
+
**1.d)** One of the students attending the course, Tim Berners Lee, thinks he knows enough about the materials taught in the course, so he will take a break and go on a vacation on 12/11/21 for two weeks. Right after that, when he returns, he will attend the lectures again, starting from the first available one according to the schedule until the end of the course. Write down a portion of the code that prints the titles of the lectures that Tim Berners Lee will attend at the end of the course.
Copy file name to clipboardExpand all lines: laboratory/site/chapter/01/index.html
+10-13Lines changed: 10 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -141,20 +141,20 @@ <h2 id="check-if-python-is-already-installed">Check if Python is already install
141
141
In computing, a <ahref="https://en.wikipedia.org/wiki/Shell_(computing)">shell</a> is a user interface for accessing the services of an operating system. It can be a <ahref="https://en.wikipedia.org/wiki/Command-line_interface">command-line interface (CLI)</a> or a <ahref="https://en.wikipedia.org/wiki/Graphical_user_interface">graphical user interface (GUI)</a>. It enables a user to perform several operations, such as file management, installations, running processes, monitoring and configuring an OS. We will use it to install packages and as an interpreter for Python.
142
142
</span></p>
143
143
<h3id="open-the-command-line-interface-cli">Open the Command Line Interface (CLI)</h3>
144
-
<h4>Windows</h4>
144
+
<h4id="windows">Windows</h4>
145
145
<ul>
146
146
<li>Open the Windows menu</li>
147
147
<li>Type <em>"powershell"</em> in the search bar</li>
148
148
<li>Select <strong>Windows PowerShell</strong> from the search results</li>
149
149
</ul>
150
-
<h4>macOS</h4>
150
+
<h4id="macos">macOS</h4>
151
151
<ul>
152
152
<li>Open the Spotlight search box in the upper right-hand corner</li>
153
153
<li>Type <em>"terminal"</em> in the search bar</li>
154
154
<li>Click on <strong>Terminal</strong> or just hit return if it is the first result</li>
155
155
</ul>
156
156
<p><spanclass="note">Alternatively, go to the directory <spanclass="dir">Applications/Utility</span> and open <strong>Terminal</strong> application.</span></p>
157
-
<h4>Linux (Ubuntu)</h4>
157
+
<h4id="linux-ubuntu">Linux (Ubuntu)</h4>
158
158
<ul>
159
159
<li>Open the applications drawer</li>
160
160
<li>Type <em>"terminal"</em> or <em>"konsole"</em> in the search bar</li>
@@ -168,10 +168,9 @@ <h3 id="check-the-python-version">Check the Python version</h3>
168
168
<pre><code>python --version
169
169
Python 2.7.3
170
170
</code></pre>
171
-
172
171
<p>Try to type <code>python3 --version</code> (also <code>python3 -V</code> on Windows) in the command line, then press return to check whether you have Python 3 installed, and eventually the last version.</p>
173
172
<h2id="download-and-install-python">Download and install Python</h2>
174
-
<h4>Windows</h4>
173
+
<h4id="windows_1">Windows</h4>
175
174
<ul>
176
175
<li>Go to the <ahref="https://www.python.org/downloads/">Python download page</a></li>
177
176
<li>Click on the download button to get the latest version of <strong>Python</strong></li>
@@ -189,15 +188,15 @@ <h4>Windows</h4>
189
188
<li>Click on the <strong>Install</strong> button, and wait until the end of the installation</li>
190
189
<li>Finally, go to the command line and try to type <code>python --version</code> (also <code>python3 -V</code>) and press return to check whether you correctly installed Python.</li>
191
190
</ul>
192
-
<h4>macOS</h4>
191
+
<h4id="macos_1">macOS</h4>
193
192
<ul>
194
193
<li>Go to the <ahref="https://www.python.org/downloads/">Python download page</a></li>
195
194
<li>Click on the download button to get the latest version of <strong>Python</strong></li>
196
195
<li>Double-click on the downloaded .pkg file to start the installation</li>
197
196
<li>Follow the step-by-step guidelines</li>
198
197
<li>Finally, go to the command line and try to type <code>python --version</code> (also <code>python3 --version</code> in case you have also Python 2) and press return to check whether you correctly installed Python</li>
199
198
</ul>
200
-
<h4>Linux (Ubuntu)</h4>
199
+
<h4id="linux-ubuntu_1">Linux (Ubuntu)</h4>
201
200
<ul>
202
201
<li>Open the Command Line Interface (Terminal or Console)</li>
203
202
<li>Type <code>sudo apt-get update</code></li>
@@ -217,7 +216,6 @@ <h3 id="play-with-python-in-the-shell">Play with Python in the shell</h3>
<li>Type <code>exit()</code> and press enter to exit from the python interactive mode.</li>
223
221
</ul>
@@ -247,23 +245,23 @@ <h3 id="install-modules-with-pip3">Install modules with pip3</h3>
247
245
</ul>
248
246
<h2id="install-the-editor-visual-studio-code">Install the editor: Visual Studio Code</h2>
249
247
<p>Programming using the shell interpreter is convenient for small tests but impractical for developing complex applications. For this reason, special editors, called <ahref="https://en.wikipedia.org/wiki/Integrated_development_environment">integrated development environments</a> (IDE), are used. For Python, the most widely used IDEs are Visual Studio Code, PyCharm and Atom. We suggest using Visual Studio Code for this course.</p>
250
-
<h4>Windows</h4>
248
+
<h4id="windows_2">Windows</h4>
251
249
<ul>
252
250
<li>Download the <ahref="https://code.visualstudio.com/docs?dv=win">Visual Studio Code installer</a> for Windows.</li>
253
251
<li>Run the downloaded installer (VSCodeUserSetup-{version}.exe). This will only take a minute</li>
254
252
<li>By default, VS Code is installed under <spanclass="dir">C:\users{username}\AppData\Local\Programs\Microsoft VS Code</span></li>
255
253
</ul>
256
254
<p><spanclass="note"><strong>Note:</strong> .NET Framework 4.5.2 or higher is required for VS Code. If you are using Windows 7, make sure you have at least <ahref="https://www.microsoft.com/download/details.aspx?id=42643">.NET Framework 4.5.2</a> installed. You can check your version of .NET Framework using this command, <code>reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full" /v version</code> from a command prompt.</span></p>
257
255
<p><spanclass="note"><strong>Note:</strong> you can find more details at <ahref="https://code.visualstudio.com/docs/setup/windows">https://code.visualstudio.com/docs/setup/windows</a></span></p>
258
-
<h4>macOS</h4>
256
+
<h4id="macos_2">macOS</h4>
259
257
<ul>
260
258
<li>Download <ahref="https://go.microsoft.com/fwlink/?LinkID=534106">Visual Studio Code</a> for macOS.</li>
261
259
<li>Open the browser's download list and locate the downloaded archive.</li>
262
260
<li>Extract the contents of the downloaded archive. (usually with a double-click)</li>
263
261
<li>Drag <strong>Visual Studio Code.app</strong> to the Applications folder, making it available in the macOS Launchpad.</li>
264
262
</ul>
265
263
<p><spanclass="note"><strong>Note:</strong> you can find more details at <ahref="https://code.visualstudio.com/docs/setup/mac">https://code.visualstudio.com/docs/setup/mac</a></span></p>
266
-
<h4>Linux</h4>
264
+
<h4id="linux">Linux</h4>
267
265
<p>For Debian/Ubuntu based distributions, download and install the <ahref="https://go.microsoft.com/fwlink/?LinkID=760868">.deb package (64-bit)</a>, either through the graphical software center if it's available, or through the command line with: <code>sudo apt install ./<file>.deb</code></p>
268
266
<p><spanclass="note"><strong>Note:</strong> you can find more details and guidelines for other linux distributions at <ahref="https://code.visualstudio.com/docs/setup/linux">https://code.visualstudio.com/docs/setup/linux</a></span></p>
269
267
<h2id="create-and-run-a-python-script-in-visual-studio-code">Create and run a Python script in Visual Studio Code</h2>
@@ -290,9 +288,8 @@ <h3 id="create-and-run-the-first-python-script">Create and run the first Python
0 commit comments