This lesson covers five essential Python interview topics (26–30). Each question includes a calm, beginner-friendly explanation, real-world analogies, and runnable examples with expected output. Work through the examples in your editor to build intuition and confidence.
A Python module is a single .py file that contains Python code - functions, classes, and variables. A package is a directory that groups related modules together and usually contains an __init__.py (though modern namespace packages can omit it). Think of a module as a single chapter in a book, while a package is the chapter collection grouped into a volume.
Example: create a small package structure and show imports. File layout:
# Directory structure:
# mypkg/
# __init__.py
# math_helpers.py
# mypkg/math_helpers.py
def add(a, b):
return a + b
def mul(a, b):
return a * b
# mypkg/__init__.py
from .math_helpers import add, mul
__all__ = ["add", "mul"]Use the package:
# script.py
import mypkg
print(mypkg.add(2, 3))
print(mypkg.mul(4, 5)) Expected output:
# Output:
5
20 Virtual environments isolate project dependencies so each project can have its own packages and versions. Imagine each project as a separate workshop with its own set of tools - virtual environments keep tools (packages) from colliding across projects.
Example using Python's built-in venv:
# Create and activate a virtual environment (Unix/macOS)
python -m venv .venv
source .venv/bin/activate
# On Windows (PowerShell)
python -m venv .venv
.\.venv\Scripts\Activate.ps1
# Install a package inside the venv
pip install requests
# Verify the package is available only inside the venv
python -c "import requests; print(requests.__version__)"Expected behavior: prints the installed requests version. Deactivating the venv returns you to the system Python where that package may not exist.
pip is the standard package installer for Python - it installs packages from PyPI into the active environment. pipenv is a higher-level tool that combines package installation with virtual environment management and produces a Pipfile/Pipfile.lock for reproducible installs. Consider pip a drill, and pipenv a drill plus a dedicated toolbox and checklist for the project.
What teams actually reach for now: uv
uv (from Astral, the makers of Ruff) is a single Rust binary that replaces pip, pip-tools, virtualenv, pipenv and most of what people used poetry for - and it is typically an order of magnitude faster. Since 2024–25 it has become the default choice in a large share of Python projects, so an answer that stops at “pip and pipenv” sounds a few years out of date.
It is worth being able to say what uv actually consolidates: resolving and installing (pip), locking (pip-tools), creating environments (venv/virtualenv), running scripts in them (pipenv run), managing Python versions themselves (pyenv), and publishing (twine).
# Start a project (writes pyproject.toml)
uv init myapp && cd myapp
# Add a dependency: resolves, installs, updates uv.lock
uv add requests
# Add a dev-only dependency
uv add --dev pytest
# Run inside the managed environment - no "activate" step
uv run python -c "import requests; print('requests OK')"
# Reproduce an environment exactly from the lockfile
uv sync
# uv can also install and pin the interpreter itself
uv python install 3.13Expected output:
requests OKHow to answer this in 2026: “pip is the installer that ships with Python and still underpins everything. For projects I use uv, because it handles environments, resolution and a real lockfile in one fast tool. poetry solves the same problem and is still common in existing codebases; pipenv is largely legacy.” That shows you know the history and the current state.
Example: the older pip and pipenv workflow, which you will still meet in existing projects.
# Using pip inside an activated venv
pip install pyyaml
# Quick pipenv workflow (creates venv and Pipfile)
pipenv install requests
pipenv run python -c "import requests; print('requests OK')" Expected output for the pipenv run:
# Output:
requests OK Type hints let you annotate variables, function parameters, and return types with expected types. They don't change runtime behavior by default, but they improve readability and enable static tools (like mypy or IDEs) to catch errors earlier. Think of hints as polite signposts for humans and tools - they guide expectations without forcing the interpreter.
Example with function annotations and runtime introspection:
from typing import List, Optional
def greet(name: str, times: Optional[int] = 1) -> List[str]:
return [f"Hello, {name}!" for _ in range(times)]
# Using the function
print(greet("Alice", 2))
# Inspect annotations at runtime
print(greet.__annotations__)Expected output:
# Output:
['Hello, Alice!', 'Hello, Alice!']
{'name': <class 'str'>, 'times': typing.Optional[int], 'return': typing.List[str]} Data classes (introduced in Python 3.7) are a decorator-driven convenience to create classes mainly used for storing data. They auto-generate __init__, __repr__, __eq__, and more, reducing boilerplate. Think of a dataclass as a concise form to declare a record or structured value.
Example demonstrating common dataclass features:
from dataclasses import dataclass, field
from typing import List
@dataclass(order=True)
class Task:
priority: int
name: str
tags: List[str] = field(default_factory=list)
# Create instances
t1 = Task(2, "write tests", ["dev"])
t2 = Task(1, "update docs")
print(t1) # auto __repr__
print(t1 == t2) # auto __eq__ (False)
tasks = [t1, t2]
print(sorted(tasks))Expected output (ordering sorts by priority first):
# Output:
Task(priority=2, name='write tests', tags=['dev'])
False
[Task(priority=1, name='update docs', tags=[]), Task(priority=2, name='write tests', tags=['dev']) Closing tips: In interviews, explain why each tool helps real projects - packages help structure code, virtual environments prevent dependency conflicts, pip/pipenv manage packages and reproducible installs, type hints improve maintainability, and dataclasses reduce boilerplate for value objects. Short, practical examples - like the ones above - show you both understand the concept and can apply it.
3 questions · no sign-up, nothing stored
Progress is stored in this browser only - no sign-up, nothing sent anywhere.