Week 1 Lecture: Course Introduction and First Concepts
Table of Contents
Fundamentals of Programming
Communicating with Computers
To begin, we must understand the nature of the machine we’re communicating with.
- Computers are not “smart.” They have no common sense or intuition.
- They are literal, precise, and incredibly fast, following instructions exactly as given.
- Their native language is Binary (sequences of 0s and 1s representing “on” and “off” electrical signals).
- The Challenge: How do we bridge the gap between complex human ideas and simple machine instructions?
What is a Programming Language?
The bridge is a programming language.
- It is a formal language with strict rules (syntax) that allows us to express complex instructions clearly.
- It serves as a translator between human logic and machine code.
- Writing
print("Hello")
is much easier than the hundreds of 0s and 1s required to do the same thing in machine code (01001000 01100101...
).Analogy: The Translator A programming language is like a skilled, professional translator. You give precise instructions in a language you know (your logic), and the translator perfectly converts them into a language the other person understands (machine code) so your instructions can be followed exactly.
The Concept of an Algorithm
Before you write code, you must have a plan. In computer science, that plan is called an algorithm.
- An algorithm is the “blueprint” or “recipe” for solving a problem.
- It is a finite, step-by-step plan of action.
- Each instruction must be clear and unambiguous (e.g., “add 2.5 grams of salt,” not “add a bit of salt”).
- Crucially: An algorithm is an idea. It is independent of any programming language. The algorithm is the solution; the code is just the implementation of that solution.
Analogy: The Recipe An algorithm is like a recipe for baking a cake. It lists ingredients (data) and gives a precise, ordered set of instructions (logic). If you follow the recipe exactly, you get a predictable result (a cake). The same recipe can be followed by an English or French-speaking chef; the language doesn’t change the underlying steps.
Programming as Problem-Solving
Programming is NOT just about writing code. It is a three-step process:
- Understand the Problem: What is the input? What is the desired output? What are the rules and constraints? Rushing this stage is the #1 cause of failure for beginners.
- Design the Algorithm: Create the step-by-step logical plan. Do this on paper or a whiteboard. This is where you actually solve the problem.
- Write the Code: Only after you have a solid algorithm do you translate your plan into a programming language like Python.
How Programs Run
From Code to Action
- We write Source Code (human-readable, e.g., Python).
- The computer’s CPU executes Machine Code (binary 1s and 0s).
- Question: How does our source code get translated into machine code?
- Answer: There are two main approaches: Compilation and Interpretation.
Approach 1: The Compiler
- Process: A compiler is a program that translates the entire source code file at once, before you run it.
- Output: It creates a new, standalone executable file (e.g.,
.exe
on Windows). - Workflow:
- Write code.
- Compile the code (this creates the executable).
- Run the executable. (If you change your code, you must re-compile).
- Examples: C, C++, Rust, Java.
Analogy: The Book Translator A compiler is like a book translator who takes a novel written in French (source code), reads the entire book, and then produces a completely new, separate book written in English (the executable file). Anyone can then read the English version without needing the original French book or the translator.
Approach 2: The Interpreter
- Process: An interpreter translates the source code line-by-line, as it is running.
- Output: No separate executable file is created.
- Workflow:
- Write code.
- Run the code directly using the interpreter.
- Examples: Python, JavaScript, Ruby.
Analogy: The Live Translator An interpreter is like a live translator at a UN meeting. When a diplomat says a sentence in French (one line of code), the interpreter immediately translates and speaks that sentence in English. You always need both the speaker (your code) and the interpreter to be present for the communication to happen.
So, How Does Python Really Work?
Python is commonly called an interpreted language, but it actually uses a clever, hybrid two-stage model.
- Stage 1: Compilation to Bytecode
- Your
.py
source code is automatically compiled into a low-level, intermediate format called bytecode.
- Your
- Stage 2: Interpretation of Bytecode
- The Python Virtual Machine (PVM) then interprets this bytecode.
Stage 1: Compilation to Bytecode
- When you run
my_program.py
, Python first compiles it into a file likemy_program.pyc
. - This
.pyc
file contains bytecode: a set of instructions optimized for the PVM. - Bytecode is lower-level than source code but is still platform-independent (it’s not machine code for a specific CPU).
Analogy: The Chef’s Mise en Place Your source code is a recipe book. A chef doesn’t read one step, do it, then read the next. They first do all the prep work (the mise en place)—chopping all vegetables, measuring all spices. Bytecode is this pre-prepped set of ingredients. It makes the final “cooking” (execution) much faster.
Stage 2: The Python Virtual Machine (PVM)
- The PVM is the runtime engine of Python; it’s the program that actually runs your code.
- It’s a piece of software that simulates a computer. It reads the bytecode line by line and executes the instructions on your actual machine’s hardware and OS.
- This is the key to Python’s portability. The same Python script (and its bytecode) can run on any OS (Windows, macOS, Linux) that has the correct PVM installed.
Analogy: The Video Game Console The PVM is like a universal video game console (e.g., a PlayStation). The bytecode is the game disc. You can take the same game disc and put it into any PlayStation in the world, and it will work. The console (the PVM) handles the details of making the game work on different TVs in different countries.
This content will be available starting September 30, 2025.