Work with everyday data
Use numbers, text, lists, and records to organise useful information. This module has 3 lessons and takes about 1h. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 02 in the Learning LabClean text and convert numbers
Turn text from a fictional stock form into usable data.
Python separates text (str), whole numbers (int), and decimal numbers (float). The text "12" is different from the number 12. int("12") makes a whole number; float("12.5") makes a decimal.
Text methods work after a dot. strip() removes spaces at the ends, and lower() makes letters lowercase. An f-string begins with f and inserts values placed inside {braces}.
We are cleaning fictional clinic supply data, not patient information. Conversion lets us calculate quantities instead of joining text.
See it in action
# Synthetic supply form
raw_item = " GLOVES "
raw_boxes = "12"
item = raw_item.strip().lower()
boxes = int(raw_boxes)
pairs_per_box = 50
total_pairs = boxes * pairs_per_box
print(f"{item}: {total_pairs} pairs")
Clean data at the boundary, then calculate with the correct types.
Keep a sequence in a list
Summarise a small set of synthetic sensor readings.
A list stores values in order inside square brackets. Use readings[0] for the first value and readings[-1] for the last. Python starts positions at zero.
append(value) adds one value to the end. len(readings) counts values, sum(readings) adds them, and max(readings) finds the largest.
The mean is the sum divided by the count. This example uses made-up vibration readings from a test rig. A mean summarises the readings, but it does not explain why vibration changed.
See it in action
# Synthetic vibration values in mm/s
readings = [1.2, 1.5, 1.3]
readings.append(1.6)
mean_vibration = sum(readings) / len(readings)
print("First:", readings[0])
print("Peak:", max(readings))
print("Mean:", round(mean_vibration, 2))
Use a list when order matters or when several values describe the same quantity.
Describe a record with a dictionary
Build a clear record for one fictional solar installation.
A dictionary stores named values as key: value pairs inside braces. In site["panels"], the key "panels" selects the value. Lists use positions; dictionaries use meaningful keys.
Assigning site["capacity_kw"] = value adds a field or updates it. Use get("key", default) when a field may be missing.
This synthetic Kumasi site has identical panels. We multiply panel count by watts per panel to estimate rated capacity. Actual output also depends on conditions and losses.
See it in action
# Synthetic solar site record
site = {"name": "Kumasi Learning Lab", "panels": 12, "panel_w": 450}
site["capacity_kw"] = site["panels"] * site["panel_w"] / 1000
print(site["name"])
print("Rated capacity:", site["capacity_kw"], "kW")
print("Inspection:", site.get("inspection", "not recorded"))
Use dictionaries to give each value a clear role within a record.
Free, open and run by a nonprofit.
This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.