CSC108H1/Unit 1
Unit 1 · Intro to Programming

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.

By the end you can
  • Name the four core types and predict which one an expression produces.
  • Explain what assignment actually does to a name.
  • Read a TypeError and say what caused it.
  • Convert between types deliberately rather than by accident.
Why it matters & where it comes from
Why it exists

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.

In the real world

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.

Story & open questions

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

Definition

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

Example Type follows the operation
python
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.

Integer division has two forms

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

Definition

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.

Example Trace the names
python
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.

Names have rules

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

Rule What combines with what
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.
Example Read the error
python
age = input('Age: ')   # input ALWAYS returns str
print(age + 1)
traceback
TypeError: can only concatenate str (not "int") to str
The message names the types. age is a str because input always returns one, even when the user types digits.
Fix by converting: age = int(input('Age: ')). Convert at the point of entry, not scattered through the program.
Example Conversions that fail
python
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.

Common pitfalls

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

Practice 1 · Predict the type

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.
Practice 2 · Floor division

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.
Practice 3 · Trace

What prints?

python
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.
Practice 4 · Fix the error

Repair this so it prints the total.

python
price = input('Price: ')
qty = input('Quantity: ')
print(price * qty)
Solution
Both are strings, so price * qty is a TypeError. Convert at entry:
python
price = float(input('Price: '))
qty = int(input('Quantity: '))
print(price * qty)
Practice 5 · Booleans are ints

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.
Practice 6 · Conversion order

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.
Practice 7 · Naming

Why is str = 'hello' a bad idea?

Answer
It rebinds the built-in str, so any later str(42) raises TypeError: 'str' object is not callable — an error that points nowhere near the real cause.
Practice 8 · Swap

Swap two variables without a temporary.

Solution
python
a, b = b, a
The right side is evaluated fully first, into a tuple, then unpacked into the names on the left. This is why no temporary is needed.

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.
Textbook/source alignment

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.

Core idea map

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.

Study routine. Keep a Python shell open beside the notes. Every claim on this page takes under ten seconds to verify, and verifying is how it sticks.

Deep practice extensions

Use these after the main practice set. They push on definitions and edge cases rather than routine calculation.

Extension 1 · Division

Which operator gives an int result for 7 and 2?

Guide
//, floor division. / always returns a float in Python 3.
Extension 2 · input

What type does input() return?

Guide
Always str, regardless of what the user typed. Convert explicitly.
Extension 3 · Assignment semantics

What does x = y actually do?

Guide
Binds the name x to whatever value y currently refers to. No copying happens, which matters enormously once values are mutable.
Extension 4 · Reading a TypeError

What does a TypeError tell you?

Guide
That an operation was applied to types it does not accept. The message names both the operation and the offending types, so it usually points straight at a missing conversion.