1.4.1 · D1Python & Scientific Computing

Foundations — Python syntax, data types, control flow

2,978 words14 min readBack to topic

Before you can read a single training loop, you must be able to read the marks on the page. This note walks every symbol, keyword, and idea the parent note leans on, starting from a blank line and adding exactly one new thing at a time.


1. The box-and-label picture (a variable)

Imagine a shelf of labelled boxes. Each box holds one value. A variable is nothing more than a label stuck to a box.

Figure 1 — A single value 5 sits in a box; the amber tag x is a label tied to that box. The white arrow shows x = 5 attaching the label. This is the picture behind every variable.

Why does the topic need this? Every hyperparameter (learning_rate = 0.01), every dataset, every model lives in a labelled box. If you misread = as a question, nothing else makes sense.


2. What lives in the box (data types)

A box does not just hold "a value" — it holds a value of some kind. The kind is the type. Python looks at the type to decide what operations are legal.

Figure 2 — Eight labelled boxes, one per type the parent uses: int holds 5, float holds 0.01, str holds "Epoch", bool holds True, list holds [0.5, 0.3], tuple holds the frozen (28, 28, 1), dict holds the key→value pair {"lr": 0.01}, and None marks an explicitly empty box. Use it as a visual glossary of the kinds of value.

Now that we have numbers, we need the symbols that combine them.


3. Turning one type into another (conversion functions)

int(...), float(...), str(...) are converter machines: feed a value in, get the same value re-expressed as a new type. (Recall from the top of this note that below just means "gives".)

Why the topic needs this: in ML you constantly move between counts (int) and measured quantities (float), and between numbers and log messages (str). Reading conversions correctly prevents silent precision loss — accidentally turning float weights into ints would flatten a model.


4. Comparison and the yes/no world (operators that make bool)

Control flow needs a question to answer. Comparison operators take two values and return a bool.


5. Grouping lines by indentation (Python's grammar)

Most languages wrap a block of lines in braces { }. Python instead uses the amount of space at the start of each line. Lines pushed in by the same amount belong to the same group.

Figure 3 — The two cyan lines are indented under if accuracy > 0.95:, so the amber bracket marks them as inside the if-block; the final white line returns to the left margin and is therefore outside. Read code by its left edge.


6. Deciding which lines run (if / elif / else)

Now the pieces combine. if takes a question (a bool), and runs its block only when the answer is True.


7. Repeating lines (for, while, range)


8. One label, two boxes? (reference semantics)

The subtlest idea the parent relies on: assignment copies the label, not the contents.


Prerequisite map

variable and equals sign

data types int float str bool

type conversion int float str

comparison and truthiness

arithmetic operators plus minus star slash

if elif else decisions

indentation blocks

for while range loops

list comprehensions

reference semantics one label

Python syntax data types control flow

Everything above flows into the parent topic and, from there, into NumPy arrays and vectorization, Pandas DataFrames, Python functions and lambda, and Python iterators and generators.


Equipment checklist

Read these to yourself as questions and check you can answer before moving on.

What does x = 5 command Python to do, read right-to-left?
Create the value 5, then stick the label x onto that box; = assigns, it does not ask a question.
Difference between = and ==?
= assigns a value to a name; == compares two values and returns True or False.
What does the computer do with everything after a #?
Nothing — it is a comment, ignored when running; it is only a note for the human reader.
Difference between / and // for 10 / 4 and 10 // 4?
/ gives 2.5 (a float, keeps the fraction); // gives 2 (rounds the answer down to a whole number).
What is the result and type of int(3.7) and int(-3.7)?
3 and -3, both int — it truncates toward zero, it does not round or floor.
Why do -3 // 2 and int(-1.5) disagree?
// floors (down to -2); int(...) truncates toward zero (up to -1). They only differ for negatives.
Does 5 * 2.0 give an int or a float, and why?
A float, 10.0 — mixing any int with a float promotes the whole result to float.
What does x ** 3 mean in plain multiplication?
x * x * x — the exponent counts how many copies of x are multiplied together.
Why does "Epoch " + 5 fail, and what fixes it?
Python won't glue text and a number; wrap the number: "Epoch " + str(5).
What does 10 % 3 give and what kind of operator is %?
1; % is an arithmetic operator (remainder after division), so x % 2 == 0 tests "is x even?".
Which values are falsy?
0, 0.0, "", [], {}, and None — empty/zero things.
How does Python know which lines form a block?
By indentation — lines pushed in by the same amount after a : belong together.
In if / elif / else, which block runs?
The first one whose condition is True; the rest are skipped. else runs only if none were true.
What does range(10) produce, and why?
0, 1, …, 9 — one argument is read as stop, with start=0 and step=1 filled in by default.
After b = a for a list a, why does changing b change a?
Both names label the same list (one box); use copy.deepcopy for a separate copy.