Python is an interpreted, high-level, general-purpose programming language.
Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace.
It provides constructs that enable clear programming on both small and large scales.
Python is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use.
Python's license is administered by the Python Software Foundation.
The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python web site, https://www.python.org/, and may be freely distributed.
A compiler takes your entire source code, analyzes it, and translates it all at once into a standalone machine code file (like an .exe file on Windows). Once compiled, the computer can run this file directly without needing the original source code or the compiler itself.
How it works: Source Code → Compiler → Machine Code (Executable) → Output
Architecture Specificity (Built for One Target): Compilers translate code into machine instructions designed for a specific hardware architecture (like x86_64 for standard PCs, or ARM for Macs and smartphones) and a specific Operating System. This means an executable compiled for Windows on an Intel processor will not run on an ARM-based Mac or an Android phone. To run the program elsewhere, the source code must be recompiled specifically for that target platform.
Speed: Very fast execution. Because the heavy lifting of translation is already done, the CPU runs the resulting machine code at native hardware speed.
Debugging: More rigid upfront. If there is a syntax error anywhere in the program—even on line 500 of a massive file—the compiler will usually refuse to generate the executable. You are forced to fix all compilation errors before you can run and test any part of the program.
Common Languages: C, C++, Rust, Go.
An interpreter does not build a separate, standalone executable file. Instead, it reads your source code line-by-line, translates that specific line into machine code, executes it immediately, and then moves on to the next line.
How it works: Source Code → Interpreter → Line-by-Line Execution → Output
Platform Independence (Write Once, Run Anywhere): Unlike compiled code, interpreted source code is not bound to a specific hardware architecture. The same Python or JavaScript file can run on Windows, Mac, Linux, or ARM-based devices without changes. The only requirement is that the target device must have the interpreter software installed to read and execute the code.
Speed: Slower execution. Because translation and execution happen simultaneously at runtime, it introduces significant overhead. For example, if a line of code sits inside a loop that runs 1,000 times, the interpreter must analyze and translate that same line 1,000 times.
Debugging: Highly interactive and easier. The program begins running immediately and executes successfully right up until it hits an error. When it does, it stops exactly on the problematic line, making it much easier to pinpoint, test, and fix bugs on the fly.
Common Languages: Python, JavaScript, Ruby, PHP.
Many modern programming languages combine both compilation and interpretation to maximize efficiency and portability.
Instead of translating directly to native machine code, a hybrid system first compiles the source code into an optimized, low-level intermediate format called Bytecode. Then, a specialized runtime environment or interpreter (often called a Virtual Machine) translates and executes that bytecode on the target device.
How it works:
Source Code → Compiler → Bytecode → Virtual Machine → Output
The Big Advantage (Portability): This approach enables the famous philosophy of "Write Once, Run Anywhere." You only need to compile your source code into bytecode a single time. As long as a target device (whether it's a Windows PC, a Mac, or a smartphone) has that language's specific Virtual Machine installed, it can read and execute the exact same bytecode file.
Just-In-Time (JIT) Compilation: To close the performance gap with purely compiled languages, modern hybrid systems use a JIT compiler. As the virtual machine interprets the bytecode, it monitors the program for "hot spots"—blocks of code, like loops, that run frequently. The JIT compiler quickly compiles those specific "hot" sections directly into native machine code on the fly, boosting execution speed significantly.
Common Languages: Java (via the Java Virtual Machine / JVM), C# (.NET / Common Language Runtime), and modern JavaScript engines (like Google Chrome's V8, which utilizes JIT compilation).
The way a Python script executes is beautifully seamless. You type python script.py, and the code executes immediately. Because of this, Python is frequently referred to as an "interpreted" language. However, this label is only half-true. Under the hood, Python actually employs a compilation step, translating your source code into an intermediate format called bytecode, which is then executed by the Python Virtual Machine (PVM).
This document explores the journey of Python code from plain text to execution, focusing on the standard implementation of Python (CPython).
The process of running a Python program follows a specific pipeline:
Source Code (.py): You write human-readable Python code.
Lexing and Parsing: The Python parser reads the code, checks for syntax errors, and builds an Abstract Syntax Tree (AST).
Compilation: The compiler takes the AST and translates it into a lower-level, platform-independent representation known as bytecode.
Execution (The PVM): The Python Virtual Machine takes this bytecode and executes it, instruction by instruction.
Note: To speed up future executions, Python often saves this compiled bytecode to disk in a __pycache__ directory with a .pyc extension.
Bytecode is a low-level, intermediate representation of your source code. It is not machine code (like the 1s and 0s directly executed by your Intel or AMD CPU). Instead, it is a set of instructions designed to be executed by a software CPU—the Python Virtual Machine.
Key characteristics of Python bytecode:
Platform-Independent: Bytecode compiled on a Windows machine will run flawlessly on a Mac or Linux machine, provided they have the same version of the PVM.
Version-Specific: Bytecode instructions (opcodes) can change between Python versions (e.g., Python 3.10 bytecode might not be perfectly compatible with Python 3.11).
Low-Level: It breaks complex Python statements down into rudimentary operations.
The Python Virtual Machine is the runtime engine of Python. If you are using CPython, the PVM is essentially a massive loop written in C (specifically, a function in the CPython source code called _PyEval_EvalFrameDefault).
The PVM is a stack-based virtual machine. Unlike your physical CPU, which uses registers to store temporary data, the PVM relies on an "evaluation stack" (a Last-In, First-Out data structure).
When the PVM executes operations:
It pushes data onto the top of the stack.
It executes an instruction, which usually pops the required number of items off the top of the stack.
It performs the calculation and pushes the result back onto the stack.
You can actually see the bytecode generated by Python using the built-in dis (disassembler) module.
import dis
def add_numbers(a, b):
result = a + b
return result
# Disassemble the function
dis.dis(add_numbers)
The Output:
When you run dis.dis(), you will see something like this:
4 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_ADD
6 STORE_FAST 2 (result)
5 8 LOAD_FAST 2 (result)
10 RETURN_VALUE
Readable and Simple Syntax: Python is designed to be highly intuitive and closely mimics human language (English), emphasizing readability and clean code.
Interpreted Language: Python executes code line-by-line rather than compiling it all at once. This makes the debugging process significantly easier.
Dynamically Typed: You do not need to explicitly declare variable types (like int, String, etc.) before using them. The type is assigned dynamically at runtime.
"Batteries Included" Standard Library: Python comes out of the box with a massive standard library, providing built-in modules for everything from regular expressions and file I/O to unit testing.
Multi-Paradigm: It supports multiple programming styles, including object-oriented, procedural, and functional programming.
Platform Independent: Python is cross-platform. Code written on a Windows machine will run seamlessly on macOS or Linux without needing modifications.
Highly Beginner-Friendly: Because of its straightforward syntax, it has a very low barrier to entry, making it an excellent first language to learn.
Massive Ecosystem: Python boasts a colossal collection of third-party frameworks and libraries (e.g., Pandas for data science, TensorFlow for AI, Django for web development).
High Productivity: You can achieve complex tasks with significantly fewer lines of code compared to languages like Java or C++. This speeds up prototyping and overall development cycles.
Strong Community Support: With one of the largest developer communities in the world, finding solutions to bugs, tutorials, and comprehensive documentation is incredibly easy.
Extreme Versatility: It is a "Swiss Army knife" language, widely used in Data Science, Machine Learning, backend Web Development, automation/scripting, and even finance.
Slower Execution Speed: Because it is interpreted and dynamically typed, Python is generally slower than compiled languages like C++ or Java. Additionally, the Global Interpreter Lock (GIL) limits true multi-threading.
High Memory Consumption: Python's flexibility and dynamic typing come at a cost; it uses a high amount of memory, making it less ideal for highly memory-constrained systems.
Weak in Mobile Computing: While it dominates servers and desktop environments, Python is rarely used for mobile app development (Android/iOS), where languages like Kotlin and Swift are standard.
Runtime Errors: Because types are evaluated dynamically, certain issues (like type mismatches or syntax bugs in unexecuted branches) might only trigger errors when the program is actually running, rather than being caught beforehand by a compiler.
In the programming world, Python flavors refer to the different implementations and interpreters of the Python language. While the language syntax remains largely the same, each flavor is built using a different underlying programming language or optimized for specific environments, performance needs, and platforms.
This is the default, standard, and most widely used implementation of Python. When you download Python from python.org, you are downloading CPython.
How it works: It is written in C. It compiles your Python code into bytecode (.pyc files) and then executes it on a virtual machine.
Pros: 100% compatible with all Python libraries (like NumPy, Pandas, etc.), up-to-date with the newest language features.
Cons: It features the Global Interpreter Lock (GIL), which can limit multi-threaded performance in CPU-heavy tasks.
If CPython isn't fast enough for your needs, developers have created alternatives focused purely on performance.
What it is: A fast, alternative implementation of Python written in Python itself (using a framework called RPython).
The Secret Sauce: It uses a Just-In-Time (JIT) compiler. Instead of translating code line-by-line, it compiles frequently used code into machine language on the fly.
Best For: Long-running CPU-bound applications where execution speed is critical. It can often run code 4x to 5x faster than CPython.
What they are: Stripped-down, highly optimized versions of Python 3.
Best For: Microcontrollers and embedded systems (like Arduino, Raspberry Pi Pico, or ESP32). They are designed to run effectively on chips with very limited RAM and processing power.
These flavors are designed to help Python blend seamlessly with other major programming ecosystems.
What it is: Python integrated with the Java Virtual Machine (JVM).
The Secret Sauce: It compiles Python code directly into Java bytecode. This allows your Python programs to seamlessly import and use any Java class, and vice versa.
Best For: Enterprise environments heavily reliant on Java infrastructure.
What it is: Python tightly integrated with Microsoft’s .NET framework.
The Secret Sauce: It uses the Common Language Runtime (CLR), allowing Python code to easily use .NET libraries and tools.
Best For: Windows developers who want to script inside .NET applications.
Let's create a very simple program called Hello World. A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language to beginners.
Type the following code in any text editor or an IDE and save it as hello_world.py
print("Hello, world!")
Then, run the file by typing the following command:
(For Linux and Mac) : python3 hello_world.py
(For Windows): python3 hello_world.py
This line instructs your Python interpreter to execute the command you just wrote. You should see the correct output
The Python interpreter is typically located at /usr/local/bin/python3. Adding /usr/local/bin to your shell's search path ($PATH) allows you to start it simply by running:
python3
Microsoft Store & Install Manager: If you installed Python via the Microsoft Store or the modern Python Install Manager, the python3 or py command will be readily available in your terminal.
Python Launcher: If you use the standard launcher (py.exe), you can launch the interpreter by typing:
py
To exit the interpreter with a success status (zero exit status), you can use a keyboard shortcut or a built-in function:
Keyboard Shortcuts: Press Control-D on Unix-like systems, or Control-Z followed by Enter on Windows.
Command: If the shortcuts don't work, type the built-in function and hit Enter:
quit()
When you run Python directly in a terminal (a TTY), the interpreter enters interactive mode. In this mode, it waits for your input using two types of prompts:
Primary Prompt (>>>): Indicates that the interpreter is ready for a new command.
Secondary Prompt (...): Indicates a continuation line for multi-line statements (like loops, functions, or the new t-string multi-line blocks). To exit secondary prompt (...), press Enter on an empty line.
Before the first prompt appears, the interpreter displays a welcome message containing its version number, build details, and an environment notice:
Python 3.14.6 (tags/v3.14.6:6abddd9, June 10 2026, 12:30:00) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>