Values and Types
Every value a program works with has a type, and that type determines which operations are valid.
By the end of this lesson, you can
- Identify the type of a given value, such as an integer, float, string, or boolean
- Predict whether an operation between two values will succeed or raise a TypeError
- Explain why Python checks types while the program runs, rather than before it starts
Why it matters
Every piece of information a program handles — a price, a name, a yes-or-no answer — is stored as a value, and every value has a type. The type is what tells the program (and you) which operations make sense: you can add two numbers, but “adding” two names doesn’t mean anything. Most beginner bugs that look mysterious — “why did my program crash?” — turn out to be a type mismatch the program refused to guess its way through.
Mental model
Think of a type as a category that comes with its own rulebook of valid operations:
| Example value | Type | Some valid operations |
|---|---|---|
12 |
integer | add, subtract, multiply, compare |
12.5 |
float | add, subtract, multiply, compare |
"coffee" |
string | join with another string, measure length |
True |
boolean | combine with and / or / not |
A value’s type doesn’t change once created. 12 is always an integer;
"12" is always a string — even though a human reading them might treat
them the same way, Python never does.
Small example
price = 12.5
name = "coffee"
in_stock = True
print(type(price))
print(type(name))
print(type(in_stock))<class 'float'>
<class 'str'>
<class 'bool'>Python’s built-in type() function reports a value’s type back to you —
useful whenever you’re not sure what you’re working with.
Trace it
Whether an expression is valid depends entirely on the types on each side of the operator. Work through these four before reading the result column:
| Expression | Types involved | Result |
|---|---|---|
| 1 + 2 | int + int | 3 |
| 1 + 2.5 | int + float | 3.5 |
| "1" + "2" | str + str | "12" (joined, not added) |
| "1" + 2 | str + int | TypeError |
The last row is the one that surprises people: + isn’t a single
universal operation. Python defines it separately for numbers (add) and
strings (join), and refuses to guess when the two sides don’t agree.
Check your understanding
Or reveal the answer without checking
Answer:An error (TypeError)
"3" is a string and 4 is an integer. + is not defined between a string and an integer, so Python raises a TypeError instead of guessing what you meant.
Or reveal the answer without checking
Answer:"5" + "5"
"5" + "5" joins two strings, producing "55". The other three each mix incompatible types: string+int, int+list, and string+int again.
Practice: warm-up
For each value below, write down what you think its type is before
checking: 42, 3.14, "hello", True, "42".
Stuck? Reveal one hint at a time.
Hint 1
Quotation marks around a value always make it a string, no matter what characters are inside them.
Hint 2
A number written with a decimal point is a float, even if the digits after the point are zero.
Hint 3
True and False (capitalized, no quotes) are their own type: boolean.
Reveal the types
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
42 -> int
3.14 -> float
"hello" -> str
True -> bool
"42" -> str (the quotes make it text, not a number)Practice: apply it
A checkout screen calculates a total like this:
quantity = 3
price = 4.5
total = quantity * price
print("Total: $" + total)
Or reveal the answer without checking
Answer:No, it raises a TypeError
total is a float (13.5), and + between a string and a float is undefined — just like string + int. Fixing it means converting total to a string first, with str(total).
Modification challenge: rewrite the last line so it prints correctly,
using str(total) to convert the number to a string before joining it.
Summary
- Every value has exactly one type, whether or not you write that type down.
- A value’s type determines which operations on it are valid.
- Mixing incompatible types doesn’t make Python guess — it raises a
TypeError. type()lets you check a value’s type directly when you’re unsure.- These rules hold regardless of language; Python just checks them while your program runs rather than before it starts.