Skip to main content

How Computers Run Code

What is Python?

Python is a general-purpose, high-level, interpreted programming language used to write instructions for computers in a simple and readable way.

Let's break down what "general-purpose" means:

  • General-purpose: Python is not limited to one specific task. We can use it for many different applications like:
    • Web Development
    • Automation
    • Data Science
    • Artificial Intelligence and Machine Learning

But how does a computer actually run the Python code we write? Let's break down how programming languages work.


How Programming Languages Work: From Code to Execution

1. Machine Language (Binary)

  • What it is: The only language a computer's CPU can actually understand.
  • Format: It is made up entirely of electrical signals, which we represent as ones and zeros (1 and 0).
  • Difficulty: It is extremely difficult for humans to read or write directly.

For example, look at how the word "HELLO" would appear in machine language:

High-Level Language (Human)Machine Language (Computer Binary)
H01001000
E01100101
L01101100
L01101100
O01101111

2. High-Level Language (Python, Java, etc.)

  • What it is: Programming languages designed to be easy for humans to read and write.
  • Format: Uses simple English-like words (for example: print("Hello")).
  • Limitation: It cannot be understood directly by the computer. It must be translated.

Because the computer only understands binary, we need a Translator to convert our high-level code into machine language.


1. Compilers vs. Interpreters

Translators are mainly of two types: Compilers and Interpreters.

Think of the difference like translating a book from English to Telugu:

  • Compiler: Translates the entire book all at once before handing it to the reader. (If there is even one typo on page 50, it stops the whole translation).
  • Interpreter: Translates live, sentence by sentence out loud. It reads one sentence, translates it, and speaks it immediately.

Here is a quick comparison:

Compiler (e.g., C, C++, Rust, Go)

Like translating a whole book at once before reading.

1Translates the complete program before execution
2Creates a separate compiled file to run
3Crashes the whole process if there is a mistake
Interpreter (e.g., Python, JavaScript, Ruby)

Like translating live, sentence-by-sentence.

1Reads and executes code step-by-step (line-by-line)
2Runs the code immediately without creating a separate file
3Stops exactly at the line where a mistake is found

2. The Live Translation Flow

When you write Python code and click Run, a special translator called the Python Interpreter converts and executes your code step-by-step:

1. Your Code
print("Hello")
Translate
2. Python Interpreter
Translates code step-by-step
Execute
3. Screen Output
Hello

3. Why Python Uses an Interpreter

Since Python translates and runs code line-by-line, it gives two big advantages for beginners:

  • No Waiting: You don't have to wait for the computer to convert the entire project. You type code and click Run to see the output instantly.
  • Easy Debugging (Error finding): If you make a mistake on line 10, Python runs lines 1 to 9 successfully, stops right at line 10, and tells you: "Hey, there is an error on line 10!" This makes finding and fixing mistakes very easy.

Quick Summary

  • Machine Language (Binary): The low-level electrical signals (0s and 1s) understood directly by the CPU.
  • High-Level Language: Human-readable languages like Python that make expressing logic easy.
  • Compiler vs. Interpreter: Compilers translate the whole program before execution; Interpreters translate and run instructions line-by-line.
  • Python's Execution Engine: Python uses an interpreter, providing instant visual feedback and precise line-by-line error messages.

What's Next?

Now that we understand how Python code runs behind the scenes, let's learn how Python stores and manages information using Variables & Data Types in Module 2!