Python is a high-level, interpreted, dynamically-typed programming language known for readable syntax and a "batteries included" standard library. This chapter covers what Python is, how it runs, and how to write and execute your first programs.
When you run a .py file, CPython (the reference implementation) compiles your source into bytecode, then executes that bytecode on the Python Virtual Machine (PVM). This is why Python is often called "interpreted" even though a compilation step happens internally.
flowchart LR
A[source.py] --> B[Compiler]
B --> C[Bytecode .pyc]
C --> D[Python Virtual Machine]
D --> E[Output]
Key characteristics:
- Dynamically typed — variable types are checked at runtime, not compile time.
- Interpreted — no separate manual compilation step for the developer.
- Indentation-based — blocks are defined by whitespace, not braces.
- Multi-paradigm — supports procedural, object-oriented, and functional styles.
# This is a commentprint("Hello, World!") # function call# Statements end at the newline, no semicolon requiredx=5y=10print(x+y)See src/01_python_basics/hello_world.py and src/01_python_basics/comments.py for runnable code.
print()is a built-in function that writes to standard output.- Python statements are executed top-to-bottom; there's no
main()requirement, though larger programs conventionally use anif __name__ == "__main__":guard (introduced properly in Chapter 07: Modules). - The
#character starts a comment; everything after it on that line is ignored by the interpreter.
- Save files with the
.pyextension and usesnake_casefilenames. - Use
python3explicitly on systems wherepythonmay point to Python 2. - Keep one statement per line — Python allows
;-separated statements on one line, but it hurts readability. - Follow PEP 8 from day one; it becomes muscle memory faster than you'd expect.
| Mistake | Why it's a problem | Fix |
|---|---|---|
| Mixing tabs and spaces | Causes TabError or silent misalignment | Configure your editor to insert spaces only |
Forgetting print() parentheses (Python 2 habit) | print "x" is a SyntaxError in Python 3 | Always use print(x) |
Assuming python = Python 3 | Some systems still alias python to Python 2 | Use python3 or check python --version |
- What's the difference between a compiled and an interpreted language, and where does Python sit?
- What is CPython, and how does it relate to "Python"?
- Why does Python use indentation instead of braces?
- Write a script that prints your name, your favorite language feature, and today's date (hardcoded is fine for now).
- Modify
hello_world.pyto print three lines using three separateprint()calls, then again using a single call with\n. - Add three different types of comments (line comment, inline comment, block comment) to a script.