Computer courses · Tools for technicians · Tech support Call 859-878-2615 WhatsApp Send a message

Home · Blog

Step-by-step guide

Compiler vs interpreter: the difference between compiled and interpreted programming languages, explained simply

By Gabriel ·

Short answer: a compiler translates your whole program into machine code once, before it runs, and gives you a ready-to-run file such as a Windows .exe. An interpreter translates and runs the program line by line, every time it runs. Compiled programs are usually faster and keep the source code private; interpreted programs run on any system that has the interpreter and are easier to test and debug.

This is lesson 4 of my Python course (2021). In this guide you will go through the idea step by step: why your Python file does not do anything when you double-click it, why software downloads like Firefox come in different versions for Windows, Mac and Linux, and how a simple letter-to-the-president analogy explains the difference between compiling and interpreting. At the end you will know the pros and cons of each method, which languages use which, and where Python fits, plus a comparison table and an FAQ.

DifficultyVery easy
Time15 minutes
Tools & requirementsAny computer with a web browser, Optional: a Python file (.py) and a text editor to try it yourself, Optional: Python 3 installed, to run the file, No programming experience needed
In this article
  1. Notice that a Python file does not run by itself
  2. Understand source code vs machine code
  3. See how compiled software is downloaded per platform
  4. Recognize the file you get for each system
  5. Learn what compiling does: translate everything at once
  6. Learn what interpreting does: translate line by line
  7. Use the letter analogy to remember the difference
  8. Remember: compiled programs must be rebuilt for each platform
  9. See cross-platform in action with browser apps
  10. Compare speed, security and debugging
  11. Match languages to each method and choose
  12. Where Python fits: compiled to bytecode, then interpreted
  13. Compiler vs interpreter comparison table
  14. Common misconceptions about compiling and interpreting
  15. Troubleshooting: why my program will not run
  16. Tips: which approach should a beginner learn first?
  17. Frequently asked questions
  18. Watch the video

Step 1Notice that a Python file does not run by itself

Python source code file hello.py opened in the Mu editor showing print statements and input lines, a text file that does nothing on its own until an interpreter runs it
  • Double-click a .py file and it usually just opens in an editor like Notepad or TextEdit: you see the code, but nothing runs.
  • That is because a Python file is source code, text written for humans. The computer only understands machine code, so something has to translate it first.
  • For Python that translator is the interpreter; for a language like Pascal you first compile the code to get an executable file, and then it runs.

Step 2Understand source code vs machine code

Teacher in front of a whiteboard explaining in class the difference between compiled and interpreted programming languages and how source code becomes machine code
  • Source code is what you write: lines 1, 2, 3... in a language like Python, C or PHP.
  • Machine code is what the processor executes. Both compiling and interpreting end in machine language; the difference is when and how the translation happens.
  • Keep this picture in mind: the same program can be translated all at once (compile) or one line at a time (interpret).

Step 3See how compiled software is downloaded per platform

Mozilla Firefox download page with the platform menu open listing Windows 64-bit, Windows 32-bit, Windows ARM64, macOS, Linux 64-bit and Linux 32-bit builds
  • Open the Firefox download page and look at the platform menu: Windows 64-bit, Windows 32-bit, macOS, Linux 64-bit, Linux 32-bit and more, plus many languages.
  • Each option is a separate build: the program was compiled once for each operating system and processor type.
  • This is the clearest everyday example of compiled software: you pick the version made for your machine.

Step 4Recognize the file you get for each system

Firefox dmg disk image opened on a Mac with the Firefox icon and an arrow to the Applications folder, an example of software compiled for macOS
  • On Windows you get an executable such as firefox.exe; on a Mac you get a .dmg disk image with the app inside; on Debian or Ubuntu Linux you get a .deb package.
  • Each file contains machine code made for that system, so it is ready to go: download, install, run.
  • Common mistake: downloading the Windows .exe on a Mac (or the reverse). It will not open, because it was compiled for another platform.

Step 5Learn what compiling does: translate everything at once

