How Python Works

From Brij kishore Pandey.

Letโ€™s walk through a simplified guide to understanding how your Python code becomes a functioning program:

1๏ธโƒฃ ๐—ช๐—ฟ๐—ถ๐˜๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ: We start with typing out our Python program in a text editor, saving it as a โ€˜.pyโ€™ file.

2๏ธโƒฃ ๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐—ฝ๐—ฟ๐—ฒ๐˜๐—ฒ๐—ฟ: When we run the program, itโ€™s sent into the Python Interpreter, which is made up of two parts:

๐—–๐—ผ๐—บ๐—ฝ๐—ถ๐—น๐—ฒ๐—ฟ: This part changes our Python code into something called byte code. This byte code is saved in a โ€˜.pycโ€™ file, helping our program run faster the next time.

๐—ฃ๐˜†๐˜๐—ต๐—ผ๐—ป ๐—ฉ๐—ถ๐—ฟ๐˜๐˜‚๐—ฎ๐—น ๐— ๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ฒ (๐—ฃ๐—ฉ๐— ): The PVM uses the byte code and follows the instructions one by one until the program is finished or runs into an error.

3๏ธโƒฃ ๐—Ÿ๐—ถ๐—ฏ๐—ฟ๐—ฎ๐—ฟ๐˜† ๐— ๐—ผ๐—ฑ๐˜‚๐—น๐—ฒ๐˜€: If our program uses library modules from Pythonโ€™s standard library or elsewhere, these are also changed into byte code. The PVM then uses this byte code, allowing our program to use the features of these modules.

4๏ธโƒฃ ๐—™๐—ฟ๐—ผ๐—บ ๐—•๐˜†๐˜๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ ๐˜๐—ผ ๐— ๐—ฎ๐—ฐ๐—ต๐—ถ๐—ป๐—ฒ ๐—–๐—ผ๐—ฑ๐—ฒ: The PVM further converts the byte code into machine code, which is a series of 1s and 0s. This machine code is what your computerโ€™s brain, the CPU, can directly understand.

5๏ธโƒฃ ๐—ฅ๐˜‚๐—ป๐—ป๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ: After the machine code is ready, your computer uses it to run your program. And there you have it! Your Python program is up and running.

Understanding the transition from Python code to a running program provides insight into how Python, as an interpreted language, bridges the gap between the high-level code we write and the low-level operations performed by a computer.