0.1 — Python as a Calculator: Numbers, Booleans, Strings

Chapter 0 · Basics of Python

Prof. Xuhu Wan

Section 0.1 · Chapter 0 · Learning Statistics with Python

Python as a Calculator: Numbers, Booleans, Strings

Basics of Python

Prof. Xuhu Wan

ISOM, HKUST Business School · 2026 Edition

Python as a Calculator: Numbers, Booleans, Strings

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.

Start with the obvious

Python evaluates expressions top to bottom and shows the value of the last one. Predict the second line.

What does this print?

print(1 + 2)
print((3 - 1) * 5 / 2)

3 then 5.0

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.

Two divisions, not one

/ 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 1
  • 3.0 -3.0 1
  • 3 -4 1
  • 3.33 -3.33 1

Confirm the floor

3.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 you

Mixing an int with a float promotes the result to float (45.14159). Python never silently truncates — but floats have their own trap, next slide.

Is 0.1 + 0.2 equal to 0.3?

What does print(0.1 + 0.2 == 0.3) print?

  • True
  • False
  • It raises a TypeError
  • 0.3

False, 0.30000000000000004, True. Every price, return and p-value in this course is a float. Never test them with ==.

Booleans are integers in disguise

True is 1, False is 0. Predict the sum.

What does the last line print?

passISOM5000 = True     # True = 1
passISOM6000 = False    # False = 0
print(passISOM5000 + passISOM6000)

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.

Strings are sequences: index and slice them

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'

Run the six slices

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.

Objects have methods: 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.

Did s.upper() change s?

After s = ” hello, world “; s.upper(), what does print(s) show?

  • ' HELLO, WORLD '
  • ' hello, world '
  • None
  • It raises an error

' 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.

Your turn: clean a string, raise a power

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 *.

What you discovered

  • / 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.
  • Slices are start-inclusive, stop-exclusive, and the same rule runs through lists and DataFrames.
  • Strings are immutable: 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.