Skip to content

Commit 2f775cc

Browse files
author
ivan.heibi
committed
lab chapters
1 parent 6ca7afa commit 2f775cc

File tree

19 files changed

+8378
-1875
lines changed

19 files changed

+8378
-1875
lines changed

laboratory/404.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
<li>
4949
<a href="/chapter/02/" class="dropdown-item">2. Programming in Python - the basics</a>
5050
</li>
51+
52+
<li>
53+
<a href="/chapter/03/" class="dropdown-item">3. Working with lists and tuples</a>
54+
</li>
55+
56+
<li>
57+
<a href="/chapter/04/" class="dropdown-item">4. Working with unordered structures</a>
58+
</li>
59+
60+
<li>
61+
<a href="/chapter/05/" class="dropdown-item">5. Recursion and working with files</a>
62+
</li>
63+
64+
<li>
65+
<a href="/chapter/06/" class="dropdown-item">6. Data analysis project</a>
66+
</li>
5167
</ul>
5268
</li>
5369
</ul>

laboratory/chapter/01/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
<li>
4949
<a href="../02/" class="dropdown-item">2. Programming in Python - the basics</a>
5050
</li>
51+
52+
<li>
53+
<a href="../03/" class="dropdown-item">3. Working with lists and tuples</a>
54+
</li>
55+
56+
<li>
57+
<a href="../04/" class="dropdown-item">4. Working with unordered structures</a>
58+
</li>
59+
60+
<li>
61+
<a href="../05/" class="dropdown-item">5. Recursion and working with files</a>
62+
</li>
63+
64+
<li>
65+
<a href="../06/" class="dropdown-item">6. Data analysis project</a>
66+
</li>
5167
</ul>
5268
</li>
5369
</ul>

laboratory/chapter/02/index.html

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@
4848
<li>
4949
<a href="./" class="dropdown-item active">2. Programming in Python - the basics</a>
5050
</li>
51+
52+
<li>
53+
<a href="../03/" class="dropdown-item">3. Working with lists and tuples</a>
54+
</li>
55+
56+
<li>
57+
<a href="../04/" class="dropdown-item">4. Working with unordered structures</a>
58+
</li>
59+
60+
<li>
61+
<a href="../05/" class="dropdown-item">5. Recursion and working with files</a>
62+
</li>
63+
64+
<li>
65+
<a href="../06/" class="dropdown-item">6. Data analysis project</a>
66+
</li>
5167
</ul>
5268
</li>
5369
</ul>
@@ -64,7 +80,7 @@
6480
</a>
6581
</li>
6682
<li class="nav-item">
67-
<a rel="next" class="nav-link disabled">
83+
<a rel="next" href="../03/" class="nav-link">
6884
Next <i class="fa fa-arrow-right"></i>
6985
</a>
7086
</li>
@@ -121,14 +137,14 @@
121137
<h1 id="programming-in-python-the-basics">Programming in Python - the basics</h1>
122138
<h2 id="syntax-in-python">Syntax in Python</h2>
123139
<h3 id="variables">Variables</h3>
124-
<p>Some general considerations:</p>
140+
<p><strong>Some general considerations:</strong></p>
125141
<ul>
126-
<li>Python does not require you to define the type of variables, just declare them:</li>
142+
<li>Python does not require you to define the type of the variables, just declare them:</li>
127143
</ul>
128144
<pre class="code-overflow-wrap"><code class="language-python">a = 2
129145
</code></pre>
130146
<ul>
131-
<li>Can not start with a digit or contain a special symbol (|£$%@# etc.)</li>
147+
<li>The name of a variable can not start with a digit or contain a special symbol (|£$%@# etc.)</li>
132148
<li>Some keywords cannot be used for naming variables.</li>
133149
</ul>
134150
<pre class="code-overflow-wrap"><code class="language-python">False | class | finally | is | return | None | continue | for | lambda | try | True | def
@@ -140,7 +156,7 @@ <h3 id="variables">Variables</h3>
140156
</ul>
141157
<pre class="code-overflow-wrap"><code class="language-python">variable != Variable
142158
</code></pre>
143-
<p>Some good practises for naming a variable:</p>
159+
<p><strong>Some good practises for naming a variable:</strong></p>
144160
<ul>
145161
<li>Combine lowercase letters (a-z) uppercase letters (A-Z), digits (1-9) and underscore (_)</li>
146162
<li>Use camelCase(<code>thisIsMyVariable</code>) or underscore to separate words (<code>this_is_my_variable</code>)</li>
@@ -317,14 +333,14 @@ <h2 id="exercises">Exercises</h2>
317333
<h3 id="1st-exercise">1st Exercise</h3>
318334
<p>The function <code>is_friend_of_harry()</code> returns a <em>True</em> value if a given name (i.e. type string) is one of the friends of Harry Potter, otherwise the function returns <em>False</em>. Let's pretend the friends of Harry Potter are: <em>"Ron", "Hermione", "Hagrid",</em> and <em>"Dumbledore"</em>. </p>
319335
<p><strong>Example:</strong> calling the function <code>is_friend_of_harry("Malfoy")</code> returns <em>False</em>.</p>
320-
<p><span class="ex-part"> <strong>1.a)</strong> Define the function <code>is_friend_of_harry()</code>, and call it using the following names as parameters: <em>"Hagrid", "Voldemort",</em> and <em>"Bellatrix"</em>. Print the result (i.e. <em>True/False</em>) of each call. </span></p>
336+
<p><span class="ex-part"> <strong>1.a)</strong> Define the function <code>is_friend_of_harry()</code>, and call it for each one of the following characters: <em>"Hagrid", "Voldemort",</em> and <em>"Bellatrix"</em>. Print the result (i.e. <em>True/False</em>) of each call. </span></p>
321337
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_1')">Show solution</button></p>
322338
<pre id="sol_1" class="code-overflow-wrap solution-code"><code class="language-python">def is_friend_of_harry(p_name):
323-
friends_list = [&quot;Ron&quot;, &quot;Hermione&quot;, &quot;Hagrid&quot;, &quot;Dumbledore&quot;]
324-
if p_name in friends_list:
325-
return True
326-
else:
327-
return False
339+
friends_list = [&quot;Ron&quot;, &quot;Hermione&quot;, &quot;Hagrid&quot;, &quot;Dumbledore&quot;]
340+
if p_name in friends_list:
341+
return True
342+
else:
343+
return False
328344

