Variables, literals and types
A literal is a value written directly in code: 42, 3.14, "hello", True. A variable is a name bound to a value. Python figures out the type from the literal — you never declare it — and type() tells you what it decided.
You should see
42 is a int
19.99 is a float
'Ada' is a str
False is a bool
None is a NoneTypebig = 10 ** 100 and print its type. Python ints have no maximum size.| Type | Literal examples | Notes |
|---|---|---|
int | 0 -7 1_000_000 0xFF | Unlimited size. Underscores are allowed for readability. |
float | 3.14 2.0 1e-9 | 64-bit; 0.1 + 0.2 != 0.3. Use decimal for money. |
str | "hi" 'hi' """multi
line""" | Immutable Unicode text. Single and double quotes are identical. |
bool | True False | Capitalised. A subclass of int: True + True == 2. |
None | None | The one "no value" object. Functions that return nothing return this. |
A name can be rebound to a value of a different type at any time — Python is dynamically typed. That is convenient and also the source of half the TypeErrors you will see, which is why type hints (Module 09) exist.
You should see
10 int
ten str
2 1NameError: name 'totl' is not defined
total = 5 + 7
print(totl)Traceback (most recent call last):
File "your code", line 2, in <module>
print(totl)
NameError: name 'totl' is not defined. Did you mean: 'total'?The interpreter looked up the name totl and no such name has ever been bound. Ninety percent of NameErrors are a typo, a variable used before the line that creates it, or a name created inside a function and used outside it (scope — Module 04). Python 3.10+ even suggests the closest match.
Read the name in the error, then find where you meant to create it. Here it is a typo.
total = 5 + 7
print(total)