Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PY
Python
0/14 completed
Beginner
Hello PythonVariables and typesWorking with stringsConditions and loopsFunctions
Intermediate
Lists, tuples, dicts and setsFiles and exceptionsModules and the standard library
Advanced
Classes and OOPIterators and generatorsDecorators and context managers
Pro
Async PythonTesting and type hintsVirtual environments, packages and real projects
Tracks / Python / Intermediate

Modules and the standard library

As programs grow, you split them into modules: files of related code you can import. Python also ships with a huge standard library ("batteries included"), and hundreds of thousands of third-party packages on PyPI.

Importing

Python · editable
Style When to use
import math The default: clear where each name comes from (math.sqrt)
from math import sqrt When you use a few names often
import numpy as np Long names with a well-known alias
from math import * ❌ Avoid: you can't tell where names come from

Your own modules

Any .py file is a module. Imagine a project like this:

shop/
├── main.py
└── pricing.py
python
# pricing.py
TAX_RATE = 0.18

def with_tax(price):
    return round(price * (1 + TAX_RATE), 2)
python
# main.py
from pricing import with_tax

print(with_tax(100))       # 118.0

Run it with python main.py. A folder of modules with an __init__.py file is a package: from shop.pricing import with_tax.

if name == "main"

When you run a file, its __name__ is "__main__". When it's imported, __name__ is the module's name. This lets a file be both a reusable module and a runnable script:

Python · editable

A tour of the standard library

random

Python · editable

datetime

Python · editable

collections

Python · editable

itertools and functools

Python · editable

re: regular expressions

Python · editable

More you'll use

Module For
os, pathlib, shutil Files, folders, environment variables
sys Command-line arguments, the Python version
json, csv, sqlite3 Data formats and a built-in database
logging Proper log messages instead of print
argparse Command-line tools with --flags
urllib, http Basic web requests (most people use requests or httpx)
subprocess Run other programs
unittest Testing (next lessons)
Python · editable

Third-party packages

Beyond the standard library, PyPI (pypi.org) hosts packages for nearly everything. You install them with pip, inside a virtual environment (covered in the last lesson):

bash
pip install requests
python
import requests

response = requests.get("https://api.github.com/repos/python/cpython")
print(response.json()["stargazers_count"])

Popular ones: requests/httpx (HTTP), pandas (data), numpy (maths), matplotlib (charts), fastapi/django/flask (web), pytest (testing), rich (pretty terminal output).

Practice

  1. Use random to simulate rolling two dice 1,000 times, and count how often each total (2 to 12) appears with a Counter.
  2. Use datetime to print what weekday you were born on.
  3. Use re to extract all the hashtags from "Loving #python and #coding today! #100DaysOfCode".
Python · editable
✎ Check your understanding

1.When is __name__ == "__main__" true?

2.Why avoid from module import *?

3.Which standard-library module gives you a dict with automatic default values?

← Files and exceptions