Module lessons (1/4)
Variables and types
In Python a variable is not a fixed "box" in memory like in C: it is a label that points to a live object on the heap. Understanding this mental model eliminates 90% of the confusion about types, assignments and copies from day zero.
The mental model: Stack -> Heap
When you write user_name = "Ada", Python first creates the string object "Ada" on the heap (with its own type and unique ID) and then binds the name user_name to that address. If you reassign user_name = "Grace", you don't "overwrite" the old string: you move the label to a new object!
greeting = "Hello, Python!"
age = 30
pi_value = 3.14
is_active = True
empty_slot = NoneThe 5 built-in scalar types compared
| Type | Literal example | Mutability | Special truthy / falsey value | Typical use |
|---|---|---|---|---|
int | 42, -7, 1_000_000 | Immutable | 0 is false | Counters, IDs, indexes |
float | 3.14, -0.5, 2e10 | Immutable | 0.0 is false | Prices, averages, measurements |
str | "ciao", 'Ada' | Immutable | "" is false | Names, texts, JSON |
bool | True, False | Immutable | Subclass of int (1 / 0) | Flags and conditions |
NoneType | None | Immutable (singleton) | Always false | Intentional missing value |
# Esempio 1: ispezionare il tipo reale dell'oggetto a runtime
sample_id = 1_000_000
sample_ratio = 0.75
print(type(sample_id)) # <class 'int'>
print(type(sample_ratio)) # <class 'float'>
print(isinstance(True, int)) # True: bool eredita da intDynamic but strong typing
Python is dynamically typed (the same label can point first to a int and then to a str), but it is also strongly typed: it never silently converts incompatible types behind the scenes.
# Esempio 2: rebinding dinamico delle etichette
counter = 10 # punta a int(10)
counter = "ten" # ora la stessa etichetta punta a str("ten")
# Esempio 3: tipizzazione forte in azione (vs JavaScript)
# "Years: " + 30 -> TypeError: can only concatenate str (not "int") to str
label = "Years: " + str(30) # conversione esplicita sicura# Esempio 4: aliasing con tipi immutabili e verifica con `is`
first_value = 500
second_value = first_value
first_value = first_value + 1 # crea un nuovo int(501); second_value resta 500
marker = None
is_missing = marker is None # True: per None usa sempre `is` / `is not`Quick check
What does the last line print after this sequence of assignments?
a = 10
b = a
a = 25
print(a, b)Which expression raises TypeError due to Python's strong typing?
Test yourself
Step 1 — First string assignment
Bind a name in snake_case to a string literal and return it as the last expression.
Create a variable greeting with the value 'Hello, Python!' and then evaluate greeting in the last row.
Show hint
In Python you don't need keywords like let or var: just write greeting = "Hello, Python!".
Solution available after 3 attempts
Step 2 — Type Inspection with type()
Use type(...) to capture the class object of an integer.
Assign age_type the type of the integer 30 using the built-in function type(...), then evaluate age_type.
Show hint
Call type(30) and assign the return value to the age_type variable.
Solution available after 3 attempts
Step 3 — Float, rebinding and final verification
Combine float declaration and type inspection in one clean block.
Define height = 1.75, assign height_type the result of type(height), and evaluate height_type.
Show hint
Use height_type = type(height) and then write height_type on the last line.
Solution available after 3 attempts