DEV Community

Cover image for How Python Programs Works?
vayola pradeep
vayola pradeep

Posted on • Updated on

How Python Programs Works?

A Python program starts as a text file written in a code editor, saved with a .py extension. The Python interpreter reads this source code, analyzes it, and executes it line by line. During this process, it performs lexical analysis, parsing, and execution. Optionally, the source code can be compiled into bytecode for faster execution. The Python interpreter acts as a virtual machine, executing the code by converting it into machine-specific binary code. The program may utilize library modules for various tasks. When run, it can interact with the system, process data, and produce output. Any errors or exceptions are handled during execution.

Image description

Python is a high-level programming language known for its simplicity and readability. To understand how a Python program works, let's break it down into several key steps with example Hello World:

Code Editor:
You start by writing your Python code in a code editor. This is where you create your program and save it with a .py extension. A code editor provides features for writing and editing code, such as syntax highlighting and code completion.
Open a text editor, and write the following code:

print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Image description

Source File:
Your Python code is saved in a source file with a .py extension, e.g., hello.py. This file contains the text of your program written in Python's syntax.

Image description

Compiler (Optional):
Python is typically an interpreted language, meaning it doesn't require explicit compilation before running. However, in some cases, your Python code may be compiled into bytecode. This compiled bytecode is saved in .pyc files. The compilation is not something you usually do manually; it's managed by the Python interpreter.

Image description

Bytecode:
Bytecode is an intermediate representation of your Python code. It's a low-level, platform-independent format that the Python interpreter can execute. Bytecode files (.pyc) are generated to improve execution speed. If the .pyc file exists and is newer than the source code, Python uses it for faster execution. In the case of "Hello, World!" where the code is simple, Python often skips the bytecode step.

Image description

Python Interpreter (Virtual Machine):
The Python interpreter is responsible for executing your Python code.You execute the Python program in your terminal or command prompt.

python hello.py
Enter fullscreen mode Exit fullscreen mode

The Python interpreter reads hello.py and performs several steps during the execution:

Lexical Analysis: The interpreter reads the source code, breaks it down into tokens, and identifies keywords, identifiers, operators, and literals.

Parsing: It constructs a parse tree to understand the code's structure and logic.

Execution: It executes the program by following the instructions defined in the parse tree. This includes performing calculations, I/O operations, and function calls.

Image description

Library Modules:
Python provides a rich standard library, which includes a wide range of modules and functions for various tasks. You can import these library modules into your program to extend its functionality. For example, the math module provides mathematical functions, and the os module allows you to interact with the operating system.While the "Hello, World!"example doesn't use any additional modules, you can import and use them in more complex programs to extend functionality.

Image description

Binary Code:
The Python interpreter converts the bytecode into machine code that is specific to your computer's architecture. This is done dynamically during the execution of your Python program.

Image description

Running Program:
As the Python interpreter executes the bytecode, your Python program runs, and it can interact with the system, read and write files, and produce output. If there are errors or exceptions, Python will handle them according to your code or raise an error if not caught.In this example, the program will run and display the output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Image description

Completion:
The program execution completes, having successfully printed Hello, World! to the console. If there were errors or exceptions in the code, Python would handle them accordingly.

In the case of Hello, World!, the process is relatively simple. You write the code in a code editor, save it as a source file, and execute it using the Python interpreter. Python interprets and executes the code, displaying the output. Complex Python programs follow a similar process, with more intricate logic and possibly the use of library modules.

Top comments (1)

Collapse
 
vlc33 profile image
VLC33 • Edited

Useful article. I like how arrows looks like python from snake game.

It would be cool to add a few more details about Lexical Analysis and Parsing steps, how import stage are fit in here, like when it implements decorators and metaclasses constructors.