MA-PY1 · Module 1.1
Expressions and numeric types
Topics: Python · Expressions · Arithmetic
Overview
Module 1.1 — Expressions and numeric types
Scroll Core ideas → worked snippets → Practice (quiz). Attempt each question before Discuss / reveal. Use any Python REPL, script, or notebook you like.
Concepts
Core ideas — expressions, arithmetic, types
Python programs are built from expressions (things that evaluate to values) combined with statements (things that do something: assign names, loop, define functions…). Early on you mostly compute expressions in the interpreter or assign their results to names.
Day zero: running code, names, and printing
If this is your very first contact with Python, start here; the arithmetic below will make more sense afterward.
What Python does. Python reads your instructions one line at a time and carries them out. You can type lines into an interactive prompt (the REPL — you'll see >>>) and get an answer instantly, or save lines in a file ending in .py and run the whole file at once. Either way the rules are identical, so use whichever feels comfortable.
Naming a value with =. The single equals sign is assignment: it stores a value under a name so you can reuse it.
price = 4 # the name `price` now refers to the integer 4
quantity = 3
total = price * quantity # names can be combined in expressions
Read price = 4 as "let price be 4," not as a maths equation. The name on the left receives whatever the right-hand side evaluates to. You can reassign a name at any time, and it simply points at the new value.
Seeing a result with print(). In a saved file, computing a value does not show it — you have to ask. print(...) writes its contents to the screen:
print(total) # shows: 12
print("Total:", total) # shows: Total: 12
(In the interactive REPL, typing total alone also echoes its value, but print works everywhere, so prefer it while learning.)
Asking what something is with type(). Every value has a type — the kind of thing it is. You can inspect it:
print(type(4)) # <class 'int'>
print(type(4.0)) # <class 'float'>
print(type("hi")) # <class 'str'>
Types matter because they decide how operators behave (you will see below that / turns whole numbers into decimals). When a result surprises you, checking its type is often the fastest first diagnosis.
Numbers and literals
- An integer literal like
42or-99has typeint. Python’s integers can grow as large as memory allows—they are not fixed to 16 or 32 bits. - A floating-point literal always contains
.or scientific notation (e.g.1e3). Arithmetic that mixesintwithfloatusually promotesint → floatbefore the operator runs. - Floats are approximations of real numbers (
0.1 + 0.2famously does not equal0.3bit-for-bit). For financial work or equality checks usedecimal.Decimalor compare with a small tolerance, not==on raw floats.
Arithmetic operators
+-*behave as usual.**is exponentiation (not bitwise XOR like in some languages)./is always true division in Python 3: even6 / 2yields3.0(afloat).//is floor division: the quotient is rounded toward negative infinity. That matters for negatives:-7 // 3is-3, not truncation toward zero.%(modulo / remainder) ties to//:a == (a // b) * b + (a % b)holds forb ≠ 0. Forb > 0,a % blies in[0, b).- Unary
-applies to literals and sub-expressions (- (3)versus- 3—both fine with spacing).
Precedence and readability
Rough order (tighter binds first):
**— watch:2**3**2is2**(3**2)(power associates right).* / // %.+ -.- Comparisons (
< > ==etc.), then Boolean ops (not,and,or—you’ll reuse these in branching).
Use extra parentheses whenever a reader—or you next week—could pause. Readable code beats minimal parentheses.
Conversions you will use often
int(x)truncates toward zero whenxis a float—not the same behaviour as//on negatives from the float’s value.float(x)builds a floating value.round(x, ndigits)returns a number; bankers’ rounding (round half to even) can surprise testers—read thehelp(round)when you rely on ties.
Self-study checklist
- Sketch
/vs//vs%for one positive and one negative dividend with a positive divisor before running code. - Articulate one sentence: why
10 / 2isfloat. - Recall why blind
==between computed floats can mislead.
Examples
Worked snippets — calculator fluency
Division trio
Predict all three prints before executing:
n, d = 17, 5
print(n / d) # float “true divide”
print(n // d) # floor toward -∞ — here positives match intuition
print(n % d) # remainder pairing with //
Concrete check: (n // d) * d + (n % d) == n should hold (bool True).
Negative dividend (floor vs intuition)
Many people learn “truncate toward zero” from other contexts; Python’s // does floor:
print(-13 // 4)
print(-13 % 4)
Work the identity a == (a // b) * b + (a % b) on paper for b = 4.
Precedence vs parentheses
These two expressions differ—only one prints 77:
print(2 + 3 * 5**2)
print((2 + 3) * 5**2)
First version groups as 2 + (3 * (25)).
Floating promotion and pitfalls
x = (4 + 8) / 4
print(x, type(x))
Shows / floats the result even when numerator “looks integral” after +.
a, b = 0.1, 0.2
print(a + b)
print(round(a + b, 1))
Use round (or Decimal) where human decimals matter.
Augmented assignments (warm-up)
score = 10
score += 5 # score = score + 5
score *= 2 # score = score * 2
print(score)
These are shorthand for reassignment—they still follow normal evaluation rules.
Practice check — expressions and numeric types
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.A weather log stores `reading = -13` and `slots = 4`. What does `reading // slots` evaluate to?
2.What is the type of the value produced by `(4 + 8) / 4`?
3.For `reading = -13` and `slots = 4`, what is `reading % slots`?
4.Python evaluates `2 + 3 * 5 ** 2` to which value?
5.Why does `0.1 + 0.2 == 0.3` evaluate to `False`?