Modules, comments, and pip
Reuse code from the standard library, install external packages deliberately, and leave comments that explain decisions.
By the end, you’ll be able to…
- Import a module and use its names through a namespace.
- Distinguish built-in modules from packages installed with pip.
- Write comments that explain intent instead of restating code.
- Use the interactive prompt as a scratchpad for quick checks.
01A module is code someone already organized
A module is a reusable namespace, usually backed by a Python file. Importing one gives you access to names it defines without copying their implementation into your program. Python ships with a large standard library, so tools such as os, random, math, and pathlib are available before you install anything.
Keep the module prefix at the call site. os.listdir(folder) says where listdir came from; a bare listdir would hide that context and could collide with a local function. The prefix is not extra ceremony: it is a small piece of documentation that survives refactoring.
02Built-in and external modules need different handling
Standard-library modules are installed with Python. External packages come from an index such as PyPI and must be installed into the interpreter that will run your program. On Windows, py -m pip install requests is safer than a bare pip command because it ties the installation to the interpreter selected by the launcher.
Treat installation as a supply-chain decision. Read the package page, check the license and maintenance activity, and pin a reviewed version in your project metadata. A package name can be misspelled on purpose, and an install command copied from an untrusted page can execute code with your user permissions.
py -m pip --versionpy -m pip install requestspy -m pip show requestspy -m pip freeze > requirements.txt03Comments explain decisions, not syntax
A hash sign starts a comment that continues to the end of the line. Comments are for the reader: record a constraint, a tradeoff, a source, or a reason that is not obvious from the code. A comment that repeats the statement beneath it adds noise and will eventually drift out of date.
Triple-quoted text is a string, not a comment. At the top of a module or function it becomes a docstring, which tools can read. Use a docstring to describe a public contract and ordinary comments for local reasoning. If code is commented out, either restore it, delete it, or explain why it must remain.
04Use the prompt as a scratchpad
Starting Python without a filename opens the interactive prompt, usually shown as >>>. It is a quick place to test an expression, inspect a module, or confirm operator behavior. The prompt is not a file: statements vanish when you exit, so move anything worth keeping into a saved .py file.
Use help(name) to read built-in documentation and exit() to return to the terminal. If a command such as py hello.py produces a SyntaxError inside the prompt, you are probably typing a terminal command where Python expects an expression. Identify which tool is listening before changing the code.
py>>> 17 // 53>>> "py" * 3'pypy'>>> help(len)>>> exit()The ideas worth keeping.
- Imports give you access to namespaces instead of copied code.
- Standard-library modules ship with Python; external packages must be installed and reviewed.
- Comments should preserve intent and constraints, not narrate syntax.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
List a folder with a reason
Use the os module to print a sorted list of entries in a temporary folder, then add a comment explaining why sorting is useful for a report. Create one file in the folder first.
Explore a worked solution
import osfrom pathlib import Pathfrom tempfile import TemporaryDirectorywith TemporaryDirectory() as folder: Path(folder, "notes.txt").write_text("review", encoding="utf-8") entries = sorted(os.listdir(folder)) # Sorting makes the report deterministic across machines. print(entries)The os module supplies listdir. Sorting turns whatever order the filesystem returns into a stable report that is easier to compare and test.
Pick a study topic reproducibly
Use random.Random(4) to choose one topic from ['strings', 'loops', 'files'] and print it. Explain in a comment why a seeded generator is useful for a demonstration but unsuitable for security.
Explore a worked solution
import random# A seed makes the demo repeatable; it is not cryptographic randomness.rng = random.Random(4)print(rng.choice(["strings", "loops", "files"]))The seed fixes the sequence so a lesson, test, or screenshot is reproducible. Secrets need os.urandom or the secrets module, which are designed for unpredictability.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
List a folder with a reason
Use the os module to print a sorted list of entries in a temporary folder, then add a comment explaining why sorting is useful for a report. Create one file in the folder first.
sorted(os.listdir(folder))
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
Pause. Put the pieces together.
You’ve reached the end of Getting started with Python. Before moving on, use this task to check what you can do independently.
Change an input value, predict the calculation result, run the saved file, and confirm it produces your expected output.
Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.
Review this moduleOne lesson closer.
Reading is a start. Trying it yourself makes the connection.