Python Program Execution
Source Code:
This is the Python program you write, usually stored in a file with a .py extension.
It consists of human-readable instructions written using Python syntax.
For example:
print(“Hello World!”)
Interpreter:
The Interpreter is the core engine that reads the source code and converts it into a format the machine can understand (bytecode). The interpreter performs multiple tasks:
● Compiler: It first parses and compiles the source code into bytecode. Bytecode is an intermediate, platform-independent representation that Python’s virtual machine can execute.
● Bytecode: This is a lower-level code, which the interpreter generates to optimize execution. Python does not directly execute source code but executes this bytecode.
● Python Virtual Machine (PVM): The PVM reads the bytecode and executes it. This is the part of the interpreter that translates bytecode into machine-level instructions that the CPU can execute.
Source Libraries:
Python programs often rely on libraries for additional functionality, like performing mathematical operations, handling file I/O, or web development.
The Source Libraries are external Python modules or standard libraries that can be imported and used within your Python program. The interpreter accesses these libraries during execution as required by the source code.
Some commonly used source libraries:
● math: The math library provides mathematical functions like trigonometry, logarithms, and other operations.
● datetime: The datetime library is used to manipulate dates and times.
● random: The random library provides functions for generating random numbers and selections.
● os: The os library provides functions for interacting with the operating system, such as file handling and directory operations.
Error:
As shown in the diagram, if any part of the program contains errors (like syntax errors or runtime errors), the interpreter will throw an Error. The Error block in the diagram reflects the role of the interpreter in detecting and reporting problems in the code. When an error occurs, Python halts execution and prints an error message, which helps you identify and fix the issue.
Example:
print(“Hello World”
#demonstration of error as the ‘)’ curly braces of print function is
#not closed
Output:
Execute Code:
Once the interpreter successfully processes the bytecode (without encountering errors), it translates the bytecode into machine instructions that the operating system can execute. This is where the Executed Code produces the desired output, whether it’s printing a result to the screen, performing a calculation, or executing a more complex function.
Once execution is completed and there is no error, we get following output:
Output:
Programs To Practice
Install Python and VS Code.
Write a Python program to print your Name and qualification on the console.