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: materials/03_python_data_types_and_operators.md
+58-1Lines changed: 58 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,33 @@ What are the different primitive data types in expenses_calculator.py
46
46
Why were they chosen?
47
47
Are there any other data types you recognise?
48
48
49
+
<!--
50
+
::: {.callout-answer}
51
+
1.
52
+
Strings (str)
53
+
54
+
Examples: "food", "expenses.csv", "done".
55
+
56
+
Why they were chosen: Strings are used for any textual data. In your script, they handle category names, filenames, and user input. Since Python treats strings as sequences of characters, they are perfect for labels and messages.
57
+
58
+
Floats (float)
59
+
60
+
Examples: 100.0, amount = float(parts[1]).
61
+
62
+
Why they were chosen: Floats represent real numbers with decimal points. In a financial calculator, you need precision for cents (e.g., 10.50). If you used integers, you wouldn't be able to track partial currency units.
63
+
64
+
Booleans (bool)
65
+
66
+
Examples: True (in while True:), and the result of category == "done".
67
+
68
+
Why they were chosen: Booleans represent truth values. They are the backbone of your script's logic, controlling the loop and the if/else validation checks.
69
+
70
+
2.
71
+
72
+
Dictionaries (dict) and Lists (list)
73
+
:::
74
+
-->
75
+
49
76
:::
50
77
51
78
## Python operators
@@ -237,6 +264,35 @@ Why are these data types used?
237
264
238
265
Can you try using a set to identify expense type - what is the advantage/disadvantage?
239
266
267
+
<!--
268
+
::: {.callout-answer}
269
+
1, 2.
270
+
Dictionaries (dict)
271
+
272
+
Example: thresholds = {"food": 100.0, ...}
273
+
274
+
Why it's used: Dictionaries allow for key-value mapping. In your script, the category name is the "key" and the budget limit is the "value." This is chosen because it allows for quick lookup. Instead of searching through a list to find the limit for "food," Python jumps straight to it.
275
+
276
+
Lists (list)
277
+
278
+
Example: expenses = [] and [category, amount]
279
+
280
+
Why it's used: Lists are ordered and mutable. You use a list of lists (a nested structure) to keep a running history of transactions. Since you don't know how many expenses a user will enter, the list’s ability to grow dynamically is essential.
281
+
282
+
2. Using a set to Identify Expense Types
283
+
A set is an unordered collection of unique elements. The script could define valid categories using a set like this:
Yes, you could use a set to identify the expense type.
288
+
This is fast - faster than a list. You cannot store duplicate values in a set. This is not an issue and instead beneficial for category types.
289
+
290
+
Unlike the dictionary used for thresholds, a set cannot hold an associated value. It can store the word "food," but it cannot link it to the numerical value 100.0.
291
+
The Verdict:
292
+
While the script could incorporate a set to validate category names, the Dictionary (thresholds) remains the most effective choice. It functions similarly to a set by allowing for fast membership checks of category names (the keys), but it provides the added necessity of storing the specific budget limits (the values) required for the script's calculations.
293
+
:::
294
+
-->
295
+
240
296
:::
241
297
242
298
::: {.callout-exercise}
@@ -269,7 +325,7 @@ State the best data type to use in Python:
269
325
270
326
12. You are working with text that may need to be split, joined, or manipulated.
271
327
272
-
328
+
<!--
273
329
::: {.callout-answer}
274
330
1. list — Ordered and mutable collection of names.
275
331
2. set — For fast membership tests with unique elements, order is irrelevant.
@@ -287,6 +343,7 @@ State the best data type to use in Python:
287
343
Which data type you use effects memory useage and speed of computations which may be important in larger programs.
288
344
For further reading you can investigate why sets are generally faster to find unordered elements (membership tests) than lists or tuples
Copy file name to clipboardExpand all lines: materials/05_python_conditionals.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,6 +145,47 @@ What is the different indentation doing?
145
145
Can you add an extra condition that you would like to your expense calculator?
146
146
147
147
What are the limitations of conditional statements?
148
+
149
+
<!--
150
+
::: {.callout-answer}
151
+
1. What do the conditional statements do?
152
+
153
+
In the script, the if, elif (implied by the logic flow), and else blocks act as filters and triggers:
154
+
155
+
Flow Control: The if category == "done": break statement monitors user input to decide exactly when to stop the loop and move to the saving phase.
156
+
157
+
Validation: The line if category in thresholds and amount_str... ensures the program doesn't crash if someone types "pizza" as a category or "a lot" as an amount. It acts as a gatekeeper for clean data.
158
+
159
+
Threshold Monitoring: The if current_total > thresholds[category]: block compares actual spending against set goals to trigger a specific warning message.
160
+
161
+
2. What is the different indentation doing?
162
+
163
+
In Python, indentation isn't just for neatness; it defines scope. It tells the computer which lines of code "belong" to a specific condition or loop.
164
+
165
+
Level 1 (No indent): Code that runs globally (like setting the initial thresholds).
166
+
167
+
Level 2 (One indent): Code that lives inside the while loop or the try block.
168
+
169
+
Level 3 (Two indents): Code that only runs if a specific condition is met (like the code that processes an expense only if the input was validated).
170
+
171
+
The "Nesting" Logic: You can only reach the "Total spent" calculation if you have already successfully passed through the "Is the input valid?" door. If that condition isn't met, the indented code is skipped entirely.
172
+
173
+
3. Adding a "Savings Milestone" Condition
174
+
175
+
A great addition would be an Overall Budget check. This warns the user if total spending across all categories exceeds a certain limit, even if individual categories are still under their specific thresholds.
176
+
177
+
Add:
178
+
179
+
```
180
+
# Add this near the bottom of the while loop logic
181
+
total_all_categories = sum(e[1] for e in expenses)
0 commit comments