Module 1: Getting started · Lesson 1.2 · about 20 min
Just enough Python
The Python you need for pandas and nothing more: variables, numbers and text, lists and dictionaries, functions and methods, and reading error messages.
Module 1 · about 20 minutes · no Python needed. You’ll be able to read and write the small amount of Python that pandas code uses.
In everyday terms: a variable is a labelled jar: put a value in, and use the label to get it back. A list is a row of jars in order; a dictionary is a set of jars with name tags you look up by name.
1. Values and variables
A variable is a name for a value. = stores the value; the name then stands for it.
Anything after # is a comment, for humans. print() shows values; the last line’s value is shown too.
f-strings put values into text: f'{store} sold {qty} items'. Add :.2f for two decimals: f'{total:.2f}'.
2. Lists and dictionaries
A list holds values in order, in square brackets. A dictionary maps keys to values, in curly braces.
[0] is the first item (Python counts from 0), [-1] the last. pandas uses lists for “these columns” (df[['store', 'qty']]) and dictionaries for “rename this to that” (df.rename(columns={'qty': 'units'})).
3. Functions and methods
A function takes inputs in brackets: len(prices), round(3.14159, 2). A method belongs to a value and is called with a dot: 'maple'.upper(), prices.append(5.99). Inputs can be named: round(number=3.14159, ndigits=2). pandas code is mostly methods, often with named options: df.sort_values('qty', ascending=False).
You can write your own function with def:
The indented lines belong to the function; rate=0.13 is a default you can override.
4. Choices and loops (briefly)
You’ll rarely loop over rows in pandas: whole-column operations are shorter and much faster (Lesson 8.8). But it helps to read loops.
- x = 5
- Put 5 in a jar labelled x (assignment, not "equals").
- [1, 2, 3]
- A list: values in order, counted from 0.
- {'S01': 'Harbourview'}
- A dictionary: look up a value by its key.
- f(x) and x.method()
- A function takes inputs; a method is a function that belongs to a value, such as text.upper().
- Physical meaning
- Python reads top to bottom and remembers every name you created. In pandas you keep reassigning tables to names (orders, clean, monthly), so the name always points at the latest version.
”= means equal.” In Python = means “store this under that name”. Comparing uses ==. Writing if qty = 3 is an error; if qty == 3 asks the question.
Watch it happen, one step at a time:
price = 18.99 and qty = 3.total = price * qty → 56.97.stores = ['S01', 'S02', 'WEB']; stores[0] is 'S01' (counting starts at 0).names = {'S01': 'Harbourview'}; names['S01'] → 'Harbourview'.'harbourview'.title() → 'Harbourview'. pandas works the same way: orders.head().5. Your turn
How it works: reading an error
Principle: Python stops at the first problem and names it. Read the last line first: the error type and message, then the line number.
NameError: name 'totl' is not defined: a typo, or a variable made in another box.SyntaxError: a missing bracket, quote or colon.TypeError: can only concatenate str (not "int") to str: mixing text and numbers; use an f-string.KeyError: 'S09': that key (or, in pandas, column) doesn’t exist.IndentationError: the spaces at the start of a line don’t line up.
Run this box to see one, then fix it:
6. Practice
- What does
[10, 20, 30][1]give? - Write an f-string that shows
price = 4.5as$4.50. - Why does
'5' + 5fail, and how would you fix it?
Answers
- 20 (the second item; counting starts at 0).
f'${price:.2f}'.- It mixes text and a number. Use
int('5') + 5for 10, or'5' + str(5)for'55'.
New words: variable, int, float, str, list, dictionary, function, method, f-string, comment.
- = stores a value under a name; == compares.
- Lists are ordered and counted from 0; negative numbers count from the end.
- Dictionaries map keys to values.
- Methods are called with a dot: value.method().
Check yourself
Sources
- The Python Tutorial, sections 3-5 https://docs.python.org/3/tutorial/
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.