Module 01 - Programming, with Python

Why learning (even a little) programming is good for you #

Software is everywhere, not a day goes by without you interacting with software

Why learn programming? #

Later, every time you encounter a new problem, ask yourself this question: “How would I solve it by writing a program?”

Programming vs coding #

“People confuse programming with coding. Coding is to programming what’s typing is to writing.” (Leslie Lamport)

What is coding ? (Oxford Dictionary) #

“To convert into a code. To express in an indirect way. To encode

What is programming ? (Oxford Dictionary) #

“To apply a set of instructions to control the behavior of a computer”

Programming vs Coding #

bg right contain

Coding is cheap (especially in the era of gen AI), programming isn’t!

Programming without coding #

There is (and always be!) code underneath…But it’s not your problem!

Programming is not (really) about computers #

bg right contain

“Computer science is a terrible name for this business… First of all, it’s not a science… It’s also not really very much about computers” (Harold Abelson, MIT 6.001 Structure and Interpretation, 1986). Watch the first episode of this course to have a wonderful introduction to programming

Programming is about processes, building how-to knowledge.

Example of a program #

We want to solve this problem:

Find the youngest person into a group.

Two steps work #

  1. Find the correct process
  2. Program a computer to execute it

Find the process #

We have to design a process/a procedure (a series of instructions/actions) to solve this problem.

Program: an automatic and executable procedure

Find the youngest person program, first try #

  1. For each person in the group:
    1. Ask for their birth date and their name.
    2. If their birth date is more recent than the previous youngest person’s (or if it’s the first person we ask), consider this person the youngest person in the group.

First analysis #

Let’s do this again using a piece of paper as memory, to keep track of the correct answer.

Find the youngest person program, second try #

bg right contain w: 50%
  1. Take a piece of paper and write none for “Youngest Person” and “Birthdate”.
  2. For each person in the group:
    1. Ask for their birth date and their name.
    2. If their birth date is more recent than the one written on the paper (or if it’s the first person), update the paper: erase the old info and write down this new person’s name and birth date.
  3. Show the name written on the paper.

Find the youngest person program, a graphical view #

bg right contain

Analysis #

Our procedure has two parts: Data and Instructions #

bg right contain

Our program is made of two parts:

What could go wrong in the manual procedure? #

Programming is like life: if things can go wrong, (given enough time) they will go wrong!

Code and automate the process #

Once figured it out, we can code (encode, translate) this process into a programming language, like the Python language (or any other language!), to automate its execution with a computer.

Any program can be encoded into different programming languages

Why turning our procedure into a program? #

Because we want:

In short: time savings, no human error and flexibility.

How to turn our procedure into a program? #

But first, what is (really) a computer? #

How do we program the computer?

To answer this question, a few words about computers:

Properties of a modern computer #

Note: They are still “slow” compared to biological systems like cells, where billions of complex chemical reactions happen in parallel.

Bits don’t Byte #

bg right contain

What is a computer ? #

Ex: 00010101 10110101 machine code for a given machine could result in the action “write the value 55 to memory” or “compare 72 to 16”.

Von Neumann Architecture #

Prior, machines required physical rewiring to switch programs (One problem to solve = One machine). The Von Neumann architecture allows computers to run any program simply by loading new binary instructions into memory! This is Software!

Von Neumann architecture remains the standard computer architecture used today!

Fundamentals of Von Neumann Architecture #

bg right contain

Computers has four major components:

Execution cycle (simplified) #

bg right contain
  1. Fetch: CPU reads next machine code instruction, from a program loaded in memory
  2. Decode what kind of instruction
  3. Execute: sequence of actions to perform the instruction (ex: Add, Compare, etc.), defined by the instruction set of the CPU.
  4. Write: write the result back to memory
  5. Go back to step 1.

At the end, memory changes (bytes are written, new values are created): program execution

The CPU clock sets the pace, ticking billions of times per second (GHz) to trigger each step of the fetch-decode-execute cycle.

What is program execution at low level (machine level) #

How to store things in computer memory ? #

Computer memory is a giant array of bytes (8-bit boxes)

How to put things into computer memory?

We need to encode things in binary, following encoding systems (standardized).

Encoding things in binary #

#Integers
1           -> 00000001                                (1 byte integer)
2           -> 00000010
255         -> 11111111

#Decimal (floating point number)
3.14        -> 01000000 01001000 11110101 11000011     (32-bit Float: Sign + Exponent + Mantissa)

#Text: different encoding systems exist
'A'         -> 01000001                                (in ASCII) 
'😀'        -> 11110000 10011111 10011000 10000000     (in UTF-8)

#Pixel: every pixel consists of 3 bytes for Red, Green and Blue colors:
Pure Red    -> 11111111 00000000 00000000              (RGB, 3 bytes: R=255, G=0, B=0)
Pure Blue   -> 00000000 00000000 11111111              (RGB, 3 bytes: R=0, G=0, B=255)

