<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: drani Godfrey</title>
    <description>The latest articles on Forem by drani Godfrey (@drani).</description>
    <link>https://forem.com/drani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2500247%2F67138864-f525-47fb-a293-7be7ee40eec1.png</url>
      <title>Forem: drani Godfrey</title>
      <link>https://forem.com/drani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/drani"/>
    <language>en</language>
    <item>
      <title>What Happens When You Run Python Code?</title>
      <dc:creator>drani Godfrey</dc:creator>
      <pubDate>Fri, 05 Dec 2025 14:33:11 +0000</pubDate>
      <link>https://forem.com/drani/what-happens-when-you-run-python-code-1b5b</link>
      <guid>https://forem.com/drani/what-happens-when-you-run-python-code-1b5b</guid>
      <description>&lt;p&gt;Python is a popular programming language, but have you ever wondered what happens behind the scenes when you run a Python program on your computer? In this article, we’ll explore the journey of Python code from a simple script on your hard drive to the output displayed on your screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Simple Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s start with a simple Python program. Imagine we have a file called greet.py stored on our computer. Inside this file, there’s a single function called greet():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#greet.py 

def greet(name:str):
    print(f"Hello {name}!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we call this function with the argument "drani":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet(name="drani")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the program prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello drani!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is straightforward, but what actually happens when you execute this program?&lt;br&gt;
Running the Python Program&lt;/p&gt;

&lt;p&gt;To run the program, we typically use the command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python greet.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When this command is executed, several things happen behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Loading the Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, the Python interpreter (CPython) loads the code from disk into memory. This is the first step in preparing the program for execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Compilation to Bytecode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, the interpreter invokes its internal compiler. The process includes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tokenization:&lt;/strong&gt; Breaking the code into meaningful symbols called tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building an Abstract Syntax Tree (AST):&lt;/strong&gt; Organizing the tokens into a tree structure that represents the code’s logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compiling to bytecode:&lt;/strong&gt; Transforming the AST into Python bytecode, a lower-level representation of your code that the interpreter can execute efficiently.&lt;/p&gt;

&lt;p&gt;If a cached version of the bytecode already exists in a .pyc file inside the pycache directory, Python may load that instead of recompiling the source file. This helps speed up execution for frequently used modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Executing Bytecode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the bytecode is ready, the CPython Virtual Machine executes it instruction by instruction. For each operation (opcode):&lt;/p&gt;

&lt;p&gt;The interpreter dispatches the opcode to a corresponding C function that implements the operation.&lt;br&gt;
These C functions are compiled into native machine code, binary instructions (zeros and ones) that the CPU can understand.&lt;br&gt;
The CPU fetches, decodes, and executes these instructions, manipulating Python objects in memory as needed.&lt;/p&gt;

&lt;p&gt;This process continues until all instructions have been executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Producing the Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Finally, when execution completes, the resulting values are stored in memory. If the program includes output commands (like print()), the results are displayed to the user on the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even a simple program like greet.py involves several layers of processing under the hood. From loading the file and compiling it to bytecode, to executing machine-level instructions, Python hides a lot of complexity from the programmer making it easy to write and run code while still being efficient and powerful.&lt;/p&gt;

&lt;p&gt;Understanding this process gives a deeper appreciation of how Python works and can help you write more efficient programs, troubleshoot performance issues, and better understand errors when they occur. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>computerscience</category>
      <category>python</category>
    </item>
  </channel>
</rss>
