Skip to content
Development

What Is Python? A Practical Introduction to the Language

Follow a small stock-report script from data to output.

What Is Python? A Practical Introduction to the Language

Key takeaways

  • Python is a general-purpose programming language used for automation, applications, and data work.
  • Indentation defines code blocks, making whitespace part of the program's structure.
  • Values have types, but ordinary Python execution does not enforce type hints.
  • Virtual environments separate project dependencies; the standard library ships with Python.

Python is a general-purpose programming language. You can use it to automate repetitive work, build server applications, process files, or analyze data.

Its syntax makes many small programs straightforward to read. That does not remove the need to understand the problem: you still decide what information enters the program, how it changes, and what should happen when it is missing or invalid.

Consider a shop that wants a daily list of products running low. A short Python script can express that rule without a spreadsheet full of repeated manual checks.

A Python program you can follow

Save this as stock_report.py:

def needs_restock(quantity: int, minimum: int = 5) -> bool:
    return quantity < minimum


stock = {
    "notebooks": 3,
    "pens": 12,
    "folders": 0,
}

for product, quantity in stock.items():
    if needs_restock(quantity):
        print(f"{product}: reorder ({quantity} remaining)")

Run it with a Python 3 interpreter. Depending on your installation, the command may be python stock_report.py, python3 stock_report.py, or, on Windows, py stock_report.py.

The output is:

notebooks: reorder (3 remaining)
folders: reorder (0 remaining)

The dictionary maps product names to quantities. The loop visits each pair. The function returns whether a quantity is below the threshold, and the conditional prints only the matching products.

Change the notebook quantity to 8 and its line disappears. That is a useful first experiment: change one input and predict the effect before running the program.

Why indentation matters

The indented lines belong to the preceding function, loop, or conditional. Moving print outside the if changes when it runs.

Use consistent indentation, conventionally four spaces per level. Do not mix tabs and spaces. A colon introduces these blocks, but indentation determines their extent.

Python supports familiar building blocks including numbers, strings, lists, dictionaries, functions, and classes. Start with the structures that fit your data; this report needs no class hierarchy. The official Python tutorial develops these concepts through progressively larger examples.

Dynamic typing and type hints

The annotations quantity: int and -> bool document expected types. An editor or static type checker can use them to spot mistakes.

They do not automatically validate runtime input. Calling needs_restock("three") attempts to compare a string with an integer and raises a TypeError. The annotation itself does not reject the argument before the function executes. Python’s typing documentation makes this distinction explicit.

When quantities arrive from a file or an API, validate and convert them at that boundary. A missing value, a negative count, and the text "unknown" need deliberate handling rather than an assumption that the data matches a hint.

Modules, packages, and environments

As the report grows, put reusable functions in a module: a Python file that other code can import. A package groups related modules.

Python includes a standard library for common work. For example, csv reads tabular files and json handles JSON data. Third-party packages add capabilities beyond that library.

A virtual environment gives a project its own environment for installed packages. Create one with the interpreter command appropriate to your installation:

python -m venv .venv

On Windows, you can address that environment directly without activating it:

.venv\Scripts\python.exe stock_report.py
.venv\Scripts\python.exe -m pip --version

On macOS or Linux, the interpreter is typically .venv/bin/python. The virtual environments tutorial explains activation and package management. An environment separates dependencies; it is not a security sandbox.

Is Python interpreted or compiled?

Python is commonly described as interpreted because you usually run source through an interpreter without a separate manual build step. CPython, the widely used implementation, compiles source to bytecode for execution. “Interpreted” does not mean the program simply reads and executes each source line independently.

For the stock report, that distinction matters less than selecting the intended interpreter and dependencies. If a package is installed but cannot be imported, first check whether installation and execution used the same environment.

Where Python fits

Python suits automation, backend services, scientific computing, and machine learning. Its ecosystem can save substantial implementation work.

CPU-intensive loops written in Python may be slower than compiled native code. Data libraries often delegate expensive operations to optimized implementations. Performance depends on the workload, so measure the part that matters before rewriting it.

For a first project, extend the stock report to read a CSV file and produce a separate reorder file. That introduces input validation, file handling, and reusable functions while keeping the result easy to inspect.

A small script becomes useful when it turns a repeated decision into a reliable sequence of steps.
Top