- Define "source code" and "machine code."
- Explain the role of a compiler in translating code.
- Explain the role of an interpreter in translating code.
- Identify Python as an interpreted language.
How Does a Computer Understand Our Code?
We have a puzzle. We know that we write code in Python (source code), which looks like English. But we also know that the CPU only understands 1s and 0s (machine code). So how does our Python code actually get executed by the CPU? The answer is that we need a special program to act as a translator. There are two main types of translators: compilers and interpreters.
🌐
Analogy: The Language Translator
Imagine you need to translate a book from English to French. You have two options:
- Option A (The Compiler): You give the entire English book to a translator. They work for a while and come back to you with a completely translated French book. You then give this new book to the French reader.
- Option B (The Interpreter): You, the translator, and the French reader all sit in a room together. You speak one sentence in English. The translator immediately translates it aloud. The reader hears it. You speak the next sentence, and the process repeats. It's a live, line-by-line translation.
The Two Types of Translators
Compiler
A compiler reads your entire program at once, checks for errors, and translates it into a standalone executable file (.exe on Windows). You then run that executable file. Languages like C++ and Java use compilers.
- Pro: The final program runs very fast.
- Con: The compilation step can be slow before you can run anything.
Interpreter
An interpreter goes through your code line by line, translating and running each line as it goes. It does not create a separate executable file. Python is an interpreted language.
- Pro: Faster and easier to test and debug because you can run your code instantly.
- Con: Can be slower to run, especially for very large, complex programs.
Visualizing the Difference
This short video provides a great visual summary of the key differences between these two approaches.
[Video Placeholder] Search YouTube for "compiler vs interpreter for beginners". Find a clear, concise video and embed it here.
Knowledge Check: Sort the Concepts
Let's see if you can remember the key differences. Try this interactive sorting game.
[Interactive Game Placeholder] An H5P "Drag and Drop" game is perfect here. Create two columns ("Compiler" and "Interpreter") and have students drag characteristics like "Translates line-by-line," "Creates an .exe file," and "Python uses this" into the correct column.
Understanding that Python is an interpreted language helps explain why we can just click "Run" in Thonny and see our results immediately. The Python interpreter is doing the hard work of translating our code for the CPU on the fly, one line at a time.
Interactive Sandbox
Use this space to practice writing code and complete the "Your Turn" activities above.