Diagram of how a compiler works: source code lines go into the compiler, which reads all lines at once and outputs separate executables for Windows exe, macOS dmg and Linux deb
  • A compiler takes all the lines of your program and turns them into one file, for example an executable for Windows.
  • The translation happens once, before the program runs, so when you start it, it is already machine code and runs fast.
  • The downside: every time you fix a bug or add something, you must compile again, and that takes time.

Step 6Learn what interpreting does: translate line by line

Diagram of how an interpreter works: the interpreter reads source code one line at a time and runs line 1, line 2 and line 3 in order while the program is running
  • An interpreter grabs line by line: it reads line 1 and runs it, then line 2, then line 3, while the program is running.
  • Because nothing has to be rebuilt, you can change a line and see the result right away, which makes testing simple.
  • The cost is speed: the translation work happens again every time the program runs.

Step 7Use the letter analogy to remember the difference

Illustration comparing a compiler to a translation company that translates a whole letter to Spanish for the president, and an interpreter who reads the letter to him line by line
  • Imagine you send a letter in English to the president of Mexico, who only reads Spanish.
  • Compiling = a translation company reads the whole letter, translates it, and the president gets a finished Spanish letter he can read quickly, anytime.
  • Interpreting = an interpreter sits with the president and reads it line one, line two, line three. Every time he needs the letter again, the interpreter must come back: that is why interpreting is slower.

Step 8Remember: compiled programs must be rebuilt for each platform

Firefox dialog opening a Linux deb package file for Debian or Ubuntu with gedit as default application, showing software packaged for one specific operating system
  • A compiled program is not cross-platform. If you compile only for Windows, Mac and Linux users cannot run it.
  • To share it, you repeat the process for Mac, for Linux, and even for different processors, such as Apple M1 (ARM) versus Intel chips.
  • This is the extra step the video lists as a con: more builds to make and to update every time the code changes.

Step 9See cross-platform in action with browser apps

Browser-based text editor Editpad with the lines this is a nice car and this is a test typed in, a web app that runs anywhere without downloading software
  • Open a web app like editpad.org and type a few lines: it works in the browser, without downloading Notepad or any program.
  • The same idea powers Google Docs, online CRM tools and QuickBooks Online: software as a service that runs on any computer or phone with a browser.
  • Code delivered to and run by the browser, like JavaScript, is the interpreted world's big advantage: it runs anywhere the interpreter (here, the browser) exists.

Note: The flip side: if the device does not have the interpreter, the program will not run at all.

Step 10Compare speed, security and debugging

Comparison chart of compiled vs interpreted languages pros and cons: speed, platforms, changes, debugging, source code privacy and readiness to use, with Python and Java as hybrids
  • Compiled: faster, ready to run, and the source code stays private, so it is harder for someone to copy your software and claim it as their own.
  • Interpreted: cross-platform, easy to test and easier to debug, but slower, and the source code is kind of public, which makes it easier to copy.
  • Neither is better in every case; the right choice depends on what your project needs.

Step 11Match languages to each method and choose

Chart of programming language examples: compiled C and C++, interpreted PHP and JavaScript, and hybrid Python and Java that use bytecode and a virtual machine
  • Examples from the class: C and C++ are compiled; PHP and JavaScript are interpreted; Python and Java are hybrids, something between compiling and interpreting.
  • Choose compiled when you need something very fast and secure; choose interpreted when you want cross-platform and easy debugging.
  • Hybrids try to give you the best of both worlds, which is one reason Python is such a popular first language.

Where Python fits: compiled to bytecode, then interpreted

People often call Python an interpreted language, and in the video it sits in the hybrid group. Both are true in practice. According to the official Python glossary, Python source code is compiled into bytecode, an internal intermediate language, and that bytecode is cached in .pyc files (you will see them in a __pycache__ folder) so the same file starts faster the second time. The bytecode then runs on the Python virtual machine.

What this means for a beginner:

  • You never run a separate compile command: you just type python3 file.py and Python does both parts.
  • You still need Python installed on every computer that runs your script, like any interpreter.
  • Java works in a similar way: code is compiled to bytecode that runs on the Java virtual machine.

