Intermediate Python

Python Trainer

Modules · List Comprehensions · OOP · pip Packages · Mini Projects · Quiz

Modules — import & use standard library
Python ships with a huge standard library. import loads a module; from X import Y pulls in just what you need. No installation required — these are built in.
💡
import math vs from math import sqrt — the first loads the whole module and you access things as math.sqrt(). The second imports just that one name so you can call sqrt() directly. Both work; use from when you only need one or two things.
Explore four essential built-in modules
Python code
math_demo.py

          
Output
live result
List Comprehensions — concise list building
A list comprehension creates a new list in one line: [expression for item in iterable if condition]. It replaces 3–5 lines of loop code with one readable line.
💡
[x * 2 for x in numbers if x > 5] — read it left to right: "give me x*2, for every x in numbers, but only if x > 5." The if part is optional — leave it out to transform every item.
Scenario — sales data filtering & transformation
A sales list has raw daily revenue figures. Use list comprehensions to filter outliers, apply a discount, extract names, and convert units — all in one line each.
$100
15%
Visual — which items pass the filter?
Python code — 4 comprehensions
sales_filter.py

          
Output
live result
[ x for x in y ] if condition sum() len()
vs. a for-loop: a comprehension is faster, shorter, and considered more "Pythonic." Use it when the logic fits one line. If you need multiple steps, error handling, or the body is more than a few operations, stick with a regular loop.
OOP Basics — classes, __init__, methods
A class is a blueprint for objects. Each object (instance) has its own data (attributes) and behavior (methods). __init__ runs automatically when you create a new instance.
🏗
self is a reference to the current instance. Every method gets self as its first parameter — Python passes it automatically. self.name stores an attribute on the object; a plain name inside the method is just a local variable that disappears.
Scenario — bank account class
Build a BankAccount class with a balance, deposit/withdraw methods, and a guard against overdrafts. Create multiple accounts and call their methods independently.
$1,000
$500
$300
Class — BankAccount (blueprint)
__init__(self, owner, balance)
deposit(self, amount)
withdraw(self, amount)
get_balance(self)
Instance — account1 (object)
Python code
bank_account.py

          
Output — method calls in sequence
live result
class __init__ self methods
📌
Why OOP? Without a class, if you had 50 bank accounts you'd need 50 separate variables for each balance, owner, etc. A class packages the data and behavior together so you can create as many instances as you need, each tracking its own state independently.
pip — installing third-party packages
pip is Python's package installer. It downloads packages from PyPI (Python Package Index) — over 500,000 packages for everything from web scraping to machine learning.
⚠️
Virtual environments first. Before installing packages for a project, run python -m venv venv then activate it. This keeps your project's dependencies isolated from other projects and from the system Python. It's a professional habit worth building from day one.
Essential packages — click to see usage
Virtual environment workflow
terminal

        
requirements.txt — share your dependencies
requirements.txt

        
Mini Project — To-Do List CLI App
This project ties together everything from all three tracks: functions, lists, dicts, file I/O, error handling, and modules. It's a command-line to-do list that saves tasks to a file.
🏆
Everything from Foundations to Intermediate appears here: variables & math (Foundations) · functions, dicts, file I/O, error handling (Core Structures) · modules, list comprehensions, a class (Intermediate Python). A mini project is the best way to cement what you've learned.
Interactive demo — try the app
The full program — todo.py
todo.py

          
Terminal session
class TodoList methods comprehension os · datetime
Comprehensive Quiz — test what you know
20 questions covering Foundations, Core Structures, and Intermediate Python. Click an answer — green means correct, red means wrong. Your score is tracked at the top.
0
Score
0
Answered
0% correct