Lesson · Getting Started
Introduction to Python
57 min readIntroduction to Python
Python is a general-purpose language that emphasizes readable code, a rich object model, and a large standard library. This lesson establishes the vocabulary you will use throughout the course.
Learning goals
- Describe the two-stage pipeline CPython runs when you execute a
.pyfile: source, bytecode, evaluation loop. - Use the REPL and a script file for the same snippet, and explain when each is the right tool.
- Explain why indentation is syntax in Python rather than decoration.
- Predict what happens when incompatible types meet, for example
1 + "1", and read the resultingTypeError. - Identify the objects behind common values with
typeandisinstance.
Mental model
Read errors through the same pipeline: syntax errors happen at the compile step before anything runs, while runtime errors happen while the evaluation loop executes bytecode. Predicting which stage a failure belongs to tells you where to look first.
Mental model
read it top to bottom
source (your .py file) ↓ compile step: parse and emit bytecode bytecode ↓ evaluation loop output and side effects
Core idea
Names are references to objects, and the interpreter is a pipeline: it compiles your source to bytecode, then an evaluation loop executes that bytecode against a runtime full of objects. Everything else in this lesson (implementations, source files, the REPL, indentation, dynamic and strong typing) is vocabulary attached to those two mechanisms.
What Python is
Python is a high-level, general-purpose programming language with automatic memory management and a strong emphasis on readable source code. The language specification is separate from any one implementation.
Where Python is used
Python appears in automation, command-line tools, web services, data processing, testing, scientific work, orchestration, education, and embedded scripting. The same core language is reused across these domains.
Interpreted language model
Python is commonly described as interpreted, but CPython first compiles source to bytecode and then executes that bytecode in its evaluation loop. The important point is that normal Python development does not require a separate ahead-of-time compile step.
Python implementations
CPython is the reference implementation and the one most users run. Other implementations, such as PyPy, can make different performance and runtime choices while targeting the same language.
Python source files
A Python source file is Unicode text containing Python syntax, conventionally stored with a .py suffix. The runtime parses this source before it can execute it.
REPL vs script execution
The REPL is ideal for small experiments because it evaluates one interaction at a time and displays expression results. A script is reproducible source saved in a file, which makes it suitable for testing, version control, packaging, and repeatable execution.
Readability and indentation
Indentation is syntactic in Python: it defines suites such as function bodies and loop bodies. Consistent four-space indentation also makes the visual structure match the parser's structure.
Dynamic typing
Names are not permanently declared with one runtime type. The type belongs to the object, and a name can be rebound to another object later.
Strong typing
Python does not freely coerce unrelated values to make an operation succeed. For example, adding an integer to a string raises a TypeError instead of guessing the desired conversion.
Python's object-oriented nature
Python's runtime is deeply object-oriented: numbers, strings, functions, classes, modules, and exceptions are all objects with types and behavior. You can use object-oriented design without forcing every program into a class hierarchy.
Standard library philosophy
Python ships with a broad standard library for files, data formats, networking, testing, concurrency, compression, introspection, and more. Learn to check the standard library before adding a third-party dependency.
Walkthrough
sys.versionis the running build's version string;split()[0]keeps only the number, so the first line prints something like3.14.0instead of the full build details.sys.implementation.namereports which implementation is executing the file, usuallycpython. The same source under another implementation can change this line without changing your code.value = 42binds the namevalueto anintobject;type(value)returns theintclass itself, so the last line prints the object and its type side by side.- Predict before running: decide whether
type(value)prints a value or a type, and what exactlysys.version.split()[0]removes.
Experiments to try
- Run the snippet once in the REPL and once from a saved file. Compare what the REPL echoes automatically with what the script prints on its own.
- Print
sys.version_infonext tosys.versionand check whethersys.version_info >= (3, 14)guards the version you are actually running. - Trigger a
TypeErrorwith1 + "1", then repair it twice, once withint()and once withstr(). Decide which repair states your intent better. - Ask the REPL for help with
help(sys.version_info)and name one thing the docstring tells you that the raw output does not.
Common mistakes
- Treating the REPL and a script file as interchangeable: REPL state disappears when you close it, while a script is reproducible source you can run, test, and version.
- Believing Python has no compile step because it is called interpreted: CPython still compiles source to bytecode, and syntax errors surface there before any code runs.
- Writing
type(x) == intwhenisinstance(x, int)is meant: the equality check compares the exact type object and rejects subclasses. - Expecting an operation to coerce types, as in
"1" + 1. Python is strongly typed and raises instead of guessing the conversion you wanted.
Practice: Easy
- Save the worked example as
probe.pyand run it twice: withpython probe.pyand by pasting the same lines into the REPL. Record one behavioral difference between the two runs. - In the REPL, bind
value = 42and printtype(value); rebindvalue = "42"and print the type again. Explain in one sentence what changed, given that the name stayed the same. - Trigger two errors on purpose: a syntax error (leave out a colon) and a runtime
TypeError(add an int to a string). Note which one appears before any output and connect each to the stage where it happens.
Practice: Medium
- Grow the example into
environment.pythat prints the version, implementation name, and platform, guarding anything version-specific withsys.version_info. Annotate which lines would fail on an older interpreter. - Write the same environment report twice: as a module with a
describe()function and as a singlepython -ccommand. Argue from the pipeline in the mental model which form belongs in a repository and which is a shell habit. - Break strong typing deliberately with
"total: " + 5, then repair it once withstr(5)and once with an f-string. Compare the two repairs for clarity and pick one to standardize on.
Practice: Hard
- Design a reusable
probepackage: a module exposingdescribe_environment()plus a__main__entry point that prints a short report. Define the returned keys, the failure behavior when a value is missing, and one test before implementing. - Build a prediction lab around four expressions of your choice: one valid, one boundary case, one type error, and one name error. Write predictions first, run them in both the REPL and a script, and explain every mismatch using the compile step and the evaluation loop.
- Connect this lesson to the modules and packaging module: explain how
sys.pathdecides which interpreter and site packages your script sees, and describe a concrete failure caused by two projects disagreeing about that environment.
Lesson checklist
- I can describe the source, bytecode, evaluation loop pipeline in one sentence.
- I can run the same snippet in the REPL and as a script, and say when each is appropriate.
- I can predict whether a mistake will surface as a syntax error or a runtime error before I run it.
- I can explain why
1 + "1"fails and show two different repairs.
Remember
Python's everyday experience hides a two-stage engine: your source compiles to bytecode, and an evaluation loop executes it while names point at objects. Indentation is syntax, and strong typing raises errors instead of guessing conversions. Keep the environment probe from the practice section nearby, because later lessons assume you know which interpreter you are running.
Interview angles
What is the main purpose of **What Python is**?+
Python is a high-level, general-purpose programming language with automatic memory management and a strong emphasis on readable source code. The language specification is separate from any one implementation.
What should you pay attention to when using **Where Python is used**?+
Python appears in automation, command-line tools, web services, data processing, testing, scientific work, orchestration, education, and embedded scripting. The same core language is reused across these domains.
How does **Interpreted language model** fit into this lesson?+
Python is commonly described as interpreted, but CPython first compiles source to bytecode and then executes that bytecode in its evaluation loop. The important point is that normal Python development does not require a separate ahead-of-time compile step.
Why is `type(value) == int` usually the wrong check, and what should replace it?+
type returns the exact type object, so the equality test rejects subclasses and proxy types; isinstance(value, int) asks whether the object is an int or behaves like one and is the intended check.