What is Computer

Module: M3-R5: Python Programming

Chapter: Ch1 Computer Intro

🔍 What is Compilation?

Compilation is the process of converting high-level source code written by a programmer (like C, Java, or C++) into a machine-readable form (binary or object code) that the computer’s processor can execute directly.

In simple terms, a **compiler** acts as a translator between humans and computers — it takes your code and transforms it into a format the machine understands.

⚙️ Steps Involved in Compilation
  1. 1. Lexical Analysis: The compiler scans the code and breaks it into small tokens (keywords, operators, identifiers).
  2. 2. Syntax Analysis: It checks whether the code follows the grammar or syntax rules of the programming language.
  3. 3. Semantic Analysis: It ensures the meaning of the program is valid (e.g., variables are declared before use).
  4. 4. Optimization: The compiler improves the performance of the code without changing its output.
  5. 5. Code Generation: It converts the intermediate code into machine code (binary).
  6. 6. Linking: Combines all compiled files and external libraries into one final executable program.
💡 Example

Let’s consider a simple example in **C language** (since Python is an interpreted language):

// hello.c
#include <stdio.h>
int main() {
    printf("Hello, World!");
    return 0;
}

When we compile this file using gcc hello.c -o hello, it goes through all the above steps and produces an executable file hello.

🐍 Why Python is Not Compiled (But Interpreted)

Python doesn’t use a traditional compiler like C or C++. Instead, it uses an **interpreter** that executes code line by line. However, internally Python converts source code (.py) into **bytecode (.pyc)** which is then executed by the **Python Virtual Machine (PVM)** — so it does involve a kind of compilation step, but it’s not visible to the user.

✅ Key Points
  • Compilation converts source code to machine code.
  • It ensures the program is error-free before running.
  • Compiled programs run faster than interpreted ones.
  • Python mainly uses interpretation, not direct compilation.
Quick Links