Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Understanding Programs and Python Execution

Recommended resources

Hello! Welcome to your first lesson in our journey into computer programming. It's great to have you here. Since your goal is to build problem-solving skills using Python, we'll start with the most fundamental question: what happens when you tell a computer to do something?

In this lesson, we'll explore what a program is and how a computer, specifically, executes a Python program. Understanding this foundation will demystify what's happening "under the hood" and set you up for success as you start writing your own code.

What Is a Program?

At its heart, a computer is a machine that follows instructions. But how does it know what to do? That’s where programs come in.

Introduction to Computers and Programming

Let's start with a formal definition. Please read the first two paragraphs of this chapter from the book "Introduction to Computers and Programming" by Tony Gaddis.

You can find these paragraphs at the very beginning of the document. Read from the start to get a clear definition of "program" and "software".

As the text explains, a program is simply a set of instructions that a computer follows to perform a task. Everything your computer does, from displaying this text to playing a video, is controlled by programs, also known as software. The people who write these instructions are called programmers or software developers. Soon, that will include you!

The instructions in a program are a bit like the steps in a recipe. Each step is precise, and they must be followed in the correct order to get the desired result.


The Language of the Machine

You and I can understand instructions written in English. But computers have their own language, and it looks nothing like ours. A computer's processor, or Central Processing Unit (CPU), understands only one language: machine code. This is a sequence of 1s and 0s that correspond to basic operations like adding two numbers or moving data.

Writing a program directly in machine code would be incredibly difficult and slow. Imagine trying to write an essay using only two different symbols! To solve this, programmers created high-level programming languages, like Python. These languages use words and syntax that are much easier for humans to read and write.

Here’s a quick overview of this relationship between different types of programming languages.

Introduction to Computers and Programming

This section explains how a computer goes from a program to actual operations. It covers machine language, assembly language, and high-level languages.

Please read the section titled "How a Program Works." Focus on these parts: Start at the beginning of the section and read the introduction to understand why machine language is necessary. Then, skip down to the subsection "High-Level Languages" and read that part to see how languages like Python make programming easier.

The key takeaway is that for the CPU to run your Python code, your human-readable instructions must be translated into machine code. This crucial translation job is done by special programs called compilers or interpreters.


Translators: Compilers and Interpreters

So, if we write in Python, but the computer only understands machine code, how does the message get across? We need a translator. There are two main types of translators: compilers and interpreters.

  • A compiler translates your entire program into machine code before you run it. It creates a separate file (an executable) that can be run by the computer.
  • An interpreter translates and runs your code line-by-line, at the same time. It reads one instruction, translates it, the CPU executes it, and then the interpreter moves to the next instruction.

Python is known as an interpreted language. To understand the difference in practice, the following article provides a great summary.

Python Basics: What Is an Interpreter? | Anaconda

This article gives a clear and concise explanation of what an interpreter is and why it's needed.

First, read the section titled "The TL;DR" for a quick summary of the difference between compiled and interpreted languages. Then, read the section "Why Do We Need Compilers and Interpreters, Anyway?" to solidify your understanding.


The Python Execution Model

We've said that Python is an interpreted language, but the reality is slightly more complex and quite clever. The program that runs your Python code is called the Python interpreter. When you run a Python script, a two-step process begins.

  1. Compilation to Bytecode: First, the interpreter's compiler component translates your human-readable Python code into an intermediate language called bytecode. Bytecode is a lower-level, platform-independent set of instructions. It's not machine code, but it's closer to it.

  2. Interpretation by a Virtual Machine: This bytecode is then sent to the Python Virtual Machine (PVM). The PVM is the "runtime engine" of Python; it's the part of the interpreter that reads the bytecode, instruction by instruction, and translates it into the final machine code that your computer's CPU can execute.

This video provides an excellent walkthrough of this entire process.

What is the Python Interpreter? (How does Python Work?)

The presenter explains exactly what happens when you run a Python file, from the command you type to the final execution by the CPU.

Please watch the section of the video that walks through the execution of a hello.py program. It starts at this timestamp and clearly explains the roles of the interpreter, compiler, bytecode, and the Python Virtual Machine.

This combination of a compiler (to bytecode) and an interpreter (the PVM) is what gives Python much of its flexibility. The following diagram illustrates this entire flow, from your Python file to the machine code executed by the hardware.

This diagram shows the steps for executing Python code. A Python source file is first processed by the interpreter, which compiles it into bytecode. This bytecode is then executed by the Python Virtual Machine (PVM), which translates it into machine code for the computer's processor to run.

Tracing the Execution Flow

Now that we understand the high-level process, let's zoom in on what it means for the computer to execute your code "line by line." When the Python Virtual Machine executes your bytecode, it processes one instruction at a time, without knowing the "big picture" of what your program is trying to accomplish. This step-by-step process is called program tracing.

Watching a trace of a simple program can make this concept much clearer.

Tracing program execution | Intro to CS - Python | Khan Academy

This video from Khan Academy visualizes how a computer "thinks" as it moves through a program one instruction at a time.

Watch the first part of the video, from the beginning up to the end of the first example. Pay attention to how the computer evaluates the expression 4 + 20.55 before it carries out the print instruction. This demonstrates the "evaluate, then execute" nature of the interpreter.

As you saw, the computer is a literal-minded machine. It evaluates any expressions to a single value and then performs whatever action the instruction (like print) tells it to do with that value. Understanding this simple but rigid process is key to learning how to give the computer the exact instructions needed to solve a problem.


Conclusion

Congratulations on completing your first lesson! We've covered some significant ground. You now have a mental model for how a computer executes a Python program, which is a fantastic starting point for any new programmer.

Here are the key takeaways:

  • A program is a sequence of instructions for a computer.
  • Computers only understand machine code (1s and 0s).
  • High-level languages like Python allow us to write code in a human-readable way.
  • The Python interpreter translates our code for the computer. It does this in two steps:
    1. Compiling the source code into bytecode.
    2. Executing the bytecode with the Python Virtual Machine (PVM).
  • The computer executes programs one instruction at a time, following the logic you provide.

In our next lesson, we will put this knowledge into practice. You'll get your hands on the Python interpreter and use it to evaluate simple expressions yourself.