/ 1.qmd
1.qmd
  1  ---
  2  title: Data Science and Visualization, S2026
  3  subtitle: Atomic types, operators, and containers
  4  date: 2026-02-10
  5  
  6  jupyter: python3
  7  ---
  8  
  9  ## Exercise 1: Using markdown
 10  
 11  In the empty markdown cell below:
 12  
 13  1. Make a level 1 heading with yesterday's calender date
 14  2. Make the level 2 heading "In the morning"
 15  3. Make a numbered list of the things you did.
 16     Put at least one word in bold
 17  4. Make the level 2 heading "In the afternoon"
 18  5. Make a bullet point list of the things you did.
 19     Insert at least one hyperlink to a web page you visited
 20  6. Make the level 1 heading "My favorite equation"
 21  7. Write a [linear equation][Linear_equation] with a single unknown
 22  
 23  [linear equation]:
 24   https://en.wikipedia.org/wiki/Linear_equation
 25  
 26  If you need help,
 27  go to <https://www.markdownguide.org/basic-syntax/>
 28  for an overview of basic markdown syntax
 29  and <https://jupyterbook.org/v1/content/math.html>
 30  for an introduction to writing mathematical expressions
 31  in Jupyter Notebook
 32  
 33  # 2026-02-09
 34  ## In the morning
 35  * woke up
 36  * lit a **fire**
 37  * checked mail
 38  * had breakfast
 39  * coded some Rust
 40  ## In the afternoon
 41  * coded some more Rust
 42  * checked the [Moodle
 43    page](https://moodle.ruc.dk/course/view.php?id=24954)
 44    for tomorrow's lecture
 45  # My favorite equation
 46  $$
 47  x=\frac{1}{2}x+5
 48  $$
 49  
 50  ## Exercise 2: Atomic types
 51  
 52  __2.1__ In the code cell below,
 53  use the functions `type()` and `print()`
 54  to examine and print the types
 55  of the variables `u`, `v`, `x`, `y`, and `z`
 56  
 57  ```{python}
 58  # Assign five objects of different atomic types
 59  # to five different variables u, v, x, y, and z
 60  u = None
 61  v = True
 62  x = 10.2
 63  y = 45
 64  z = "100.01"
 65  
 66  # Print the types of u, v, x, y, and z
 67  
 68  #print("u is of type ", type(u))
 69  #print("v is of type ", type(v))
 70  #...
 71  vartypes = {"u": u, "v": v, "x": x, "y": y, "z": z}
 72  for name, value in vartypes.items():
 73      print(f"{name} is of type ", type(value))
 74  ```
 75  
 76  __2.2__ In the code cell below:
 77  
 78  1. Cast the variable `x` defined in the code cell above to an integer
 79     and assign the resulting object to a new variable `a`.
 80     Then print the value and type of `a`
 81  2. Cast the variable `y` defined in the code cell above to a string
 82     and assign the resulting object to a new variable `b`.
 83     Then print the value and type of `b`
 84  3. Cast the variable `y` defined in the code cell above to a float
 85     and assign the resulting object to a new variable `c`.
 86     Then print the value and type of `c`
 87  
 88  ```{python}
 89  # Recast x, y, and z to three new variables a, b, and c.
 90  # Print the types and values of a, b, and c
 91  a = int(x)
 92  print("variable a has value ", a, " and type ", type(a))
 93  
 94  b = str(y)
 95  print("variable b has value ", b, " and type ", type(b))
 96  
 97  c = float(y)
 98  print("variable c has value ", c, " and type ", type(c))
 99  ```
100  
101  ## Exercise 3: Operators
102  
103  __3.1__ In the code cell below,
104  use arithmetic operators to compute the following expressions.
105  Then print the results:
106  
107  1. $1489 - 2078 + 1359$
108  2. $195 * 236 / 17$
109  3. $7.4^5$
110  4. $(348 - 63)^4 / 72$
111  
112  ```{python}
113  # Compute the expressions and print the results
114  print("1: ", 1489 - 2078 + 1359)
115  print("2: ", 195 * 236 / 17)
116  print("3: ", 7.4**5)
117  print("4: ", (348 - 63)**4 / 72)
118  ```
119  
120  __3.2__ In the code cell below,
121  use augmentation operators to do the following:
122  
123  1. Divide the variable `x` by 10
124     and overwrite it with the resulting value.
125     Then print the value of `x`
126  2. Add the string " Visualization" to the variable `y`
127     and overwrite it with resulting value.
128     Then print the value of `y`
129  3. Repeat the string variable `z` three times
130     and overwrite it with the resulting value.
131     Then print the value of `z`
132  
133  ```{python}
134  # Assign three objects to three different variables x, y, and z.
135  x = 35
136  y = "Data Science and"
137  z = "Python"
138  
139  # Change and overwrite x, y, and z. Print the values of x, y, and z
140  x /= 10
141  print("1: ", x)
142  y += " Visualization"
143  print("2: ", y)
144  z *= 3
145  print("3: ", z)
146  ```
147  
148  __3.3__ In the code cell below,
149  use Boolean operators and the function `len()`
150  to check if the following statements are true:
151  
152  1. $120 = 9 * 19 - 51$ 
153  2. $6 * 12 + 44 > 5^3$
154  3. $(-3)^2 + 5 * -3 + 6 \geq 0$
155  4. There are 28 __or__ 32 letters including spaces
156     in the phrase "Data Science and Visualization"
157  5. There are 17 letters including spaces
158     in the phrase "Boolean operators" __and__ $20 < 3.5 * 4 + 3^2$
159  
160  ```{python}
161  # Check if the statements are true
162  print("1: ", 120 == 9 * 19 - 51)
163  print("2: ", 6 * 12 + 44 > 5**3, " (", 6 * 12 + 44, " < ", 5**3, ")")
164  print("3: ", (-3)**2 + 5 * -3 + 6 >= 0)
165  phrase_len = len("Data Science and Visualization")
166  print("4: ", (28 | 32) == phrase_len, " (length is ", phrase_len, ")")
167  print("5: ", len("Boolean operators") and (20 < 3.5 * 4 + 3**2))
168  ```
169  
170  ## Exercise 4: Accumulated savings
171  
172  The the future value of an investment can be computed
173  using the formula:
174  
175  $$
176  A = P(1 + r)^t
177  $$
178  
179  where $A$ is the final amount,
180  $P$ is the initial amount,
181  $r$ is the annual interest rate,
182  and $t$ is the number of years.
183  Do the following:
184  
185  1. The initial amount is 10,000, the annual interest rate is 2.5%,
186     and you save up for 6 years.
187     Assign these values to three new variables `P1`, `r1`, and `t1`.
188     Use the formula to compute the final amount.
189     Assign the result to a new variable `A1` and print the value.
190  2. The initial amount is 15,000, the annual interest rate is 4.25%,
191     and you save up for 3 years.
192     Assign these values to three new variables `P2`, `r2`, and `t2`.
193     Use the formula to compute the final amount.
194     Assign the result to a new variable `A2` and print the value.
195  3. The initial amount is 2,500, the annual interest rate is 1.75%,
196     and you save up for 12 years.
197     Assign these values to three new variables `P3`, `r3`, and `t3`.
198     Use the formula to compute the final amount.
199     Assign the result to a new variable `A3` and print the value.
200  
201  Use the `round()` function to return the results
202  with 2 decimal places.
203  You will use the variables created in this excercise
204  in the next exercise
205  
206  ```{python}
207  # Compute and print accumulated savings under scenario 1
208  P1 = 10000
209  r1 = 2.5/100
210  t1 = 6
211  A1 = round(P1*(1+r1)**t1, 2)
212  print("A1 = ", A1)
213  # Compute and print accumulated savings under scenario 2
214  P2 = 15000
215  r2 = 4.25/100
216  t2 = 3
217  A2 = round(P2*(1+r2)**t2, 2)
218  print("A2 = ", A2)
219  # Compute and print accumulated savings under scenario 3
220  P3 = 2500
221  r3 = 1.75/100
222  t3 = 12
223  A3 = round(P3*(1+r3)**t3, 2)
224  print("A3 = ", A3)
225  ```
226  
227  ## Exercise 5: Containers
228  
229  __5.1__ Use the code cell below to do the following:
230  
231  1. Create a list with the names of the seven days of the week.
232     Assign the list to the variable `days`
233  2. Use slice notation and the indexing operator on the list `days`
234     to create two new lists `weekdays`,
235     i.e., "Monday" through "Friday",
236     and `weekend`,
237     i.e. "Saturday" and "Sunday".
238     Print the new lists
239  3. Use a [list method][datastructures]
240     to do add an extra "Sunday" to the end of the list `weekend`.
241     Then print the list
242  4. Use a list method
243     to remove the first occurence of the "Sunday"
244     from the list `weekend`.
245     Now print the list again
246  5. Use a list method
247     to reverse the order of the list `weekdays`
248     so the first element is now "Friday".
249     Then print the list again
250  
251  [datastructures]:
252   https://docs.python.org/3/tutorial/datastructures.html
253  
254  ```{python}
255  # Create a list the names of the days of the week
256  days = [
257      "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
258      "Saturday", "Sunday"
259  ]
260  # Create two list with weekdays and weekend
261  weekdays = days[0:5]
262  print("1: week days: ", weekdays)
263  weekend = days[-2:7]
264  print("2: weekend: ", weekend)
265  # Add "Sunday" to the end of the list weekend
266  weekend.append("Sunday")
267  print("3: extended weekend: ", weekend)
268  # Remove the first occurence of "Sunday" from the list weekend
269  weekend.pop(1)
270  print("4: reduced extended weekend: ", weekend)
271  # Reverse of the order of the list weekdays
272  weekdays.reverse()
273  print("5: week days reversed: ", weekdays)
274  ```
275  
276  __5.2__ Use the code cell below to do the following:
277  
278  1. Create a dictionary
279     that maps the keys "Initial amount", "Interest rate", "Years",
280     and "Final amount"
281     to the values stored in the variables `P1`, `r1`, `t1`, and `A1`
282     that you created under the first scenario in exercise 4.
283     Assign the dictionary to the new variable `S1`.
284  2. Create corresponding dictionaries
285     for the second and third scenarios in exercise 4
286     and assign them to the new variables `S2` and `S3`
287  3. Now create a [nested dicitionary]
288     that maps the keys "Scenario 1", "Scenario 2", and "Scenario 3"
289     to the dictionaries stored in the variables `S1`, `S2`, and `S3`.
290     Assign the nested dictionary to the variable `accumulated_savings`
291  4. Use the indexing operator
292     on the nested dictionary `accumulated_savings`
293     to access the final amount under scenario 2 and print it
294  
295  [nested dicitionary]:
296   https://www.geeksforgeeks.org/python/python-nested-dictionary/
297  
298  ```{python}
299  # Create a dictionary for the first scenario in exercise 4
300  S1 = {
301      "Initial amount": P1,
302      "Interest rate": r1,
303      "Years": t1,
304      "Final amount": A1,
305  }
306  # Create corresponding dictionaries
307  # for the second and third scenarios in exercise 4
308  S2 = {
309      "Initial amount": P2,
310      "Interest rate": r2,
311      "Years": t2,
312      "Final amount": A2,
313  }
314  S3 = {
315      "Initial amount": P3,
316      "Interest rate": r3,
317      "Years": t3,
318      "Final amount": A3,
319  }
320  # Create a nested dictionary
321  # of accumulated savings under the three scenarios
322  accumulated_savings = {
323      "Scenario 1": S1,
324      "Scenario 2": S2,
325      "Scenario 3": S3,
326  }
327  # Access the final amount under scenario 2 and print it
328  print("4: final amount under scenario 2: ",
329        accumulated_savings["Scenario 2"]["Final amount"])
330  ```
331  
332  ## Exercise 6: String methods
333  
334  Use the code cell below to do following:
335  
336  1. Go to the [FUTUREDEMICS] page
337     on Roskilde University's research portal.
338     Copy the URL and assign it to a new variable `URL`
339  2. Use a [string method]
340     to count the number forslashes "/" in the URL
341  3. Use the functions `len()` and `set()`
342     to count how many different unique characters the URL contains
343  4. Use a string method and the indexing operator
344     to split `URL` into two new strings:
345     One containing the base URL up to and inclduing ".dk"
346     and one containing the part of the URL
347     after the second to last forslash,
348     i.e., the title of the research project.
349     Assign the new strings
350     to the variables `base_URL` and `project_title`
351  5. Use a string method
352     to replace all hyphens "-" in `project_title` with whitespaces
353  6. Use a string method
354     to strip the string " futuredemics" from `project_title`
355  7. Use a string method
356     to put `project_title` in titlecase. Then print it
357  
358  [FUTUREDEMICS]:
359   https://forskning.ruc.dk/da/projects/nordic-pandemic-preparedness-modelling-network-futuredemics/
360  
361  [string method]:
362   https://docs.python.org/3/library/stdtypes.html#string-methods)
363  
364  ```{python}
365  # Assign the FUTUREDEMICS URL to a new variable URL
366  URL = "https://forskning.ruc.dk/"\
367  + "da/projects/"\
368  + "nordic-pandemic-preparedness-modelling-network-futuredemics/"
369  # Count the number of forslashes in URL
370  #print("2: slashes: ", URL.count("/"))
371  # Count the number of unique characters in URL
372  #print("3: unique chars: ", len(set(URL)))
373  # Create two new string variables from URL
374  URL_parts = URL.partition(".dk")
375  base_URL = "".join(URL_parts[0:2])
376  project_title = URL_parts[2].split("/")[-2]
377  #print(f"4: base URL: {base_URL}\n   project title: {project_title}")
378  # Replace hyphens with whitespace in project_title
379  project_title = project_title.replace("-", " ")
380  #print("5: project title, spaced: ", project_title)
381  # Strip the word "futuredemics" from the project tilte
382  project_title = project_title.removesuffix(" futuredemics")
383  #print("6: project title, shortened: ", project_title)
384  # Title case project_title
385  project_title = project_title.title()
386  # Print the project title
387  print(project_title)
388  ```
389  
390  ## Exercise 7: Exporting Jupyter Notebooks to PDF 
391  
392  Several possibilities for exporting Jupyter Notebooks to PDF exists.
393  Two of them are:
394  
395  1. __Print to PDF via HTML__:
396      * Select File > Save and Export Notebook As > HTML
397        in Jupyter Notebook.
398        An HTML file is saved in you Downloads folder.
399      * Open the HTML,
400        press `Ctrl + P`,
401        select Print to PDF,
402        and choose a file path
403  2. __PDF via LaTeX__:
404      1. Install necessary dependencies: 
405      * Go to <https://miktex.org/download>
406        and download and install [MiKTeX]
407      * Go to <https://pandoc.org/installing.html>
408        and download and install [Pandoc]
409      * Run `pip install nbconvert` in a code cell in Jupyter Notebook
410      * Select Kernel > Restart Kernel in Jupyter Notebook
411      2. Export to PDF:
412         Select File > Save and Export Notebook As > PDF
413         in Jupyter Notebook.
414         An PDF file is saved in you Downloads folder.
415         Open it, click save, and choose a file path
416  
417  [MiKTeX]: https://miktex.org/
418  
419  [Pandoc]: https://pandoc.org/index.html
420  
421  The advantage of the first approach is
422  that it has no dependencies,
423  i.e., you do not have to install other applications to make it work.
424  The drawback of the first approach is
425  that you have limitied control over the output.
426  For example, lines in code cell that are too long to fit the page
427  are cut off rather than shown across several lines.
428  To make the second approach work,
429  you have to install the dependencies listed.
430  Once you have done that,
431  it is fast and easy to convert to PDF via LaTeX.
432  If following the steps listed here does not enable you
433  to produce a PDF via LaTeX
434  (if you use Mac or Linux you may run into trouble),
435  I recommend that you use your favorite search engine
436  or go talk to a large language model
437  to find a solution that works on your system.
438  
439  Try exporting your notebook to PDF using both methods.
440  You will need to export a Jupyter Notebook to PDF
441  when you will eventually submit your exam mini-project on May 1.