Chapter 0 · Basics of Python
Section 0.1 · Chapter 0 · Learning Statistics with Python
Basics of Python
Prof. Xuhu Wan
ISOM, HKUST Business School · 2026 Edition
You already know arithmetic. What you will predict here is where Python’s arithmetic differs from the calculator on your desk — one division operator that always returns a float, one that rounds down, a True that is secretly 1, and a text type that behaves like a list you cannot edit.
Python evaluates expressions top to bottom and shows the value of the last one. Predict the second line.
3, 5.0, 1024, 1. The .0 on the second line is the first trap: / always returns a float, even when the answer is a whole number.
/ is true division; // is floor division. Commit to an answer for the negative case before running.
What does print(10 // 3, -10 // 3, 10 % 3) print?
3 -3 13.0 -3.0 13 -4 13.33 -3.33 13.3333333333333335, then 3 -4 1. Floor division rounds down — -3.33 floors to -4. In finance you will use // to count whole lots (shares // 100) and % for the odd lot (shares % 100); get the sign wrong on a short position and the lot count is off by one.
int, float, and what type() tells youMixing an int with a float promotes the result to float (45.14159). Python never silently truncates — but floats have their own trap, next slide.
0.1 + 0.2 equal to 0.3?What does print(0.1 + 0.2 == 0.3) print?
TrueFalseTypeError0.3False, 0.30000000000000004, True. Every price, return and p-value in this course is a float. Never test them with ==.
True is 1, False is 0. Predict the sum.
What does the last line print?
1
1, then <class 'bool'> 2. Because booleans add, sum(returns > 0) counts the up-days in Chapter 1 — a bool is a subclass of int.
s[start:stop:step] — start inclusive, stop exclusive. Predict two of the six lines.
With s = ‘data Analysis’, what are s[0:4] and s[::2]?
'data' and 'dt nlss''data ' and 'dt nlss''data' and 'aaAayi''dat' and 'dt nlss'd, s, data, data, Analysis, dt nlss. Exactly the same rule slices lists, tuples, Series and DataFrames — df.iloc[0:4] is four rows, not five.
object.method(args)Every value in Python is an object, and each object carries functions that belong to it. You call them with a dot.
split is the one to remember: it turns one string into a list of strings — the first step of the text mini-project in §0.4.
s.upper() change s?After s = ” hello, world “; s.upper(), what does print(s) show?
' HELLO, WORLD '' hello, world 'None' hello, world ', then ' HELLO, WORLD '. This is the pandas habit too: df.dropna() returns a new frame; the old one is unchanged until you assign.
Turn s into 'HELLO, WORLD' (strip the spaces, then upper-case — chain two methods), and set power to \(2^{10}\) using the exponentiation operator, not *.
/ always returns a float; // floors toward \(-\infty\) (-10 // 3 is -4); % is the remainder.0.1 + 0.2 != 0.3 — compare floats with a tolerance, never ==.True + True is 2: booleans are integers, which is why sum(condition) counts.s.upper() returns a new string; keep it with s = s.upper().Next: §0.2 — containers, and the difference between a copy and a second name for the same object.
Prof. Xuhu Wan · HKUST ISOM · Learning Statistics with Python