Module 1 · Python for Data Analytics
Python and Colab for analysts
Why analysts use Python, how a Colab notebook works, and the building blocks you'll use in every analysis: values, variables, types, maths and f-strings.
About 25 minutes
The problem
Kolanut Distribution's analyst, Bisi, rebuilds the same sales report every Monday. She downloads three CSV files, pastes them into Excel, fixes the dates, adds a revenue column, rebuilds two pivot tables and copies the numbers into an email. It takes her most of the morning, and twice this year a pasted range was one row short and the totals were wrong.
Python solves both problems. You write the steps once, as code, and run them again every Monday in seconds. The code is also a record of exactly what you did, so anyone can check it, and a mistake fixed once stays fixed.
This course teaches the Python an analyst actually uses: enough of the language to read and write it confidently, then pandas, the library for tables of data, from loading files to a finished analysis with charts.
The concept
Where you'll write Python: Google Colab
Colab is a free notebook that runs in your browser on Google's computers. Nothing to install, and pandas and the charting libraries are already there. Open colab.research.google.com, sign in with a Google account and choose New notebook.
A notebook is a list of cells:
- Code cells hold Python. Click inside one and press Shift + Enter to run it and move to the next.
- Text cells hold notes, written in Markdown. Use them to explain what you did and what you found: an analysis without explanation isn't finished.
Cells share memory: a variable created in one cell is available in every cell you run afterwards. If you restart the notebook (Runtime → Restart session) that memory is wiped, and you run the cells again from the top. Runtime → Run all does that in one go.
Values and variables
A variable is a name for a value. You create it with =:
quantity = 14
unit_price = 18600
customer = "Ada Superstore"Names use lowercase letters, numbers and underscores, and can't start with a number. Choose names that say what the value is: unit_price, not x.
The four types you'll meet most
| Type | Example | What it's for |
|---|---|---|
int (whole number) | 14 | Counts, IDs, quantities |
float (decimal number) | 0.05 | Prices with kobo, rates, averages |
str (text, a "string") | "Lagos" | Names, categories, codes |
bool (true/false) | True | Results of comparisons |
type(value) tells you which one you have. Types matter because they decide what you can do: "14" * 2 gives "1414" (text repeated), while 14 * 2 gives 28. Most "my numbers won't add up" problems in data work are really type problems.
Maths
+ - * / work as you'd expect. ** is "to the power of", // divides and drops the remainder, % gives the remainder, and round(x, 2) rounds to 2 decimal places. Brackets control the order, exactly as in a spreadsheet formula.
f-strings: putting numbers into sentences
Put an f before the quotes and anything inside { } is worked out and inserted. After a colon you can say how to format it: :, adds thousands separators, :.1f shows one decimal place, :.1% shows a percentage.
revenue = 247380.0
print(f"Revenue: ₦{revenue:,.0f}")
print(f"Discount rate: {0.05:.0%}")That prints Revenue: ₦247,380 and Discount rate: 5%.
Example
Take an order line like the ones in Kolanut's data: 14 cartons of orange juice at ₦18,600 a carton with a 5% discount. Work out its revenue, and the money the discount cost:
quantity = 14
unit_price = 18600
discount_pct = 5
gross = quantity * unit_price
revenue = gross * (1 - discount_pct / 100)
discount_cost = gross - revenue
print(f"Gross value: ₦{gross:,.0f}")
print(f"Discount cost: ₦{discount_cost:,.0f}")
print(f"Revenue: ₦{revenue:,.0f}")Gross value: ₦260,400
Discount cost: ₦13,020
Revenue: ₦247,380The same formula, quantity × unit_price × (1 − discount_pct ÷ 100), is how revenue is worked out for every order line in this course. In lesson 5 you'll apply it to all 4,266 lines at once.
Walkthrough
- Open a new Colab notebook and rename it
Python for analysts - lesson 1(click the name at the top). - Add a text cell at the top with a heading:
# Lesson 1: Python basics. Text cells use Markdown, so#makes a heading. - In a code cell, create the variables
quantity,unit_priceanddiscount_pctfrom the Example, and run it with Shift + Enter. - In the next cell, calculate
gross,revenueanddiscount_cost, and print them with f-strings. - Run
type(quantity),type(revenue)andtype("Lagos")in separate cells and read the results. - Try the type trap: run
"14" * 2, thenint("14") * 2.int()converts text to a whole number;float()andstr()convert to the other types. - Choose Runtime → Restart session, then run just the last cell. You'll get a
NameError: after a restart, the variables are gone until you run their cells again. Use Runtime → Run all.
Practice
Do these in your notebook, then type the result here.
Practice
A supermarket orders 25 cartons of body lotion at ₦24,600 each with a 10% discount. What is the revenue of that order line, in naira?
Practice
Kolanut adds 7.5% VAT on top of revenue when it invoices. What is the VAT on that same order line? Round to the nearest naira.
Practice
What does print(f"{1234567.891:,.1f}") print? Type it exactly.
Challenge
Challenge · optional
What is the result of 7 // 2 + 7 % 2 * 10? Work it out first, then check in Colab.
Check your understanding
Answer every question to check.