7.1 Object-Oriented Programming - A Gentle Introduction
You do not need to master Object-Oriented Programming (OOP) before your course - but you need to understand it well enough to read library documentation and use classes confidently. Every pandas DataFrame, every scikit-learn model and every Airflow DAG is a class instance.
What is a class?
A class is a blueprint for creating objects. An object (or instance) is a concrete thing built from that blueprint. Think of a class as a form template and an object as a filled-in form.
class Student:
def __init__(self, name, age, programme):
"""Initialise a new Student object."""
self.name = name
self.age = age
self.programme = programme
self.scores = []
def add_score(self, score):
"""Add a score to the student's record."""
self.scores.append(score)
def average_score(self):
"""Return the average score, or None if no scores recorded."""
if not self.scores:
return None
return round(sum(self.scores) / len(self.scores), 2)
def __str__(self):
"""Return a human-readable string representation."""
avg = self.average_score()
avg_str = str(avg) if avg is not None else "No scores yet"
return f"{self.name} ({self.programme}) | Avg: {avg_str}"Creating and using objects
alice = Student("Alice", 28, "Data Analytics")
bob = Student("Bob", 34, "Data Engineering")
alice.add_score(85)
alice.add_score(92)
alice.add_score(78)
bob.add_score(91)
print(alice) # Alice (Data Analytics) | Avg: 85.0
print(bob) # Bob (Data Engineering) | Avg: 91.0
print(alice.name) # Alice - accessing an attribute
print(alice.average_score()) # 85.0 - calling a methodKey terms
| Term | Meaning |
|---|---|
| class | The blueprint |
| Instance | An object created from a class: alice = Student(...) |
__init__ | The constructor - runs automatically when an object is created |
self | A reference to the current object - always the first parameter of instance methods |
| Attribute | A variable belonging to an object: self.name |
| Method | A function belonging to a class: add_score() |
__str__ | A special method that defines how an object prints |
Why this matters for data work
When you write df = pd.read_csv("data.csv"), you are creating an instance of the DataFrame class. When you write df.head(), you are calling a method. When you write model.fit(X, y), you are calling a method on a scikit-learn estimator. OOP is the structure that makes all of these libraries work. You do not need to build classes every day - but recognising the pattern makes reading documentation dramatically easier.
7.2 pip and Virtual Environments
pip - Python's package installer
Python's standard library is powerful, but the data ecosystem lives in third-party packages. pip is the tool that installs them.
pip install pandas # install a package
pip install pandas==2.1.0 # install a specific version
pip install pandas numpy matplotlib # install multiple packages
pip uninstall pandas # remove a package
pip list # list all installed packages
pip freeze # list installed packages in requirements format
pip freeze > requirements.txt # save your environment's packages to a file
pip install -r requirements.txt # install from a requirements fileVirtual environments
A virtual environment is an isolated Python installation for a specific project. This prevents packages from one project conflicting with another.
# Create a virtual environment
python -m venv myenv
# Activate it
# Windows:
myenv\Scripts\activate
# macOS / Linux:
source myenv/bin/activate
# Your terminal prompt will change to show (myenv)
# Now pip install goes into this environment only
# Deactivate when done
deactivateAlways create a virtual environment for each new project. This is standard professional practice.
7.3 A First Look at Key Libraries
Here is a brief, honest preview of the libraries you will master in your course. Do not try to learn these now - just get a feel for how the Python you know translates into powerful tools.
pandas - Data Analytics & Data Engineering
import pandas as pd
df = pd.read_csv("students.csv") # load a CSV into a DataFrame
print(df.head()) # first 5 rows
print(df.describe()) # summary statistics
print(df["score"].mean()) # average score
filtered = df[df["score"] >= 70] # filter rows
df["grade"] = df["score"].apply(lambda s: "Pass" if s >= 50 else "Fail")matplotlib - Data Analytics
import matplotlib.pyplot as plt
scores = [85, 92, 78, 95, 88, 72, 96, 81]
plt.hist(scores, bins=5, color="steelblue", edgecolor="white")
plt.title("Score Distribution")
plt.xlabel("Score")
plt.ylabel("Frequency")
plt.savefig("scores.png")
plt.show()numpy - Data Science & AI
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr * 2) # [2 4 6 8 10] - vectorised operation
print(np.mean(arr)) # 3.0
print(np.std(arr)) # 1.4142...
matrix = np.zeros((3, 3)) # 3x3 matrix of zerosrequests - AI Workflow
import requests
response = requests.get("https://api.example.com/data")
if response.status_code == 200:
data = response.json()
print(data)7.4 Capstone Project - Student Score Analyser
This project combines everything from Days 1–6. Build it step by step. Do not look at the solution until you have made a genuine attempt.
Specification
You will build a command-line programme that:
- Reads student data from a CSV file (
students_data.csv) with columns:name,subject,score. - Validates the data - skips rows where the score is not a valid number between 0 and 100.
- Computes the following statistics for each student (a student may have multiple subject scores):
- Average score
- Highest score
- Lowest score
- Grade (A ≥ 90, B ≥ 75, C ≥ 60, D ≥ 40, F < 40) based on their average
- Displays a formatted report to the terminal.
- Saves the report to a file called
report.txt.
Sample input file (students_data.csv)
name,subject,score
Alice,Maths,88
Alice,Python,95
Alice,Statistics,82
Bob,Maths,72
Bob,Python,68
Bob,Statistics,75
Charlie,Maths,55
Charlie,Python,invalid
Charlie,Statistics,48
Diana,Maths,92
Diana,Python,97
Diana,Statistics,94Expected output
==================================================
STUDENT SCORE ANALYSER REPORT
==================================================
Student Avg High Low Grade
--------------------------------------------------
Alice 88.3 95 82 B
Bob 71.7 75 68 C
Charlie 51.5 55 48 D [1 invalid score skipped]
Diana 94.3 97 92 A
--------------------------------------------------
Class average : 76.5
Top student : Diana (94.3)
==================================================
Report saved to: report.txtHints:
- Use a dictionary of dictionaries to group scores by student name.
- Wrap score conversion in a
try/except ValueErrorto handle invalid values. - Write a
get_grade(average)function that returns the letter grade. - Build the report as a list of strings, then join with
"\n"to write to the file and print to screen.
7.5 What Comes Next - Your Course Roadmap
Completing this 7-day course means you now have the foundations. Here is how they connect to your programme:
Week 1 of your course
In Week 1, you will no longer be learning Python syntax - you will be using Python as a tool. Instructors will assume you know:
- Variables, types and basic operators ✅ Day 1–2
- Conditions and loops ✅ Day 3
- Lists and dictionaries ✅ Day 4
- How to write and call functions ✅ Day 5
- How to read a file and handle an error ✅ Day 6
The progression by programme
| Programme | First new tool | Builds on |
|---|---|---|
| Data Analytics | pandas, matplotlib | Days 4, 5, 6 |
| Data Science & AI | numpy, scikit-learn | Day 5 (functions), Day 7 (OOP) |
| Data Engineering | sqlalchemy, pyspark | Days 4, 6 (files, modules) |
| AI Workflow | requests, openai | Days 5, 6 (functions, modules) |
Recommended practice before Week 1
- Re-do the exercises from memory, without looking at the solutions.
- Extend the capstone project: add sorting by grade, a subject breakdown, or ask the user which file to load.
- Explore one library from your programme - install it with
pipand run the official “Getting Started” example. - Read Python style guidelines: search for PEP 8 - the official Python style guide. Your code quality will thank you.
📌 Day 7 Summary
| Concept | What you learned |
|---|---|
class | Define a blueprint for objects |
__init__ | Constructor - runs on object creation |
self | Reference to the current instance |
| Attributes & methods | Data and behaviour belonging to an object |
__str__ | Controls how an object is printed |
pip | Install third-party packages |
| Virtual environments | Isolate project dependencies |
| Library previews | pandas, numpy, matplotlib, requests |
| Capstone project | Full programme combining all 7 days |
🎓 Course Complete - Full Summary
| Day | Core topic | Key skills acquired |
|---|---|---|
| 1 | Variables & types | print, int, float, str, bool, None |
| 2 | Strings & operators | f-strings, arithmetic, comparison, input() |
| 3 | Control flow | if/elif/else, while, for, range(), break |
| 4 | Collections | Lists, tuples, dicts, sets, comprehensions |
| 5 | Functions | def, return, default args, *args, lambda, scope |
| 6 | Files & errors | open(), with, try/except, import, stdlib |
| 7 | OOP & ecosystem | Classes, pip, virtual envs, library overview |
You are ready. The Python you have learned this week is the same Python used by data engineers at FAANG companies, data scientists publishing research, and analysts at every level of industry. The syntax is the same - the difference is what you build with it. That is what your course is for.
See you in class.
© Luxley Digital College - Python Foundations Pre-Course · All rights reserved