Skip to content

Commit 0ebbaab

Browse files
committed
Third lab lecture updated
1 parent ec7bdb4 commit 0ebbaab

13 files changed

Lines changed: 321 additions & 247 deletions

File tree

laboratory/docs/chapter/03.md

Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,93 @@
44
Similar to lists, but their values can’t be modified
55

66
```py
7-
numbers = (10,30,50,70)
7+
numbers = (10, 30, 50, 70)
88
print(numbers[2])
99

10-
#ERROR: assignment is not allowed
10+
# TypeError: 'tuple' object does not support item assignment
1111
numbers[2] = 55
1212
```
1313

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:
1515

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+
```
1924

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 in range(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'):
2154

2255
```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]
2764
```
2865

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.
3069

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.
3471

3572
```py
36-
for a in range(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
3881
```
3982

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+
4085
## Exercises
4186

4287
### 1st Exercise
4388
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:
4489
```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"]
4691
```
4792

48-
**1.a)** Define a function named <code class="py">lab_lessons()</code> which takes <code class="py">l_schedule</code> as a parameter and returns the total number of <code class="py">"Laboratory"</code> lessons.
93+
**1.a)** Define a function named <code class="py">lab_lessons()</code> which takes <code class="py">ctp_lessons</code> as a parameter and returns the total number of <code class="py">"Laboratory"</code> lessons.
4994

5095
<button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_3_1a')">Show solution</button>
5196
<div class="solution-code" id="sol_3_1a">
@@ -59,7 +104,7 @@ def lab_lessons(a_list):
59104
```
60105
</div>
61106

62-
**1.b)** Define a function named <code class="py">all_before_lab()</code> which takes <code class="py">l_schedule</code> as a parameter and returns a list containing all the lessons scheduled before the first <code class="py">"Laboratory"</code> lesson.
107+
**1.b)** Define a function named <code class="py">all_before_lab()</code> which takes <code class="py">ctp_lessons</code> as a parameter and returns a list containing all the lessons scheduled before the first <code class="py">"Laboratory"</code> lesson.
63108

64109
<button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_3_1b')">Show solution</button>
65110
<div class="solution-code" id="sol_3_1b">
@@ -76,13 +121,13 @@ def all_before_lab(a_list):
76121
```
77122
</div>
78123

79-
**1.c)** Let's pretend we have a new list representing an extended version of the <code class="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 <code class="py">l_schedule_extended</code>. Each element is represented as a tuple: <code class="py">([HOURS],[DATE],[TIME],[TITLE])</code>. For instance, the third lesson "Algorithms" will have the corresponding tuple: <code class="py">(2,"15/10/21","12:30-14:30","Algorithms")</code>. Here we have the entire <code class="py">l_schedule_extended</code>:
124+
**1.c)** Let's pretend we have a new list representing an extended version of the <code class="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 <code class="py">ctp_lessons_extended</code>. Each element is represented as a tuple: <code class="py">([HOURS],[DATE],[TIME],[TITLE])</code>. For instance, the third lesson "Algorithms" will have the corresponding tuple: <code class="py">(2,"15/10/21","12:30-14:30","Algorithms")</code>. Here we have the entire <code class="py">ctp_lessons_extended</code>:
80125

81126
```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")]
83128
```
84129

85-
Define a function <code class="py">max_lessons_hours()</code> which takes <code class="py">l_schedule_extended</code> and a number <code class="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 = <code class="py">max_hours</code>, starting from the first lesson of the year.
130+
Define a function <code class="py">max_lessons_hours()</code> which takes <code class="py">ctp_lessons_extended</code> and a number <code class="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 = <code class="py">max_hours</code>, starting from the first lesson of the year.
86131

87132
<button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_3_1c')">Show solution</button>
88133

@@ -101,20 +146,23 @@ def max_lessons_hours(a_list, max_hours):
101146
```
102147
</div>
103148

104-
**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.
150+
105151
**Hint:** take a look at the `datetime` library
106152

107153
<button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_3_1d')">Show solution</button>
108154
<div class="solution-code" id="sol_3_1d">
109155
```py
110-
vacation_date = datetime.datetime.strptime("12/11/21", '%d/%m/%y')
156+
from datetime import datetime
157+
158+
vacation_date = datetime.strptime("12/11/21", '%d/%m/%y')
111159
back_date = vacation_date + datetime.timedelta(weeks=2)
112160
result = []
113161
i = 0
114-
for lesson_tuple in l_schedule_extended:
162+
for lesson_tuple in ctp_lessons_extended:
115163
lesson_date = datetime.datetime.strptime(lesson_tuple[1], '%d/%m/%y')
116164
if lesson_date < vacation_date or lesson_date > back_date:
117165
result.append(lesson_tuple[3])
118166
print(result)
119167
```
120-
</div>
168+
</div>

