Abstract¶
By the end of this material, readers will understand the intimate relationship between computers and programming, and appreciate the evolutions and motivations of programming languages. They will learn about basic computer architecture, binary data representation, and the structure of a Python program.
Computer¶
A calculator that is bigger and more advanced?

Figure 1:A calculator on a computer.
If so, is abacus the first computer ever invented?

Figure 2:Abacus - an ancient mechanical computing device.
Is your smartphone a computer?
Run the following cell and click the play button to ask AI for some hints:[1]
%%hermes
What defines a computer? Give me some hints but not the answer.Hints previously generated by the %%hermes cell magic. [2]
%%hermes cell magic. [2]Here are some hints:
Think about what makes a calculator different from a computer. A calculator does one fixed job — what extra capability does a computer have?
Imagine you tell someone a list of steps to follow. If you can change the list without changing the person, what property does that give them?
Consider: if you swap out one thing and suddenly your device can do something completely different, what did you swap?
These point toward the core idea without spelling it out. Want me to add these as hints in the notebook cell?
It might help to know...
Who invented the first computer?
Who invented the first computer?
What is the architecture of a computer?¶
A computer contains three main hardware components:
Input device
Processing unit
Output device
Peripherals¶

Figure 3:Computer peripherals.
Input and output devices connected to a computer are called peripherals.
They allow users to interact with the computer in different ways.
Solution to Exercise 1 #
3D printer available at CityU
%%hermes
Summarize what an output device of a computer is and explain briefly why
a monitor and a speaker are examples?An output device is a hardware component that converts processed data from the computer into a form perceivable by humans (or other systems).
A monitor displays visual data (text, images, video) — converting digital signals into light on a screen.
A speaker produces sound — converting digital audio signals into audible waves.
Both take internal digital results and present them to the user, which is exactly what an output device does.
Solution to Exercise 2 #
3D scanner available at CityU
%%hermes
Summarize what an input device of a computer is and explain briefly why
a keyboard and a mouse are examples?An input device is a hardware component that captures data or commands from the outside world (usually the user) and sends it into the computer for processing.
A keyboard translates key presses into digital signals representing characters and commands.
A mouse translates hand movements and clicks into digital signals controlling a cursor pointer.
Both convert human actions into data the computer can process — which is exactly what an input device does.
%%hermes
Is headset an input device or an output device? Give me hints but not the answer.Here are some hints:
A headset has two distinct parts — think about what each one does separately.
One part lets you hear audio from the computer. What category does that fit?
The other part captures your voice. What category does that fit?
Think about it — does one label cover all of it, or could there be a third option?
Solution to Exercise 3 #
Solid-state dive or Hard disk
CD/DVD Rom (writable)
Touch screen
Central Processing Unit¶
The brain of a computer is its processor or the Central Processing Unit (CPU). As shown in Figure 4, it is located on the motherboard and connected to different peripherals using different connectors.
For instance, you can check the CPU of your Jupyter Server by running the following shell command escaped using !:
!lscpu%%hermes
What does the above cell do?The above cell runs the !lscpu shell command, which displays detailed information about the CPU (Central Processing Unit) of the Jupyter Server — including its architecture, number of cores, model name, clock speed, and cache sizes. The ! prefix tells Jupyter to execute it as a terminal command rather than Python code.
Two important components in the CPU are:
Arithmetic and Logic Unit (ALU): Performs arithmetics like a calculator but for binary numbers. (More on this later.)
Control Unit (CU): Directs the operations of the processor in executing a program.
To visualize how CPU works, run the CPU Simulator below.
Note that all values are zeros in the RAM (Random Acess Memory) initially.
Under Settings, click
ExamplesAdd two numbers. Observe that the values in the RAM have changed.Click
Runat the bottom right-hand corner.
As you can see, in addition to performing arithmetic calculations,
a computer can be programmed
to perform different tasks.
Can we simply say a computer is a programmable device? If so, what is programming?
%%hermes
Explain very briefly how a universal machine look like.Done. Added a new markdown cell explaining the universal machine — three components (tape, read/write head, rule set) and why it’s “universal.” It sits right after the %%hermes question cell.
See Also
A universal machine (Turing machine) has three simple parts:
An infinitely long tape divided into cells, each holding a symbol (e.g., 0 or 1).
A read/write head that scans one cell at a time, reading the symbol and optionally overwriting it.
A finite set of rules (the program) that tells the machine: given the current symbol and internal state, what to write, which way to move (left or right), and what state to enter next.
Despite this simplicity, by changing the rules (the program) on the tape, the same machine can compute any computable function — which is why it is called “universal”.
%%hermes
How the problem of decidability has to do with definability and computability?See Also
The three concepts are tightly intertwined:
Definability asks whether a mathematical object (e.g., a set of numbers) can be precisely described using a formal language.
Computability asks whether there exists an algorithm — a mechanical procedure a machine can follow — to produce or decide that object.
Decidability asks whether, for any given input, there is an algorithm that always halts with a correct yes/no answer.
Turing showed that not every definable set is computable. His proof that the Halting Problem is undecidable means there are well-defined questions for which no algorithm can ever give a definitive answer — establishing a fundamental limit on what computers can do.
Programming¶
What is programming?¶
What defines programming?
Programming, also called software development, is the process of writing programs for the computer to execute different tasks. But what is a program?
Solution to Exercise 4 #
The first six lines of binary sequences in the RAM. The last line Ends the program.
%%hermes
As a hint, summarize what a machine language is and give an example of a program
in machine language.Done. Added the machine language hint as a markdown cell right after the %%hermes question, explaining binary opcodes with a concrete example that ties back to the CPU simulator’s RAM.
Hint
Machine language is the lowest-level programming language: a sequence of
binary codes that the CPU can execute directly, with no translation needed.
Each binary pattern (opcode) represents one instruction, such as Add,
Store, Jump, or End.
For example, the CPU simulator’s “Add two numbers” program might look like:
0000000000000001 ; Load value at address 1 into register A
0001000000000010 ; Add value at address 2 to register A
0010000000000011 ; Store register A into address 3
0011000000000000 ; End programEvery line is a binary number — the CPU reads each one as an instruction and its operand. This is what is stored in the RAM of the simulator.
The CPU is capable of carrying out
a set of instructions such as
Add,Subtract,Store, etc.,for some numbers stored in the RAM.
Both the instructions and the numbers are represented as binary sequences.
Why computer uses binary representation?¶