329345
print(is_friend_of_harry(&quot;Hagrid&quot;))
330346
print(is_friend_of_harry(&quot;Voldemort&quot;))
@@ -341,7 +357,7 @@ <h3 id="1st-exercise">1st Exercise</h3>
341357
else:
342358
print(&quot;Harry has no friends!!&quot;)
343359
</code></pre>
344-
<p><span class="ex-part"> <strong>1.c)</strong> Let's make our function <code>is_friend_of_harry()</code> a bit more powerful. Such that if I type a name in lowercase (e.g. "ron") or if I use spaces at the end of the name (e.g. "Ron "), or even if I do both the things, the function should work the same. </p>
360+
<p><span class="ex-part"> <strong>1.c)</strong> Let's make our function <code>is_friend_of_harry()</code> a bit more powerful. The new function should work the same way even if the given name is not well-written: (a) is all/in part written in lowercase (e.g. "ron", "Ron"), (b) includes spaces (e.g. "Ron ", " ron"), or (c) has both (a) and (b) cases in it. </p>
345361
<p><strong>Hint:</strong> in python <code>{string}.lower()</code> transforms a string to its lowercase form; <code>{string}.strip()</code> removes the starting and ending whitespaces (if any); and <code>{string}.capitalize()</code> capitalizes the first letter of a string </span></p>
346362
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_3')">Show solution</button></p>
347363
<pre id="sol_3" class="code-overflow-wrap solution-code"><code class="language-python">def is_friend_of_harry(p_name):
@@ -354,7 +370,7 @@ <h3 id="1st-exercise">1st Exercise</h3>
354370
else:
355371
return False
356372
</code></pre>
357-
<p><span class="ex-part"> <strong>1.d)</strong> Define another function <code>is_prof_friend_of_harry()</code> which returns a <em>True</em> value if a given name is a professor and a friend of Harry, otherwise the function must return <em>False</em>. The function should call the improved version of the function <code>is_friend_of_harry()</code> in its code (i.e. defined in (1.c)). Let's pretend the professors of Harry Potter are: <em>"Snape", "Lupin", "Hagrid",</em> and <em>"Dumbledore"</em>. </span></p>
373+
<p><span class="ex-part"> <strong>1.d)</strong> Define another function <code>is_prof_friend_of_harry()</code> which returns a <em>True</em> value if a given name, which might be in a not well-written form (as in (1.c)), is a professor and a friend of Harry, otherwise the function must return <em>False</em>. The function should call the improved version of the function <code>is_friend_of_harry()</code> (i.e. defined in (1.c)) in its code. Let's pretend the professors of Harry Potter are: <em>"Snape", "Lupin", "Hagrid",</em> and <em>"Dumbledore"</em>. </span></p>
358374
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_4')">Show solution</button></p>
359375
<pre id="sol_4" class="code-overflow-wrap solution-code"><code class="language-python">
360376
def is_prof_friend_of_harry(p_name):
@@ -373,7 +389,7 @@ <h3 id="1st-exercise">1st Exercise</h3>
373389
</code></pre>
374390
<h3 id="2nd-exercise">2nd Exercise</h3>
375391
<p>Each house of Hogwarts has a score which is updated based on some actions such as exams grades and the rules violation. Let's pretend we have a list of the houses <code class="py">houses = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]</code> and another list containing the score of each house <code class="py">scores = [0,0,0,0]</code>, such that the score value in position N of the scores list belongs to the house in position N of the houses list .</p>
376-
<p><strong>a)</strong> Define the function <code>update_house_score()</code> function which increments/decrements the score of a specific house with a given points. The function takes three parameters: <code>house_name</code>, <code>action</code> (string value "+"/"-"), and <code>points</code>.<br />
392+
<p><strong>2.a)</strong> Define the function <code>update_house_score()</code> function which increments/decrements the score of a specific house with a given points. The function takes three parameters: <code>house_name</code>, <code>action</code> (string value "+"/"-"), and <code>points</code>.<br />
377393
<strong>Example:</strong> Calling the function this way: <code class="py">update_house_score("Gryffindor","+",5)</code> should increment the score of house Gryffindor by 5 points.<br />
378394
<strong>Hint:</strong> The function <code class="py">{list}.index({value})</code> returns the index of an element in the list, e.g. <code class="py">houses.index("Hufflepuff")</code> returns the value <em>1</em></p>
379395
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_5')">Show solution</button></p>
@@ -387,14 +403,21 @@ <h3 id="2nd-exercise">2nd Exercise</h3>
387403
else:
388404
scores[index] -= points
389405
</code></pre>
390-
<p><strong>b)</strong> After the Quidditch cup the Houses have increased/decreased their scores as follows: +10 Gryffindor, +7 Hufflepuff, -3 Slytherin. After the game a member of house "Slytherin" helped a man riding his broom and his house gained back 5 points. Call the function <code>update_house_score()</code> for each action in order to update the houses points and print the two lists: <code>houses</code> and <code>scores</code>.</p>
406+
<p><strong>2.b)</strong> After the Quidditch cup the Houses have increased/decreased their scores as follows: +10 Gryffindor, +7 Hufflepuff, -3 Slytherin. After the game a member of house "Slytherin" helped a man riding his broom and his house gained back 5 points. Call the function <code>update_house_score()</code> for each action in order to update the houses points and print the two lists: <code>houses</code> and <code>scores</code>.</p>
391407
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_6')">Show solution</button></p>
392408
<pre id="sol_6" class="code-overflow-wrap solution-code"><code class="language-python">update_house_score(&quot;Gryffindor&quot;,&quot;+&quot;,10)
393409
update_house_score(&quot;Hufflepuff&quot;,&quot;+&quot;,7)
394410
update_house_score(&quot;Slytherin&quot;,&quot;-&quot;,3)
395411
update_house_score(&quot;Slytherin&quot;,&quot;+&quot;,5)
396412
print(houses)
397413
print(scores)
414+
</code></pre>
415+
<p><strong>2.c)</strong> redefine <code>update_house_score()</code> using the function <code>eval()</code> (<a href="https://docs.python.org/3/library/functions.html#eval">https://docs.python.org/3/library/functions.html#eval</a>)</p>
416+
<p><button class="toggle-solution btn btn-light" onclick="toggle_click(this,'sol_2_2c')">Show solution</button></p>
417+
<pre id="sol_2_2c" class="code-overflow-wrap solution-code"><code class="language-python">def update_house_score(house_name, action, points):
418+
index = houses.index(house_name)
419+
scores[index] = eval(str(points) + action + str(scores[index]))
420+
return scores
398421
</code></pre></div>
399422
</div>
400423
</div>

0 commit comments

Comments
 (0)