#Image: A big array of pixels 
11111111 00000000 00000000 11111111 00000000 11111111 00000000 00000000 11111111 00000000 00000000 
00000000 11111111 00000000 00000000...

#Movie : A big array of images...

Everything (text, photos, programs, etc.) is ultimately just (m|b)illions of zeros and ones (binary data) being manipulated by hardware components

How to store a bit (for real!): a few examples of binary objects #

Beacons of Gondor #

bg right contain

Important binary objects

Candle #

bg right contain w: 50%

For many obvious reasons, computers doesn’t use candles for storing bits

Vacuum tube #

bg right contain

Magnetic tore (e.g. used by Apollo Guidance Computer) #

bg right contain bg right contain

Total Apollo Guidance Computer memory was 4 KB. AGC is real software engineering

Transistor (used in modern computers!) #

w: 200% bg right contain w: 50%

Typical Dynamic RAM module (Corsair DDR2-533) :

Reminder #

40’s/50’s - Mainframes : ENIAC #

bg right contain

ENIAC, 1945-1950 (USA)

Evolution of Computing: From the 1940s to Today 1/2 #

Machine Year Processor / GHz RAM Storage Price (EUROS)
ENIAC 1945 ~0,00001 GHz 20 Kb Magnetic drum 20Kb ~ > 500 000 EUROS
PDP-11 1970 ~0,001 GHz 4 Kb to 256 Kb Magnetic tape 10 to 40 Mb ~ 250 000 EUROS
Apple II 1977 0,001 GHz (1 MHz) 4 Kb to 48 Kb Tape / 140 Kb to 5 Mb ~ 1300 EUROS
ComMbdore 64 1982 0,001 GHz (1 MHz) 64 Kb Tape / 170 Kb to 5 Mb ~ 550 EUROS
IBM PC 1981 0,00477 GHz (4,77 MHz) 16 Kb to 640 Kb Hard drive 10 to 20 Mb ~ 1500 EUROS
IMac G3 1998 0,233 to 0,5 GHz 32 to 512 Mb 4 to 20 Gb ~ 1300 EUROS
BlacKberry 2003 0,065 to 0,12 GHz 2 to 16 Mb 16 to 64 Mb ~ 500 EUROS
IPhone 2007 0,412 GHz 128 Mb 8 to 16 Gb ~ 600 EUROS
Lenovo ThinkPad Mbderne 2025 3 to 5 GHz (multi-core) 16 to 64 Gb 512 Gb to >4 To 2500 EUROS

Evolution of Computing: From the 1950s to Today 2/2 #

Today, a modern mainstream laptop, such as a Lenovo ThinkPad:

than a computer like the 1945 ENIAC (the first fully programmable computer)!

Welcome to the Space Age!

What has changed over 80 years #

What has BARELY/NOT changed over 80 years #

How to encode our procedure ? 1/2 #

How to encode our procedure ? 2/2 #

^  -> Python, PHP, Lisp, Java, R, Fortran, etc.
|  ----------------------
|  Higher level languages (closer to humans)
|  ----------------------
|  - C Language : on the border between lower and higher level languages
|  ---------------------
|  Lower level languages  (closer to hardware)
|  ---------------------
|  - Assembly languages (last human readable programming language before binary)
|  - Machine code (just bits)

Same program, different codes, an example #

Let see the same program written in different codes:

Example of Machine code (lowest level language) #

01111111 01000101 01001100 01000110 00000010 00000001
00000001 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000011 00000000
00111110 00000000 00000001 00000000 00000000 00000000
01010000 00010000 00000000 00000000 00000000 00000000
00000000 00000000 01000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 10011000 00110110
00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 01000000 00000000
00111000 00000000 00001101 00000000 01000000 00000000
00011111 00000000 00011110 00000000 00000110 00000000

Example of Assembly Code (low level language) #

.LC0:
 .string "c=%d"
 .text
 .globl main
 .type main, @function
main:
.LFB0:
 .cfi_startproc
 pushq %rbp
 .cfi_def_cfa_offset 16
 .cfi_offset 6, -16
 movq %rsp, %rbp
 .cfi_def_cfa_register 6
 subq $16, %rsp
 movl $2, -4(%rbp)
 movl $21, -8(%rbp)
 movl -4(%rbp), %eax
 imull -8(%rbp), %eax
 movl %eax, -12(%rbp)
 movl -12(%rbp), %eax
 movl %eax, %esi
 leaq .LC0(%rip), %rax
 movq %rax, %rdi
 movl $0, %eax
 call printf@PLT
 movl $0, %eax
 leave
 .cfi_def_cfa 7, 8
 ret
 .cfi_endproc

Example of C code (“higher” level language) #

