Values, Types & Variables
Everything Python touches is a value, and every value has a type. Most beginner bugs are type confusion wearing a disguise, so this page spends its time on what the types actually are and what happens when you mix them.
- Name the four core types and predict which one an expression produces.
- Explain what assignment actually does to a name.
- Read a
TypeErrorand say what caused it. - Convert between types deliberately rather than by accident.
A computer stores bits. Types are the agreement about what a particular
pile of bits means — the same bytes could be a number or a letter.
Python tracks that agreement for you, which is why it can catch
"3" + 4 and stop.
The 1999 Mars Climate Orbiter was lost because one team worked in pounds and another in newtons. No type system caught it. Modern code adds type annotations for exactly that reason, and this course grades you on writing them.
Python was released by Guido van Rossum in 1991 and named after Monty
Python. Its "everything is an object" design means even an integer has
methods, which is why (5).bit_length() works and surprises
people.
The four core types
CSC108H1 starts with four:
int — whole numbers, unbounded in
Python: 3, -17, 10**100.float — approximate real numbers:
3.0, -0.5, 2e10. Approximate is not a
typo; Unit 10 is about the consequences.str — text: 'hello',
"a", ''. Quotes are part of the notation, not the
value.bool — True or
False, capitalised.Ask Python directly with type(x).
type(7) # int type(7.0) # float type(7 + 1) # int type(7 / 1) # float <- division always gives float type(7 // 2) # int <- floor division keeps int type(7 == 7) # bool
That fourth line catches people every year. In Python 3,
/ is true division and always produces a
float, even when the answer is whole.
7 / 2 is 3.5; 7 // 2 is
3; 7 % 2 is 1. Floor division rounds
down, not toward zero, so -7 // 2 is
-4, not -3.
Variables are names, not boxes
Assignment x = 5 binds the name x to
the value 5. It does not create a box called
x that holds a number. Rebinding just points the name
elsewhere.
x = 5 y = x # y now names the same value x = 10 # x is rebound; y is untouched print(y) # 5
For immutable values like int this feels obvious. In Unit 5
the same rule applied to lists produces the aliasing trap, and it will not
feel obvious at all.
Letters, digits and underscores; not starting with a digit; case
sensitive, so Total and total differ. Avoid
shadowing built-ins — naming a variable list or
str breaks the function of that name for the rest of the
program, and the resulting error is baffling.
Mixing types
int and float mix freely; the
result is a float.str + str concatenates;
str * int repeats.str + int is a TypeError. Python
refuses to guess whether you meant text or arithmetic.bool is a subtype of int, so
True + True is 2. Useful for counting, confusing
when accidental.age = input('Age: ') # input ALWAYS returns str
print(age + 1)TypeError: can only concatenate str (not "int") to str
age is a
str because input always returns one, even when the
user types digits.age = int(input('Age: ')).
Convert at the point of entry, not scattered through the
program.int('42') # 42
int('42.7') # ValueError - int() will not parse a decimal string
int(42.7) # 42 - truncates toward zero, does not round
float('42') # 42.0
str(42) # '42'Note the second and third lines differ. int() of a
string demands a clean integer; int() of a
float truncates.
• Expecting / to give an integer. It never does in
Python 3; use //.
• Forgetting input returns a string. Every numeric input
needs an explicit conversion.
• Writing true or TRUE. Python wants
True.
• Using = where == is meant. Assignment is not
comparison, and inside an if it is a syntax error rather than a
silent bug — which is a mercy.
Practice
Predict each answer before running it. Original exercises written for this guide in the style of the course's weekly sets.
What are the types of 3 * 2, 3 * 2.0,
'3' * 2?
Answer
int (6),
float (6.0), str ('33'). The last repeats the
string rather than multiplying.What is -7 // 2?
Answer
-4. Floor
division rounds down toward negative infinity, not toward zero. Check with
-7 % 2, which is 1, since
-4 * 2 + 1 = -7.What prints?
a = 2 b = a a = a + 3 print(a, b)
Answer
5 2.
b was bound to the value 2 and never rebound;
changing a does not reach it.Repair this so it prints the total.
price = input('Price: ')
qty = input('Quantity: ')
print(price * qty)Solution
price * qty is a TypeError. Convert at entry:
price = float(input('Price: '))
qty = int(input('Quantity: '))
print(price * qty)What is True + True + False, and where is that
useful?
Answer
2. It is
useful for counting how many conditions hold:
sum(x > 0 for x in nums) counts positives without an
explicit loop.Why does int('3.9') fail while int(3.9)
works?
Answer
int()
parsing a string requires the whole string to be a valid integer literal,
and '3.9' is not. Applied to a float it truncates instead.
Route through int(float('3.9')) if you need
3.Why is str = 'hello' a bad idea?
Answer
str, so any later str(42) raises
TypeError: 'str' object is not callable — an error that
points nowhere near the real cause.Swap two variables without a temporary.
Solution
a, b = b, a
Expanded topic checklist
Course coverage. This unit is part of the complete CSC108H1 topic map. It should cover the following items before you move on:
- Elementary data types. int, float, str, bool, and
type()to check. - Operators. Arithmetic including
//and%, and what each returns. - Assignment. Names bound to values, not boxes holding them.
- Conversion. Deliberate use of
int(),float(),str(), and where each fails.
The calendar lists "elementary data types" first, and every offering starts here. The department does not publish a fixed textbook for every term; check the syllabus on Quercus in week one for readings and the exact Python version.
Values have types; types decide which operations are legal; names refer to values. Every later unit is that trio applied to a richer kind of value.
Deep practice extensions
Use these after the main practice set. They push on definitions and edge cases rather than routine calculation.
Which operator gives an int result for
7 and 2?
Guide
//, floor
division. / always returns a float in Python
3.What type does input() return?
Guide
str, regardless of what the user typed. Convert
explicitly.What does x = y actually do?
Guide
x to whatever value y currently refers to. No
copying happens, which matters enormously once values are
mutable.What does a TypeError tell you?