To try it yourself, install Python first (on a Mac) and then run a .py file from the command line on Windows.

Compiler vs interpreter comparison table

CompilerInterpreter
When it translatesOnce, before the program runsEvery time, while it runs
How much at a timeThe whole programOne line at a time
OutputExecutable file (.exe, app in a .dmg, .deb package)No separate file, runs the source
SpeedFasterSlower
PlatformsOne build per system and processorAny system with the interpreter
After a code changeCompile againRun again right away
DebuggingHarderEasier
Source codePrivateVisible
Examples (from the class)C, C++PHP, JavaScript

Common misconceptions about compiling and interpreting

  • "Interpreted means not real programming." Wrong: huge websites and apps run on interpreted and hybrid languages; the method is a trade-off, not a quality level.
  • "Compiled code can never be read." It is much harder to read than source code, which is why the video calls it more private, but it is not a lock. Keep your source code secure either way.
  • "A language is only one or the other." Many modern tools mix both. Python and Java use bytecode, and today's JavaScript engines in browsers also compile code while it runs (just-in-time compilation) to make it faster.
  • "Double-clicking a .py file should run it." On most systems it opens in an editor. Run it with Python instead, from the terminal or from an editor's Run button.

Troubleshooting: why my program will not run

What happensWhyWhat to do
Double-clicking a .py file opens the code in an editorThe file is source code; the system opened it as textRun python3 file.py (or py file.py on Windows) in a terminal
A downloaded program will not openIt was compiled for another platformDownload the build for your system (Windows, macOS, Linux, 32 or 64-bit, ARM or Intel)
*python3: command not found*The interpreter is not installedInstall Python 3 from python.org
Your fix does not appear in a compiled programYou changed the source but did not rebuildCompile again, then run the new executable
Code works on one PC but not anotherMissing interpreter or different versionInstall the same version on both computers

Tips: which approach should a beginner learn first?

  • Start with an interpreted or hybrid language such as Python: you see results right away and errors are easier to find, exactly the advantages listed in the video.
  • Learn what the files mean: .py is source code, .exe and .app are compiled programs, .deb and .dmg are packages for Linux and Mac.
  • Get comfortable with a good editor; see how to install Visual Studio Code on Windows for Python.
  • When you later need raw speed or to ship a program to people who do not have Python, look at compiled languages like C or C++.
  • Protect your source code: keep backups and do not publish it if you do not want it copied.

Frequently asked questions

What is the difference between a compiler and an interpreter?

A compiler translates the whole program into machine code once, before it runs, and produces an executable. An interpreter translates and runs the program line by line every time it runs.

Is Python compiled or interpreted?

Both, in a way. Python compiles your source code to bytecode automatically and then runs it on the Python virtual machine, so it is usually called interpreted or hybrid.

Which is faster, compiled or interpreted code?

Compiled code is usually faster, because the translation happens once in advance instead of every time the program runs.

Why is compiled software not cross-platform?

Because the compiler produces machine code for one operating system and processor. A Windows build will not run on a Mac or Linux, so each platform needs its own build.

What are examples of compiled and interpreted languages?

C and C++ are classic compiled languages. PHP and JavaScript are usually described as interpreted, and Python and Java are hybrids that use bytecode.

Why does my Python file not run when I double-click it?

A .py file is source code, so the system often opens it in a text editor. Run it with the interpreter instead, for example python3 file.py in a terminal.

Is interpreted code less secure?

The source code is easier to see and copy, because it is shipped as readable text. Compiled programs keep the source private, which makes copying harder.

Is an interpreter easier to debug?

Usually yes. You can change a line and run it again right away without a rebuild, so finding and fixing bugs is faster.

Need help with this?

Call, text or WhatsApp me at 859-878-0554.

Watch the video

4- What is compilation & interpreter in programming coding (Course Python 2021)

Original video: 4- What is compilation & interpreter in programming coding (Course Python 2021)

Comments

More articles

WhatsApp Visitors today: — This month: — Total: —