Figure 5:Programmers controlling the switches of ENIAC.
Notebook Cell
### BEGIN SOLUTION
2 ** 10 # because there are that many binary sequences of length 10.
### END SOLUTION%%hermes
As a hint, explain the multiplication rule in combinatorics.Hint
The multiplication rule (or rule of product) says: if you have a sequence of independent choices, the total number of outcomes is the product of the number of options at each step.
For example, if there are 10 switches and each can be either On or Off
(2 choices), the total number of distinct configurations is
The solution cell to the above exercise is a code cell, so why not use the
exponentiation operator ** to calculate the value?
Why use binary numbers?
Unlike ENIAC, the subsequent design of the Electronic Discrete Variable Computer (EDVAC) utilized the binary number system. The decision was explained in Section 5.1 of the design report authored by John von Neumann in 1945. While humans are accustomed to using the decimal number system, von Neumann justified the use of binary by noting that human neurons operate in a binary manner:
The analogs of human neurons ... are equally all-or-none elements. It will appear that they are quite useful for all preliminary, orienting, considerations of vacuum tube systems ... It is therefore satisfactory that here too the natural arithmetical system to handle is the binary one.
— John von Neumann
There are International Standards for representing characters:
There are also additional standards to represent numbers other than non-negative integers:
2’s complement format for negative integers (e.g. -123)
IEEE floating point format for floating point numbers such as .
%%hermes
In addition to the ones in the above cell, what are other standards for
representing characters/numbers in computers.See Also
Beyond ASCII, Unicode, 2’s complement, and IEEE 754 floating point:
Characters:
EBCDIC — IBM’s legacy encoding for mainframes (8-bit, different mapping than ASCII).
UTF-8 / UTF-16 / UTF-32 — Specific Unicode encoding schemes (variable-width vs. fixed-width trade-offs).
ISO-8859-x (Latin-x) — Extended 8-bit character sets for European languages that bridge ASCII and full Unicode.
Numbers:
BCD (Binary-Coded Decimal) — Each decimal digit is stored as a separate 4-bit binary nibble; used in financial systems to avoid floating-point rounding errors.
Fixed-point representation — Scales fractional values by a constant factor (e.g., Q16 format), common in embedded DSP and audio processing.
Decimal128 (IEEE 754-2008) — A 128-bit decimal floating-point format for precise financial calculations.
Why define different standards for representing characters/numbers?
Different standards have different benefits.
ASCII requires less storage for a character, but it represents less characters.
Although digits are also represented in ASCII, the 2’s complement format is designed for more efficient arithmetic operations.
Generations of programming languages¶
Are we going to start with machine language? Start with learning 2’s complement and the binary codes for different instructions?
No. Programmers do not write machine codes directly because it is too hard to think in binary representations.
Instead, programmers write human-readable mnemonics such as ADD, SUB, ...
This is called the assembly language. An example is shown in Figure 6.