#include<stdio.h>

int main(){
    int a = 2;
    int b = 21;
    int c = a * b;
    printf("c=%d", c);
}

Example of Python code (higher level language) #

a = 2
b = 21
c = a * b
print(f"c={c}")

Lower level languages #

Hardware details are exposed (CPU architecture, registers, memory addresses). Memory is managed explicitly. Perfect for embedded systems. Examples: Assembly languages, C/C++ (from a modern perspective).

Higher level languages #

Abstract away hardware details. Focus on problem-solving logic rather than how the computer works (CPU, RAM). Examples: Java, JavaScript, Lisp, Python!, etc.

Python IS higher level language

Interpreted and Compiled languages #

Python IS an interpreted language

Python is an interpreted language #

A special program, called the Python Interpreter will translates and executes our Python code into machine code

bg right contain

Python: a higher level, dynamic, interpreted language #

In this course, we will use the Python Programming language to write our programs.

We need to learn its language syntax to code our procedures.

Syntax changes slightly from one language to another, concepts and fundamentals don’t! Without knowing, by learning Python, you will be learning many other languages at the same time!

Python #

Python is an excellent language to learn programming!

What you can do with Python? #

Python is a general purpose programming language. It’s used:

It’s a good choice!

Setting up our programming environnement 1/2 #

What we need:

Setting up our programming environnement 2/2 #

bg right contain w: 30%

We will use the PyCharm, an Integrated Development Environment (IDE), maintained by Jet Brains (Community Version).

  1. Download and install PyCharm on your machine.
  2. Launch PyCharm
  3. Create a new script script.py and run it. See the result in the terminal.

Why use PyCharm ? #

PyCharm is a sophisticated program! We will only use basic functionalities for this course.

Programming in PyCharm #

bg right contain
# In the terminal, inspect Python version installed (3+)
python -V
Python 3.14.7
# Run a program written in my_program.py
python my_program.py

Be sure to be able to master the full cycle: write/read code, run your program, see the output

Our “Find the youngest person in the group” program… in Python #

Disclaimer:

At the end of the next module you will be able to write such programs yourself!

Run “Find the youngest person in the group” program #

  1. Copy/Paste the program in a new script called find_youngest.py
  2. Run it in PyCharm and check the result in the terminal
from datetime import datetime

# Input data {name: "YYYY-MM-DD"}
group = {
    "Alice": "1995-04-12",
    "Bob": "2002-11-23",
    "Charlie": "1988-08-05",
}

youngest_name = None
youngest_date = None

for name, date_str in group.items():
    # Convert string to datetime object (strptime(): string "p"arsing time)
    birth_date = datetime.strptime(date_str, "%Y-%m-%d")
    if youngest_date is None or birth_date > youngest_date:
        youngest_name = name
        youngest_date = birth_date
print(f"The youngest person is {youngest_name}, born on {youngest_date.strftime('%Y-%m-%d')}.")

Make some changes #

  1. Add two new members to the group:
- Frank, born on September 1st, 2011 ("2011-09-01")
- Grace, born on March 20th, 1975 ("1975-03-20")
  1. Run the program. Check the result.
  2. Now, we would like to find the oldest person instead of the youngest:
    1. Copy the program to a new file called find_oldest.py
    2. Modify the code so it finds and displays the oldest person in the group. Hints:
      1. Rename the variables from youngest_… to oldest_….
      2. Change the > operator to < in the if statement (an older birth date comes before in time, so it is smaller).
      3. Update the sentence in the print() statement in order to make sense. Run and check the result.

Making mistakes and obtaining errors is OK! It is the best and only way to learn. Be sure to read carefully errors thrown by the interpreter

Discussion #

See the Find the oldest person python program

On the usage of gen. AI (LLMs) #

Don’t worry, you will have PLENTY OF TIME to be productive in your (future) job! We are here to LEARN, not to be productive.

Learning Space vs Production Space #

Next Module #

Go to next module

Going Further/Extra information #

About Von Neumann, the man from the future #

bg right contain

Von Neumann has made major contributions to quantum mechanics, functional analysis, mathematical logic, quantum logic, computer science, economics, etc! He also participated in U.S. military programs (Manhattan Project).

See The Man from the Future: The Visionary Life of John von Neumann, by Ananyo Bhattacharya, 2021, Allen Lane

Dynamic vs static languages #

The difference comes down to when variable types are checked: at compile time (before running) or at runtime (while running).

Python IS a (strong) dynamically typed language

Demonstration: from high level code to binary code in C #

Compilation of a C program with gcc compiler.

  1. High level C code (main.c)
  2. After compilation (cc -S main.c), assembly code (main.s)
  3. After assembling (cc main.c), binary code (a.out). See binary code in hexadecimal format with hexdump -C a.out

See full demo and documentation