From fe1745eb3291f42c974ad81d47560221a37d7067 Mon Sep 17 00:00:00 2001 From: Kristian Rother Date: Wed, 3 Jan 2024 11:38:07 +0100 Subject: [PATCH] got rid of all warnings --- challenges/count_words.rst | 18 ++-- challenges/postcard.rst | 7 +- data_structures/dictionaries.md | 8 +- data_structures/tables.md | 8 +- first_steps/guess_the_number.rst | 2 +- first_steps/python_shell.rst | 15 ++-- reference/README.rst | 64 ------------- reference/os.rst | 9 +- structure/builtin_functions.md | 12 +-- structure/functions.md | 14 +-- tabular_data/first_letters.md | 148 ------------------------------- tabular_data/list_agg.md | 37 -------- tabular_data/parsing.md | 8 +- tabular_data/readfile.md | 12 +-- tabular_data/writing_files.md | 12 +-- 15 files changed, 58 insertions(+), 316 deletions(-) delete mode 100644 reference/README.rst delete mode 100644 tabular_data/first_letters.md delete mode 100644 tabular_data/list_agg.md diff --git a/challenges/count_words.rst b/challenges/count_words.rst index 01a1de4..d960735 100644 --- a/challenges/count_words.rst +++ b/challenges/count_words.rst @@ -35,8 +35,8 @@ have mastered this challenge. The Data -------- -You can find the full text for Herman Melville’s “Moby Dick” in the text -file :download:`mobydick.txt` and on +You can find the full text for Herman Melville's “Moby Dick” in the text +file :download:`moby_dick.txt` and on `www.gutenberg.org `__. Hints @@ -91,8 +91,8 @@ Which operations on this data type will be necessary to .. code:: python3 counter = {} - counter.setdefault(‘fish’, 0) - counter[‘fish’] += 1 + counter.setdefault('fish', 0) + counter['fish'] += 1 Hint 4: Functions @@ -157,16 +157,16 @@ How does Python sort integers, strings, tuples, and other lists? Hint 7: Did it work? ~~~~~~~~~~~~~~~~~~~~ -Where would you expect words like ‘is’, ‘the’, ‘sea’, and ‘cerebellum’ to occur? +Where would you expect words like 'is', 'the', 'sea', and 'cerebellum' to occur? Check whether the output of the program corresponds to your expectations. -Does ‘captain’ or ‘whale’ occur more often in the text? +Does 'captain' or 'whale' occur more often in the text? .. hint:: The first five places should be taken by of (6614), and (6433), a (4726), to (4625), and in (4173). - You have to check yourself whether ‘whale’ or ‘captain’ is first. - - + You have to check yourself whether 'whale' or 'captain' is first. + + Hint 8: Special characters ~~~~~~~~~~~~~~~~~~~~~~~~~~ Special and uppercase characters may be a problem when separating words. diff --git a/challenges/postcard.rst b/challenges/postcard.rst index cdc94de..c114a50 100644 --- a/challenges/postcard.rst +++ b/challenges/postcard.rst @@ -3,11 +3,6 @@ Create a Postcard **🎯 Write a program that composes a postcard from your favorite city.** -.. figure:: images/poznan.png - :alt: Welcome to Poznan - - Welcome to Poznan - Step 1: Images -------------- @@ -82,7 +77,7 @@ Paste images with Pillow: .. code:: python3 image1.paste(image2, (0, 0)) - image1.save(‘postcard.png’) + image1.save('postcard.png') - Create a postcard composed of smaller pictures - Add the horizontal bar and some text on it. diff --git a/data_structures/dictionaries.md b/data_structures/dictionaries.md index 57506d9..3f5590c 100644 --- a/data_structures/dictionaries.md +++ b/data_structures/dictionaries.md @@ -1,7 +1,7 @@ # Dictionaries -### Exercise 1: Explore +## Exercise 1: Explore Find out what each of the expressions does to the dictionary in the center. @@ -9,7 +9,7 @@ Find out what each of the expressions does to the dictionary in the center. ---- -### Exercise 2: Commands +## Exercise 2: Commands Define a dictionary: @@ -34,7 +34,7 @@ Then run the following commands and find out what they result in: ---- -### Exercise 3 +## Exercise 3 The following program allows you to travel from one city to the next. Unfortunately, it contains **three bugs**. Find and fix them. @@ -62,7 +62,7 @@ Unfortunately, it contains **three bugs**. Find and fix them. ---- -### Reflection Questions +## Reflection Questions * How can you create a dictionary? * How can you modify values in a dictionary? diff --git a/data_structures/tables.md b/data_structures/tables.md index b241cdd..f2eb209 100644 --- a/data_structures/tables.md +++ b/data_structures/tables.md @@ -24,7 +24,7 @@ In this chapter we will create and process nested lists. ---- -### Exercise 1 +## Exercise 1 Write all rows of the above table to the screen with a `for` loop. @@ -35,13 +35,13 @@ Complete the code: ---- -### Exercise 2 +## Exercise 2 Now modify the loop to output only the names. ---- -### Exercise 3 +## Exercise 3 Write all *cells* of the table to the screen with a nested `for` loop. @@ -53,7 +53,7 @@ Complete the code: ---- -### Exercise 4 +## Exercise 4 Create an empty table of 10 x 10 cells and fill them with numbers from 1 to 100. diff --git a/first_steps/guess_the_number.rst b/first_steps/guess_the_number.rst index 6e90f5c..113f49d 100644 --- a/first_steps/guess_the_number.rst +++ b/first_steps/guess_the_number.rst @@ -182,7 +182,7 @@ In the **Guess the Number** game, the player tries to guess a number that the co 5. Repeat steps 3-5 until the correct number was guessed. Example output: ---------------- +~~~~~~~~~~~~~~~ :: diff --git a/first_steps/python_shell.rst b/first_steps/python_shell.rst index acbc502..02a1dcf 100644 --- a/first_steps/python_shell.rst +++ b/first_steps/python_shell.rst @@ -8,14 +8,14 @@ Unsplash `__ In this chapter you will: -~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------- ==== ================================ area topic ==== ================================ 🚀 use Python as a pocket calculator 💡 use the data type *integer* -⚙ use arithmetical operators (+-*/) +⚙ use arithmetical operators (+-\*/) ⚙ store numbers in a variable ⚙ change the contents of a variable 🔧 use the Variable Explorer @@ -170,12 +170,11 @@ The rabbits constantly multiply. Every month, their number grows by **How many rabbits will you have in December?** -Hints: -^^^^^^ - -- assume that rabbits never die -- it is ok to calculate with fractions of rabbits -- it is ok to copy the same lines multiple times for each month +.. hint:: + + - assume that rabbits never die + - it is ok to calculate with fractions of rabbits + - it is ok to copy the same lines multiple times for each month .. |image0| image:: calculator.png diff --git a/reference/README.rst b/reference/README.rst deleted file mode 100644 index 3343623..0000000 --- a/reference/README.rst +++ /dev/null @@ -1,64 +0,0 @@ - -Basics ------- - -.. toctree:: - :maxdepth: 1 - - python_shell.rst - basics.rst - -Data Types ----------- - -.. toctree:: - :maxdepth: 1 - - data_types.rst - numbers.rst - strings.rst - tuples.rst - lists.rst - dictionaries.rst - type_conversions.rst - indexing.rst - -Input and Output ----------------- - -.. toctree:: - :maxdepth: 1 - - print.rst - string_formatting.rst - input.rst - -Reading and Writing Files -------------------------- - -.. toctree:: - :maxdepth: 1 - - reading_files.rst - writing_files.rst - os.rst - -Control Flow ------------- - -.. toctree:: - :maxdepth: 1 - - if.rst - for_loops.rst - while.rst - -Functions ---------- - -.. toctree:: - :maxdepth: 1 - - builtin_functions.rst - functions.rst - introspection.rst diff --git a/reference/os.rst b/reference/os.rst index 3aa8cc9..ed3789e 100644 --- a/reference/os.rst +++ b/reference/os.rst @@ -65,11 +65,9 @@ Overview The table lists some frequently used functions in ``os``: -================================ -================================================= +================================ ================================================= function description -================================ -================================================= +================================ ================================================= ``os.listdir(path)`` returns list of file names in ``path`` ``os.remove(path)`` removes a file ``os.getcwd()`` returns current working directory @@ -82,5 +80,4 @@ function description ``os.path.join(d1, d2, d3, ..)`` connects names by path separator ``os.environ[key]`` dictionary of environment variables ``os.system(cmd)`` executes shell command -================================ -================================================= +================================ ================================================= diff --git a/structure/builtin_functions.md b/structure/builtin_functions.md index 8c4b88e..a758ae3 100644 --- a/structure/builtin_functions.md +++ b/structure/builtin_functions.md @@ -5,7 +5,7 @@ Python 3.11 has 75 builtin functions. To start writing useful programs, knowing about 25 of them is sufficient. Here you find a few exercises where you use builtin functions to make code shorter. -### Exercise 1 +## Exercise 1 Simplify the following code using the function `sum()`: @@ -16,7 +16,7 @@ Simplify the following code using the function `sum()`: total = total + number print(total) -### Exercise 2 +## Exercise 2 Simplify the following code using the function `range()`: @@ -25,7 +25,7 @@ Simplify the following code using the function `range()`: print(i * '*') i += 1 -### Exercise 3 +## Exercise 3 Simplify the following code using the function `zip()`: @@ -40,7 +40,7 @@ Simplify the following code using the function `zip()`: i += 1 print(table) -### Exercise 4 +## Exercise 4 Simplify the following code using the function `enumerate()`: @@ -51,7 +51,7 @@ Simplify the following code using the function `enumerate()`: print(i, name) i += 1 -### Exercise 5 +## Exercise 5 Use `list(range())` to create the following lists: @@ -62,7 +62,7 @@ Use `list(range())` to create the following lists: [33, 32, 31, 30] -### Exercise 6 +## Exercise 6 On which data types does the `len()` function work? diff --git a/structure/functions.md b/structure/functions.md index 681bafb..5f1c466 100644 --- a/structure/functions.md +++ b/structure/functions.md @@ -6,7 +6,7 @@ Writing all instructions into a single sequence creates programs that are hard to debug. Here, you learn to break the code down into smaller units: **functions**. -### In this chapter you learn: +## In this chapter you learn: | Bereich | Thema | |---------|-------| @@ -18,7 +18,7 @@ Here, you learn to break the code down into smaller units: **functions**. ---- -### Exercise 1: Sum up +## Exercise 1: Sum up Write a function that calculates a sum from a list of numers. Insert into the gaps: `amount`, `data`, `def`, `return`, `calc_sum', `+=` @@ -35,7 +35,7 @@ Insert into the gaps: `amount`, `data`, `def`, `return`, `calc_sum', `+=` ---- -### Exercise 2: Mean +## Exercise 2: Mean Write a function that calculates the arithmetic mean from the following numbers: @@ -50,7 +50,7 @@ Find out what the function should deliver with `return`. ---- -### Exercise 3: Standard Deviation +## Exercise 3: Standard Deviation The following program calculates the standard deviation from a list of numbers. You would like to generalize the code, so that it can be used with other data sets. @@ -72,7 +72,7 @@ Wrap the code for the calculation – but not the data – in a function. ---- -### Exercise 4: Optional Parameters +## Exercise 4: Optional Parameters Explain the program: @@ -89,7 +89,7 @@ Explain the program: ---- -### Exercise 5: Recursion +## Exercise 5: Recursion Explain the code: @@ -107,7 +107,7 @@ Explain the code: ---- -### Reflection Questions +## Reflection Questions * Why is it useful to write functions? * What is the difference between a function and a method? diff --git a/tabular_data/first_letters.md b/tabular_data/first_letters.md deleted file mode 100644 index f9eeea8..0000000 --- a/tabular_data/first_letters.md +++ /dev/null @@ -1,148 +0,0 @@ - -# Anfangsbuchstaben zählen - -## In diesem Kapitel lernst Du: - -| Bereich | Thema | -|---------|-------| -| 🔀 | Gruppieren von Daten | -| ⚙ | Funktionen definieren | -| 💡 | Methoden von Dictionaries | -| 💡 | Die Funktion `help()` | -| 🔧 | Docstrings schreiben | -| 🐞 | Funktionen einzeln testen | - ----- - -In diesem Kapitel werden wir ein Programm schreiben, das **die häufigsten Anfangsbuchstaben** in Babynamen ermittelt. Dazu benötigen wir den Datentyp **Dictionary**. - ----- - -### Aufgabe 1 - -Dieses Flussdiagramm zeigt schematisch ein Programm zum Zählen von Anfangsbuchstaben: - -![Flussdiagramm](../images/zaehlen.png) - -Nimm an, das folgende Datei verarbeitet wird: - - :::bash - Penny,F,342 - Leonard,M,384 - Sheldon,M,164 - Stuart,M,82 - -Beantworte folgende Fragen: - -1. Was sollte nach Ausführen des Programms im Dictionary stehen? -2. Was wird ausgegeben, wenn die Datei stattdessen leer ist? -3. Durch einen Datenfehler enthält die Eingabedatei eine Leerzeile. An welcher Stelle des Flussdiagramms könnten dadurch Probleme entstehen? - ----- - -### Aufgabe 2 - -Jetzt fangen wir an, das Programm zu *implementieren*. - -Schreibe zuerst den Code für die **erste** und die **letzte** Box im Flussdiagramm. - -Im leeren Dictionary kannst Du den Wert für alle Buchstaben auf `0` setzen: - - :::python3 - data = { - 'A': 0, - 'B': 0, - ... - } - -Dies ist nicht die kürzeste Variante, aber am einfachsten zu verstehen. - -Stelle sicher, dass das Programm läuft. - ----- - -### Aufgabe 3 - -Baue als nächstes die Verarbeitung der Datei in das Programm ein. Schreibe dazu den Code für die restlichen Boxen im Flussdiagramm links, sowie die oberste Box rechts (*"nächste Zeile holen"*). - -Dazu kannst Du eine `for`-Schleife aus einem früheren Programm *"ausleihen"*. - -Stelle sicher, dass das Programm läuft. - ----- - -### Aufgabe 4 - -Kümmere Dich nun um die Box *"Anfangsbuchstabe ermitteln"*. - -Gib den in jeder Zeile ermittelten Anfangsbuchstaben aus. - -Stelle sicher, dass das Programm läuft. - ----- - -### Aufgabe 5 - -Kümmere Dich nun um die restlichen Boxen. - -Um einen Wert in einem Dictionary zu erhöhen, kannst Du folgendes Muster verwenden: - - :::python3 - data[schluessel] = data[schluessel] + 1 - -oder kürzer: - - :::python3 - data[schluessel] += 1 - -Stelle sicher, dass das Programm läuft. - ----- - -### Aufgabe 6 - -Vereinfache das Programm (das Erstellen des leeren Dictionaries), indem Du die Methode `d.setdefault()` verwendest. - ----- - -### Aufgabe 7 - -Erstelle eine Liste mit den Schlüsseln des Dictionaries und eine zweite mit den entsprechenden Werten. Verwende das Muster: - - :::python3 - for key in dictionary: - print(key, dictionary[key]) - - -Alternativ kannst Du auch mit der Methode `d.items()` alle Schlüssel-Wert-Paare ermitteln. - ----- - -### Aufgabe 8 - -Plotte die Häufigkeit der Buchstaben als Balkendiagramm. - ----- - -### Aufgabe 9 - -Zähle die Anzahl der Babys anstatt für jeden Namen nur um 1 hochzuzählen (falls Du nicht schon längst selbst darauf gekommen bist). - ----- - -### Aufgabe 10 - -Sammle die Buchstabenhäufigkeiten für **alle** Jahrgänge im Dictionary. - -Das Dictionary enthält dann für jeden Buchstaben eine Liste, z.B.: - - :::python3 - data = { - 'A': [100, 103, 107, ..], - 'B': [73, 32, 22, ..], - .. - } - -Normalisiere die Anahl, indem Du durch die Geburtenzahl teilst. - -Plotte einige Liniendiagramme, um zu sehen, ob einige Buchstaben mit der Zeit häufiger werden. diff --git a/tabular_data/list_agg.md b/tabular_data/list_agg.md deleted file mode 100644 index 6dc2bd0..0000000 --- a/tabular_data/list_agg.md +++ /dev/null @@ -1,37 +0,0 @@ - -### Exercise 2 - -What does the following program do? - - top8 = [34465, 32025, 28569, 27531, \ - 24928, 23632, 22818, 22307] - - for value in top8: - print(value) - - -### Exercise 3 - -How many babies are there in total? Write a program that calculates that number. - - -### Exercise 4 - -You have a list of the 20 most popular girls names from the year 2000: - - ['Emily', 'Hannah', 'Madison', 'Ashley', 'Sarah', - 'Alexis', 'Samantha', 'Jessica', 'Elizabeth', 'Taylor', - 'Lauren', 'Alyssa', 'Kayla', 'Abigail', 'Brianna', - 'Olivia', 'Emma', 'Megan', 'Grace', 'Victoria'] - -Write a program that prints all names starting with `'A'` or `'M'`. - - -### Exercise 6 - -Create a new list containing the sum of *California* and *New York* for each name. - - names = ["Emily", "Amy", "Penny", "Bernadette"] - california = [2269, 542, 54, 21] - new_york = [881, 179, 12, 11] - diff --git a/tabular_data/parsing.md b/tabular_data/parsing.md index 2bf9d36..13c0a5f 100644 --- a/tabular_data/parsing.md +++ b/tabular_data/parsing.md @@ -4,7 +4,7 @@ Most text files contain both text and numbers. Extracting these data fields from a file and storing them in reasonably *structured* variables is called **parsing**. To parse files, we need methods of strings, lists and **type conversions**. -### Exercise 1 +## Exercise 1 The following program collects names in a list that occur at least 10000 times. Unfortunately, the program contains **four errors**. Find and fix these. @@ -20,18 +20,18 @@ The following program collects names in a list that occur at least 10000 times. print(frequent) -### Exercise 2 +## Exercise 2 Write a program that calculates the total number of babys for the year 2015 and writes it to the screen. Compare that number with the year 1915. -### Exercise 3 +## Exercise 3 Write a program that finds the *three most frequent* names for boys and girls in a given year and writes them to the screen. **Hint:** The three most frequent names are on top of the list. -### Exercise 4 +## Exercise 4 Write a program that calculates the percentage of the 10 most frequent names for the year 2015 and writes it to the screen. diff --git a/tabular_data/readfile.md b/tabular_data/readfile.md index c16611c..4acdd1a 100644 --- a/tabular_data/readfile.md +++ b/tabular_data/readfile.md @@ -13,7 +13,7 @@ The authorities of the United States have recorded the first names of all people Download the shorter archive of baby names (not grouped by states) from [http://www.ssa.gov/oact/babynames/limits.html](http://www.ssa.gov/oact/babynames/limits.html). -### Exercise 1 +## Exercise 1 Create a text file `bigbang.txt` in a text editor, containing the following data: @@ -29,7 +29,7 @@ Create a text file `bigbang.txt` in a text editor, containing the following data -## Exercise 1: +## Exercise 2 Make the program work by inserting `close`, `line`, `bigbang.txt`, `print` into the gaps. @@ -38,12 +38,12 @@ Make the program work by inserting `close`, `line`, `bigbang.txt`, `print` into ____(line) f.____() -#### Hint: +### Hint: Depending on your editor, you may need to insert the complete path to the file. If the program does not work, a wrong file name or location are the most probable reasons. -### Exercise 3 +## Exercise 3 How many different *girls names* were there in 2015? @@ -62,11 +62,11 @@ How many different *girls names* were there in 2015? girls += 1 -### Exercise 4 +## Exercise 4 Extend the program from the previous exercise such that boys and girls names are counted separately. -### Exercise 5 +## Exercise 5 Write a program that reads lines from the file `yob2015.txt`. Identify all lines containing your name and print them to the screen. diff --git a/tabular_data/writing_files.md b/tabular_data/writing_files.md index 86750bf..ce1de34 100644 --- a/tabular_data/writing_files.md +++ b/tabular_data/writing_files.md @@ -1,14 +1,14 @@ # Writing Files -### Exercise 1: +## Exercise 1: Form pairs of Python commands and their meanings. ![file exercise](../images/files.png) -### Exercise 2: Write a file +## Exercise 2: Write a file Execute the following program. Explain what happens. @@ -20,13 +20,13 @@ Execute the following program. Explain what happens. f.close() -### Exercise 3: New lines +## Exercise 3: New lines Remove the `+ '\n'` from the program and execute it again. What happens? ---- -### Exercise 5: f-Strings +## Exercise 5: f-Strings Try the following commands in a Python shell: @@ -44,7 +44,7 @@ Try the following commands in a Python shell: ---- -### Exercise 6: Table to string +## Exercise 6: Table to string Create a single comma-separated string from the nested list in exercise 4 and print it. @@ -54,7 +54,7 @@ You can concatenate a list of strings with: ---- -### Exercise 7: Table to file +## Exercise 7: Table to file Write the formatted data from exercise 6 to a CSV file.