Chapter 0 · Basics of Python
Section 0.2 · Chapter 0 · Learning Statistics with Python
Basics of Python
Prof. Xuhu Wan
ISOM, HKUST Business School · 2026 Edition
Four ways to hold many values. You will predict which ones can be edited in place, which ones have positions, and — the trap that bites every analyst once — what happens to a when you change b.
Predict the first line before you run.
[20, 30] → [10, 20, 30, 40, 50] → [10, 15, 20, 30, 40] removed: 50 → 10 40 [15, 20]. append, insert, pop all edit the list in place — no assignment needed.
Two names, one list. Predict before running — this one costs people real money.
nums = [10, 20, 30, 40]; alias = nums; alias.append(50). What is nums now?
[10, 20, 30, 40]nums is protected[10, 20, 30, 40, 50][50, 10, 20, 30, 40]nums: [10, 20, 30, 40, 50] alias: [10, 20, 30, 40, 50] then nums: [10, 20, 30, 40, 50] copy: [10, 20, 30, 40, 50, 15]. Assignment never copies; .copy() does. In Chapter 2 this returns as the SettingWithCopyWarning.
is asks about identity; == asks about valuenums = [10, 20, 30, 40]; alias = nums; fresh = [10, 20, 30, 40]. What do nums == fresh, nums is fresh, alias is nums give?
True True TrueTrue False TrueFalse False TrueTrue False FalseTrue False True. Use is only for None (x is None); use == for everything else.
Parentheses instead of brackets, and one difference that matters. Predict.
coords = (34.05, -118.25); coords[0] = 0. What happens?
coords becomes (0, -118.25)coords becomes (0,)ValueErrorTypeError — tuples do not support item assignmentTypeError: 'tuple' object does not support item assignment. Unpacking (lat, lon = coords) is how you will receive the (sharpe, max_drawdown) pair from PerformanceMeasure in the projects.
{1, 2, 3} 3, True, {1, 2, 3, 4, 5}, {2}, {1, 2, 3}. set(tickers) is the one-line answer to “how many distinct tickers are in this file?”
What does bag[0] do for a set bag?
TypeError: 'set' object is not subscriptable0A pandas Series is a dictionary that learned arithmetic; a DataFrame is a dictionary of columns. .keys(), .values(), .items() return in insertion order (Python ≥ 3.7).
Before “gpa” is added, student = {“name”: “Alex”, “age”: 20, “skills”: […]}. What do student[“gpa”] and student.get(“gpa”, 0.0) do?
NoneKeyError; the second returns 0.0KeyError0.0; the second raises KeyErrorKeyError: 'gpa', then 0.0, then None. When df["Volumn"] throws a KeyError in Chapter 2, it is this exact mechanism — and the fix is a typo, not a .get.
my_list.__class__, df.shape, df.columns.(): my_list.append(3), text.upper(), df.head().text = “tiger”. What is text.isalpha (no parentheses)?
TrueFalseSyntaxError<class 'list'>, True, True, builtin_function_or_method. Rule for pandas: df.shape and df.columns (no brackets); df.head() and df.mean() (brackets). Forget the brackets and you print <bound method …> instead of a number.
The notebook’s mini-exercise. From the list of 5 numbers nums5: (1) slice the middle three into middle3; (2) convert it to a set — what happens to the duplicate 20?; (3) build stats5 = {'min': …, 'max': …} with min() and max().
append, insert, pop); tuples refuse (TypeError); strings refuse too.alias = nums is a second name, not a copy — use .copy() when you want independence.== compares values, is compares identity; reserve is for None.bag[0] fails); sorted(bag) gives you a list.d[key] raises KeyError on a missing key; d.get(key, default) does not.df.shape vs df.head().Next: §0.3 — making decisions and repeating them: if, for, while, and the comprehension.
Prof. Xuhu Wan · HKUST ISOM · Learning Statistics with Python