Skip to main content
Module 1 · Lesson 1 of 41/36 in the course~14 min
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!

Python
greeting = "Hello, Python!"
age = 30
pi_value = 3.14
is_active = True
empty_slot = None

The 5 built-in scalar types compared

TypeLiteral exampleMutabilitySpecial truthy / falsey valueTypical use
int42, -7, 1_000_000Immutable0 is falseCounters, IDs, indexes
float3.14, -0.5, 2e10Immutable0.0 is falsePrices, averages, measurements
str"ciao", 'Ada'Immutable"" is falseNames, texts, JSON
boolTrue, FalseImmutableSubclass of int (1 / 0)Flags and conditions
NoneTypeNoneImmutable (singleton)Always falseIntentional missing value
Python
# 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 int

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

Python
# 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
Python
# 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

Python Quiz#python.m1.l1.e4
Ready

What does the last line print after this sequence of assignments?

Python
a = 10
b = a
a = 25
print(a, b)
Answer options
Python Quiz#python.m1.l1.e5
Ready

Which expression raises TypeError due to Python's strong typing?

Answer options

Test yourself

Warm-Up Stage

Step 1 — First string assignment

Bind a name in snake_case to a string literal and return it as the last expression.

Exercise#python.m1.l1.e1
Attempts: 0Loading…

Create a variable greeting with the value 'Hello, Python!' and then evaluate greeting in the last row.

Loading editor…
Show hint

In Python you don't need keywords like let or var: just write greeting = "Hello, Python!".

Solution available after 3 attempts

Practice Stage

Step 2 — Type Inspection with type()

Use type(...) to capture the class object of an integer.

Exercise#python.m1.l1.e2
Attempts: 0Loading…

Assign age_type the type of the integer 30 using the built-in function type(...), then evaluate age_type.

Loading editor…
Show hint

Call type(30) and assign the return value to the age_type variable.

Solution available after 3 attempts

Final Challenge

Step 3 — Float, rebinding and final verification

Combine float declaration and type inspection in one clean block.

Exercise#python.m1.l1.e3
Attempts: 0Loading…

Define height = 1.75, assign height_type the result of type(height), and evaluate height_type.

Loading editor…
Show hint

Use height_type = type(height) and then write height_type on the last line.

Solution available after 3 attempts