Start here¶
Welcome to Python Essentials. Over this course you write Python for one small, real job: keeping a barangay's records. You start with single values (a household's size, a barangay's name), collect them into lists and records, decide and repeat over them, wrap the steps in functions, and in the end read the records from files and write a short report from them. Everything is plain Python: no extra packages until the next course, Arrays and DataFrames.
This first notebook makes sure your laptop is ready, shows how a notebook works, and walks you through the way every exercise in the course is checked.
| section | what you do |
|---|---|
| 1 · Is your laptop ready? | run one cell that checks everything the course needs |
| 2 · How a notebook works | cells, the kernel, and why the order you run cells in matters |
| 3 · How the exercises work | your first two functions, and their checks |
import course
from course import check_cases, try_it
course.quizzes_at("1.0")
1 · Is your laptop ready?¶
Click the cell below and press Shift + Enter. It checks your Python, the course's packages, the course folder, and that the course can save files in it. Every line should say yes. If one says no, its last column says what to do.
course.readiness()
Your computer is ready.
| check | found | ready | what to do | |:--|:--|:--|:--| | Python 3.12 or newer | 3.14.10 | yes | | | the jupyterlab package | found | yes | | | the marimo package | found | yes | | | the course folder | complete | yes | | | the course can save files here | yes | yes | |
2 · How a notebook works¶
A notebook is a list of cells. This text is a Markdown cell: formatted text, for reading. The cell above it is a code cell: Python, which runs when you press Shift + Enter, and shows what its last line gives back underneath it.
Behind the notebook runs the kernel: one Python program that keeps everything you have run so far. A name you set in one cell (households = 12) exists for every cell you run after it, until you restart the kernel.
Run the next cell once.
visited = 0
visited
0
Now run the next cell three times, pressing Shift + Enter on it again each time (or clicking into it and using the ▶ button).
visited = visited + 1
visited
1
It says 3, although the cell only adds one. The kernel remembers visited from each earlier run, and the notebook does not show how many times a cell was run: the number in square brackets beside it ([5]) is only the order of the last run. This is hidden state: what the notebook holds is no longer what its cells, read from top to bottom, say.
Hidden state is the most common source of "it worked a minute ago". Two habits keep it away:
- Run cells from top to bottom, the way someone reading the notebook would.
- When a result looks wrong, use Kernel → Restart Kernel and Run All Cells: the kernel forgets everything and runs every cell once, in order. If the notebook is right after that, it is right.
Try it now: restart and run all. The cell above says 1 again.
Later in the course you meet marimo, a notebook that never has hidden state: it reruns the cells that depend on a change by itself. Module 7, Meet marimo, shows how.
3 · How the exercises work¶
Every exercise in the course is a function for you to finish. The cell below the task starts with the function's first line and a few steps written as comments. You write the code, delete the raise NotImplementedError line, and run the cell. Then:
- Try it: the next cell calls your function with one input and shows what it gives back, or explains the error it raised.
- The check: the cell after that runs your function on several inputs and compares each result with what a correct function gives. It turns green when every one matches, red with the first input that does not, and stays grey while you have not started.
The check never shows the right answer, only which input went wrong and what your function gave for it. A function has to work for any input, not just the example, so typing a result such as return "Kumusta, Ana!" passes for one input and fails for the rest.
If you are stuck after ten minutes, or your check is green and you want to compare, the check links to the exercise on the course's companion: the solution, and why it works.
Exercise 1.1 · Say hello¶
Finish greeting(name) so that it returns a greeting for that name: Kumusta, then the name, then !.
| call | returns |
|---|---|
greeting("Ana") |
"Kumusta, Ana!" |
greeting("Juan dela Cruz") |
"Kumusta, Juan dela Cruz!" |
An f-string puts a value inside a text: write f before the opening quote, and the name between braces, as in f"Hello, {name}".
def greeting(name):
# Step 1: return an f-string with the name between "Kumusta, " and "!"
raise NotImplementedError("Exercise 1.1") # delete this line when your code is ready
try_it(greeting, "Ana")
greeting('Ana'): not written yet. Write your code, delete the raise NotImplementedError line, and run the function's cell.
check_cases(
"Exercise 1.1", greeting, [(f'greeting("{n}")', (n,)) for n in ["Ana", "Juan dela Cruz", "Maria Clara", "Bong"]],
what="names", hint="Use `return`, not `print`, and keep the space after the comma.",
)
Exercise 1.1: not attempted yet. Use return, not print, and keep the space after the comma.
If the check is red: common causes for Exercise 1.1
Nonefor every name: the function prints the greeting instead of returning it. Changeprint(...)toreturn ....'Kumusta, {name}!': thefbefore the opening quote is missing, so the braces are plain text.- One character off (
'Kumusta,Ana!','Kumusta, Ana'): compare your text with the table, space and!included.
Exercise 1.2 · How far along is the count?¶
A barangay has total households, and the enumerators have visited visited of them. Finish share_visited(visited, total) so that it returns the share visited, as a percentage rounded to one decimal place.
| call | returns |
|---|---|
share_visited(45, 120) |
37.5 |
share_visited(1, 3) |
33.3 |
share_visited(0, 80) |
0.0 |
/ divides, and round(value, 1) rounds a number to one decimal place.
def share_visited(visited, total):
# Step 1: divide visited by total, and multiply by 100 to get a percentage
# Step 2: return that percentage rounded to one decimal place
raise NotImplementedError("Exercise 1.2") # delete this line when your code is ready
try_it(share_visited, 45, 120)
share_visited(45, 120): not written yet. Write your code, delete the raise NotImplementedError line, and run the function's cell.
check_cases(
"Exercise 1.2", share_visited, [(f"share_visited({v}, {t})", (v, t)) for v, t in [(45, 120), (1, 3), (0, 80), (80, 80), (7, 9)]],
what="counts", hint="Round last, after multiplying by 100.",
)
Exercise 1.2: not attempted yet. Round last, after multiplying by 100.
If the check is red: common causes for Exercise 1.2
30.0for 1 of 3: the share is rounded before it is multiplied by 100. Multiply first, then round.0.4instead of37.5: the share is not multiplied by 100.ZeroDivisionError: the inputs are the wrong way round,total / visited.
course.quizzes_at("1.3")
That is the course's rhythm¶
Every module works the same way: a short idea, a few lines to run and change, then an exercise with its check. Keep the notebook tidy as you go, run it from top to bottom, and restart and run all when something looks off.
Next: Values and names, in 10_values.ipynb.