Chapter 01 of 24 | Python

What Python is and how a program runs

Python is a general-purpose programming language used for scripts, web services, data analysis, automation, and education. It is designed to make programs readable, but readable syntax does not mean the computer guesses what you intended: every expression and value still follows precise rules.

Source code and the interpreter

Save instructions in a text file ending in .py. A Python implementation reads the source, parses its syntax, creates executable instructions, and runs them. In CPython, the most widely used implementation, source is compiled to bytecode and executed by a virtual machine. Other implementations may use different internal techniques while preserving Python's language behavior.

print("Python is running")
answer = 6 * 7
print(answer)

Run the file from a terminal with python hello.py, or use python3 hello.py if that is the command on your system. Check the version with python --version. This course uses modern Python 3; some advanced examples note a minimum version when they require newer features.

Statements, expressions, and indentation

An expression produces a value: 6 * 7 evaluates to 42. An assignment statement binds a name to an object, and a call such as print(...) performs an action. Unlike languages that mark blocks with braces, Python uses indentation after a colon. Consistent four-space indentation is the usual convention.

temperature = 22
if temperature > 20:
    print("Warm")
    print("Wear light clothes")
print("This line is outside the if block")

Indentation is part of the program's structure, not visual decoration. Mixing tabs and spaces or indenting a statement inconsistently can produce an error or change which branch owns that statement.

Names refer to objects

A useful first model is that a variable name refers to an object. Python is dynamically typed: the object has a type, while a name can later be rebound to a different object. This does not mean Python values have no types. The interpreter checks which operations are valid for the objects involved.

value = 10
value = "ten"  # the name now refers to a string object
print(value.upper())

Use descriptive names such as total_minutes rather than x when the meaning matters. Python conventions use lowercase words separated by underscores for variables and functions.

Beginner-to-master progression

Exercises

  1. Create a script that prints your name, a calculation, and the Python version using import sys; print(sys.version).
  2. Change the indentation of one line in the conditional example and explain how the program structure changes.
  3. Compare CPython with another Python implementation. Record one internal difference without treating it as a language rule.

Key idea: Python source is executed by an implementation, names refer to typed objects, and indentation communicates block structure.

What happens between a line of source and an effect?

For print(6 * 7), the interpreter first recognizes a call expression and a multiplication expression. It evaluates the arguments, obtains the integer result, resolves the name print, and invokes that callable. The callable writes a textual representation to a stream. This mental trace helps distinguish a parse-time syntax problem from a runtime problem such as looking up a name that does not exist.

print("before")
answer = 6 * 7
print("answer:", answer)
missing_name  # NameError happens only when execution reaches this line

Python generally executes statements in order, but expressions can call functions with side effects. An exception interrupts ordinary flow and begins stack unwinding. Later statements in the same block are skipped unless the exception is handled. This explains why a traceback reports a chain of calls rather than only the original source line.

Interactive execution and scripts

The interactive interpreter evaluates input immediately and is useful for short experiments. A script stores a repeatable sequence of instructions. An IDE may run a file with a different working directory, interpreter, or environment than a terminal. When imports or files behave unexpectedly, inspect sys.executable, sys.path, and Path.cwd() so you know which runtime and directory the program actually uses.

import sys
from pathlib import Path

print("Interpreter:", sys.executable)
print("Working directory:", Path.cwd())

Version and implementation discipline

Python's language evolves through accepted proposals and releases. A feature documented for a recent version may not exist on an older interpreter. Check the project's supported version and test it explicitly; avoid relying on whatever interpreter name happens to resolve first on a machine. Implementations such as CPython, PyPy, and other runtimes have different execution and memory strategies. Measure and describe the implementation when making performance claims.

Source code can be parsed and compiled to bytecode, but bytecode format is an implementation detail and may change between releases. Do not distribute cached bytecode as though it were portable source. Distribute source or supported packaging artifacts and let the target interpreter handle execution.

Mastery checkpoint

  1. Trace evaluation of a function call that contains arithmetic and explain where an exception could occur.
  2. Record interpreter executable, Python version, current directory, and import path for a reproducibility report.
  3. Explain the difference between a language guarantee, a CPython implementation detail, and an observation from one benchmark.
0% read