laboratory/site/chapter/01/index.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ <h2 id="check-if-python-is-already-installed">Check if Python is already install
141141
In computing, a <a href="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 <a href="https://en.wikipedia.org/wiki/Command-line_interface">command-line interface (CLI)</a> or a <a href="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.
142142
</span></p>
143143
<h3 id="open-the-command-line-interface-cli">Open the Command Line Interface (CLI)</h3>
144-
<h4>Windows</h4>
144+
<h4 id="windows">Windows</h4>
145145
<ul>
146146
<li>Open the Windows menu</li>
147147
<li>Type <em>"powershell"</em> in the search bar</li>
148148
<li>Select <strong>Windows PowerShell</strong> from the search results</li>
149149
</ul>
150-
<h4>macOS</h4>
150+
<h4 id="macos">macOS</h4>
151151
<ul>
152152
<li>Open the Spotlight search box in the upper right-hand corner</li>
153153
<li>Type <em>"terminal"</em> in the search bar</li>
154154
<li>Click on <strong>Terminal</strong> or just hit return if it is the first result</li>
155155
</ul>
156156
<p><span class="note">Alternatively, go to the directory <span class="dir">Applications/Utility</span> and open <strong>Terminal</strong> application.</span></p>
157-
<h4>Linux (Ubuntu)</h4>
157+
<h4 id="linux-ubuntu">Linux (Ubuntu)</h4>
158158
<ul>
159159
<li>Open the applications drawer</li>
160160
<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>
168168
<pre><code>python --version
169169
Python 2.7.3
170170
</code></pre>
171-
172171
<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>
173172
<h2 id="download-and-install-python">Download and install Python</h2>
174-
<h4>Windows</h4>
173+
<h4 id="windows_1">Windows</h4>
175174
<ul>
176175
<li>Go to the <a href="https://www.python.org/downloads/">Python download page</a></li>
177176
<li>Click on the download button to get the latest version of <strong>Python</strong></li>
@@ -189,15 +188,15 @@ <h4>Windows</h4>
189188
<li>Click on the <strong>Install</strong> button, and wait until the end of the installation</li>
190189
<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>
191190
</ul>
192-
<h4>macOS</h4>
191+
<h4 id="macos_1">macOS</h4>
193192
<ul>
194193
<li>Go to the <a href="https://www.python.org/downloads/">Python download page</a></li>
195194
<li>Click on the download button to get the latest version of <strong>Python</strong></li>
196195
<li>Double-click on the downloaded .pkg file to start the installation</li>
197196
<li>Follow the step-by-step guidelines</li>
198197
<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>
199198
</ul>
200-
<h4>Linux (Ubuntu)</h4>
199+
<h4 id="linux-ubuntu_1">Linux (Ubuntu)</h4>
201200
<ul>
202201
<li>Open the Command Line Interface (Terminal or Console)</li>
203202
<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>
217216
<pre><code>&gt;&gt;&gt; print(&quot;Hello world&quot;)
218217
Hello World
219218
</code></pre>
220-
221219
<ul>
222220
<li>Type <code>exit()</code> and press enter to exit from the python interactive mode.</li>
223221
</ul>
@@ -247,23 +245,23 @@ <h3 id="install-modules-with-pip3">Install modules with pip3</h3>
247245
</ul>
248246
<h2 id="install-the-editor-visual-studio-code">Install the editor: Visual Studio Code</h2>
249247
<p>Programming using the shell interpreter is convenient for small tests but impractical for developing complex applications. For this reason, special editors, called <a href="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+
<h4 id="windows_2">Windows</h4>
251249
<ul>
252250
<li>Download the <a href="https://code.visualstudio.com/docs?dv=win">Visual Studio Code installer</a> for Windows.</li>
253251
<li>Run the downloaded installer (VSCodeUserSetup-{version}.exe). This will only take a minute</li>
254252
<li>By default, VS Code is installed under <span class="dir">C:\users{username}\AppData\Local\Programs\Microsoft VS Code</span> </li>
255253
</ul>
256254
<p><span class="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 <a href="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>
257255
<p><span class="note"><strong>Note:</strong> you can find more details at <a href="https://code.visualstudio.com/docs/setup/windows">https://code.visualstudio.com/docs/setup/windows</a></span></p>
258-
<h4>macOS</h4>
256+
<h4 id="macos_2">macOS</h4>
259257
<ul>
260258
<li>Download <a href="https://go.microsoft.com/fwlink/?LinkID=534106">Visual Studio Code</a> for macOS.</li>
261259
<li>Open the browser's download list and locate the downloaded archive.</li>
262260
<li>Extract the contents of the downloaded archive. (usually with a double-click)</li>
263261
<li>Drag <strong>Visual Studio Code.app</strong> to the Applications folder, making it available in the macOS Launchpad.</li>
264262
</ul>
265263
<p><span class="note"><strong>Note:</strong> you can find more details at <a href="https://code.visualstudio.com/docs/setup/mac">https://code.visualstudio.com/docs/setup/mac</a></span></p>
266-
<h4>Linux</h4>
264+
<h4 id="linux">Linux</h4>
267265
<p>For Debian/Ubuntu based distributions, download and install the <a href="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 ./&lt;file&gt;.deb</code></p>
268266
<p><span class="note"><strong>Note:</strong> you can find more details and guidelines for other linux distributions at <a href="https://code.visualstudio.com/docs/setup/linux">https://code.visualstudio.com/docs/setup/linux</a></span></p>
269267
<h2 id="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
290288
<!-- <button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_1')">Show solution</button>s
291289
```{.python .code-overflow-wrap .solution-code #sol_1}-->
292290

293-
<pre><code class="py">print(&quot;Hello World !!&quot;)
291+
<pre><code class="language-py">print(&quot;Hello World !!&quot;)
294292
</code></pre>
295-
296293
<ul>
297294
<li>Run the script by clicking on the menu arrow near the run button and selecting <strong>Run Python File in Terminal</strong><br />
298295
<img src="../img/vsc_run.png"></li>

0 commit comments

Comments
 (0)