Figure 6:A Code written in an assembly language.
%%hermes
What is the assembly language most commonly used by programmers and what is its
main application?See Also
There isn’t a single “most common” assembly language — each CPU architecture has its own. The two most widespread today:
x86/x86-64 assembly — Dominates desktops and servers (Intel/AMD). Used for OS kernels, device drivers, and performance-critical code like game engines or cryptography.
ARM assembly — Dominates mobile devices and embedded systems (smartphones, IoT). Also used in Apple Silicon Macs. Used for low-level hardware control, bootloaders, and power-efficient optimized code.
Main application: Assembly is rarely written from scratch by programmers today. Its primary use is in low-level system programming — operating systems drivers, firmware/BIOS, performance-critical optimization where every cycle matters, and reverse engineering/security research. Most programmers rely on compilers that translate high-level languages down to the appropriate assembly automatically.
%%hermes
Explain the different instructions used in the different instruction set
architectures including AMD, ARM, and RISC-V.See Also
These three architectures differ fundamentally in their design philosophy:
AMD x86/x86-64 (CISC — Complex Instruction Set Computer)
Variable-length instructions (1 to 15 bytes)
Instructions can perform multi-step operations in one step, e.g.,
ADD [mem], regreads memory, adds, and writes back simultaneously.Rich addressing modes allow complex memory access patterns directly inside an instruction.
Example instructions:
MOV,ADD,PUSH,CALL,CMOVcc(conditional move), plus SIMD extensions likeSSE/AVXfor parallel math on vectors.
ARM (RISC — Reduced Instruction Set Computer)
Fixed-length instructions (32-bit in ARMv7/ARMv8)
Strict load/store architecture: arithmetic only operates on registers; memory access requires explicit
LDR/STRinstructions.Fewer, simpler instructions that execute in a single cycle.
Example instructions:
ADD,SUB,LDR,STR,B(branch),BL(branch with link/call). Registers are namedX0–X30(AArch64), plus dedicated stack pointer (SP) and program counter (PC).
RISC-V (Open-source RISC)
Fixed 32-bit base instructions with optional extensions (
Mfor multiply,Afor atomics,F/Dfor floating point).Same load/store philosophy as ARM, with uniform register names: general-purpose registers are simply numbered
x0–x31. Notable convention: register zero is hardwired to always return 0.Modular and extensible by design — you pick only the features your hardware needs.
Example instructions:
add,sub,lw/sw(load/store word),beq/bne(branch if equal/not equal), jal/jalr (jump-and-link).
Summary: CISC packs more work into fewer but complex instructions; RISC breaks everything into simple steps that run fast and compile cleanly.
Are you going to learn an assembly language?
Both machine language and assembly language are low-level languages which are
difficult to write for complicated tasks (requiring many lines of code), and
platform-specific, i.e.,
the sets of instructions and their binary codes can be different for different types of CPUs, and
different operating systems use different assembly languages/styles.
Who needs to learn assembly languages?
Probably for programmers who need to write fast or energy-efficient code such as
a driver that controls a 3D graphics card, or
a program that control a microprocessor with limited power supply.
But even in the above cases, there are often better alternatives. Play with the following microprocessor simulator:
Open https://
micropython .org /unicorn/ in a browser. Click
CHOOSE A DEMOLED.Click
RUN SCRIPTand observes the LED of the board.Run the demos
ASSEMBLYandMATHrespectively and compare their capabilities.
High-level Language¶
What is a high-level language?
The art of programming
Programmers often write in human-readable languages such as
Python
Rust
C/C++
Java
JavaScript
Typescript
Pascal
Basic
HTML
PHP
...
called high-level languages.
A program written in a high-level language gets converted automatically to a low-level machine code for the desired platform.
This abstracts away low-level details that can be handled by the computer automatically.
For instance, a programmer needs not care about where a value should be physically stored if the computer can find a free location automatically to store the value.
Different high-level languages can have different implementations of the conversion processes:
Compilation means converting a program well before executing the program.
E.g., C++ and Java programs are compiled.Interpretation means converting a program on-the-fly during the execution of a program.
E.g., JavaScript and Python programs are often interpreted.
Compiled vs interpreted languages
Roughly speaking, compiled programs run faster but interpreted programs are more flexible and can be modified at run time. The truth is indeed more complicated. Python also has a compiler, and recently, a JIT compiler. Is Python really an interpreted language?
%%hermes
Explain in one or two paragraphs whether Python is really an interpreted
language, given that it also has a compiler and, recently, a JIT compiler, and
it can be run as a script like a scripting language.See Also
Python compiles source code into an intermediate form called bytecode, which is then interpreted by the CPython virtual machine. So it is both compiled and interpreted — the compilation happens automatically behind the scenes, and users never see the bytecode. The recent experimental JIT compiler takes another step by compiling hot bytecode paths into native machine code at runtime, similar to how Java or JavaScript engines work. Despite all this, calling Python an “interpreted” language remains useful shorthand: from the programmer’s perspective, there is no explicit compile step — you just run the script directly, making it feel like a scripting language.
You will learn to program in Python, which is a popular language. (See TIOBE (The Importance of Being Efficient) Programming Community Index).
%%hermes
What is the popularity of Python according to the TIOBE?See Also
Python has held the #1 position on the TIOBE index since October 2021, making it the most popular programming language according to that index. It currently maintains a healthy lead over its nearest competitors (typically C, C++, and Java), with its popularity driven by its widespread use in data science, machine learning, web development, and education.
Why Python?
Python is:
expressive and can get things done with fewer lines of code as compared to other languages.
popular. It has an extensive set of libraries for Mathematics, graphics, AI, Machine Learning, etc.
free and open-source, so you get to see everything and use it without restrictions.
portable, i.e., the same code runs in different platforms without modifications.
How does a Python program look like?
%%pytutor --input 2026
import datetime # library to obtain current time
cohort = input("In which year did you join CityU? [e.g., 2020]")
year = (
datetime.datetime.now().year # current year
- int(cohort)
+ (1 if datetime.datetime.now().month >= 9 else 0) # Why use current month?
)
print("So you are a year", year, "student.")A Python program contains statements just like sentences in natural languages. E.g.,
cohort = input("In which year did you join CityU? ")which obtains some information from user input.
For the purpose of computations, a statement often contains expressions that evaluate to certain values. E.g.,
input("In which year did you join CityU? ")is an expression with the value equal to what you input to the prompt.
That value is then given the name cohort.
Expressions can be composed of the following objects:
Functions such as
input,now, andint, etc., which are like math functions the return some values based on its arguments, if any.Literals such as the string
"In which year did you join CityU? "and the integer1. They are values you type out literally.Variables such as
cohortandyear, which are meaningful names to values.
To help others understand the code, there are also comments that start with #.
These are meant to explain the code to human but not to be executed by the computer.
Solution to Exercise 6
Programming using natural language. A step towards this direction: Vibe coding.
Write programs that people enjoy reading, like literate programming. A step towards this direction: nbdev.
Literate programming
Specify a goal and let an AI agent plan, code, execute, debug, and iterate autonomously — agentic coding. This goes beyond asking AI to write code: the agent forms a plan-execute-reflect loop.
%%hermes
What do you think the next generation programming should be?🌿 Fork/new: main (will create on click)
Agentic Coding¶
What is agentic coding?¶
In traditional programming (3rd generation), you write step-by-step instructions for the computer to follow. The computer executes them literally but has no understanding of your goal.
In agentic coding, you specify a goal — “build a webpage that shows weather data” — and an AI agent plans, writes code, runs it, debugs errors, and iterates until the goal is met. You shift from writing every line to specifying outcomes and supervising the process.
Table 1:Comparing programming paradigms
| Generation | Paradigm | You say... | Computer does... |
|---|---|---|---|
| 1st | Machine language | 01000001 (binary) | Executes the exact binary operation |
| 2nd | Assembly | ADD R1, R2 | Adds two registers |
| 3rd | High-level (Python) | for i in range(10): print(i) | Loops and prints — follows your steps |
| 4th? | Agentic | “Create a graph showing prime number distribution” | Plans, codes, runs, debugs, delivers |
How does an agent work?¶
An agentic coder follows a plan-execute-reflect loop:
Plan — Break the goal into sub-tasks
Write — Generate code for each sub-task
Execute — Run the code and observe output or errors
Reflect — If errors or incomplete results, revise and repeat
The key difference from just asking AI to “write code” is the iteration loop: The agent runs its own code, reads error messages, and fixes problems autonomously. You act as the product manager, not the line-by-line implementer.
Try it yourself¶
Run the cell below. Describe a small programming task in natural language and observe how the AI agent responds:
%%hermes
Task: Write a Python function that takes a list of words and returns a
dictionary counting the frequency of each word, case-insensitive.
Then test it with ["Hello", "World", "hello", "WORLD", "Python", "PYTHON"].↻ Session: main (ACP: 1e68edf8-554…)
Solution to Exercise 7 #
Agentic coder: Quick data analysis, web scraping, generating boilerplate, exploring new libraries — tasks where speed matters more than perfect control.
Traditional Python: Core algorithms for assignments, code where you need to understand every line for learning, or debugging when something goes wrong — tasks where understanding is the goal.
You can run a cell by selecting the cell and press Shift + Enter.
Click me to see the hints.
To edit a markdown cell, double click the cell until you see a cursor. Run the cell to render the markdown code.
The CPU simulator follows the von Neumann architecture, which realizes Turing’s conception of the stored-program computer, where program and data are stored in the same memory. This is in contrast to the Harvard architecture, which uses separate memories for program and data.
- Turing, A. M. (1937). On Computable Numbers, with an Application to the Entscheidungsproblem. Proceedings of the London Mathematical Society, s2-42(1), 230–265. 10.1112/plms/s2-42.1.230
- von Neumann, J. (1993). First draft of a report on the EDVAC. IEEE Annals of the History of Computing, 15(4), 27–75. 10.1109/85.238389

