Variables, Data Types & Mutability
What is a variable?
A variable is a name that refers to a value stored in memory. Think of it as a label stuck on a box. We create a variable by assigning a value with the = operator — no declaration is needed.
name = 'Ananya' # str
age = 16 # int
height = 5.4 # float
is_monitor = True # bool
print(name, age, height, is_monitor)
age = age + 1 # value of a variable can change
print('Next year', name, 'will be', age)In Python = means "assign the value on the right to the name on the left". So age = age + 1 is perfectly valid: take the old age, add 1, store it back. To compare two values we use ==.
Assignment, multiple assignment and swap
a, b, c = 5, 'six', 7.0 # many values at once print(a, b, c) p = q = r = 0 # same value to many print(p, q, r) x, y = 3, 9 x, y = y, x # swap in one line print(x, y)
Swapping with x, y = y, x needs no third variable — a favourite viva question.
Everything is an object: id() and type()
In Python every value — a number, a string, a list, even a function — is an object. Every object has an identity (id()), a type (type()) and a value.
x = 10 y = x print(id(x) == id(y)) # both names point to the same object x = x + 5 # a NEW object 15 is created for x print(x, y, id(x) == id(y))
After y = x
- x → 10
- y → 10 (same object)
- id(x) == id(y) is True
After x = x + 5
- x → 15 (new object)
- y → 10 (unchanged)
- id(x) == id(y) is False
Data types at a glance
Python data 📁 types/ ├── 📁 Numbers/ │ ├── int 25, -7, 10**20 │ ├── float 3.14, -0.5, 2.0e3 │ ├── complex 3+4j │ └── bool True, False ├── 📁 Sequences/ │ ├── str 'Hello', "नमस्ते" │ ├── list [10, 'a', 2.5] │ └── tuple (10, 20, 30) ├── 📁 Set/ │ └── set {1, 2, 3} ├── 📁 Mapping/ │ └── dict {'roll': 1, 'name': 'Riya'} └── None None (no value)
print(type(25), type(3.5), type(2 + 3j))
print(type(True), type('Hi'), type(None))
print(type([1, 2]), type((1, 2)), type({1, 2}), type({'a': 1}))Numbers: int, float, complex, bool
| Type | Stores | Examples |
|---|---|---|
int | Whole numbers of any size | 0, 45, -300, 2**100 |
float | Numbers with a decimal point | 3.14, -0.5, 1.5e3 (=1500.0) |
complex | Real + imaginary part (j) | 3+4j |
bool | True or False (a kind of int: True = 1) | marks >= 33 |
print(7 / 2, 7 // 2) # float division, integer division
print(2 ** 100) # int has no size limit
print(0.1 + 0.2) # floats are approximate
z = 3 + 4j
print(z.real, z.imag, abs(z))
print(True + True, int(False))
print(bool(0), bool(''), bool('0'), bool([]))Note: 0.1 + 0.2 is not exactly 0.3 because floats are stored in binary approximately. Use round(x, 2) to display money values. Empty values (0, '', []) are False; almost everything else is True — even the string '0'.
Sequences, sets, mappings and None
- str — an ordered sequence of characters in quotes:
'Python'. - list — an ordered, changeable collection in square brackets:
[90, 85, 77]. - tuple — an ordered, unchangeable collection in round brackets:
(28.6, 77.2)for a location. - set — an unordered collection of unique items:
{'red', 'green'}. - dict — key: value pairs:
{'name': 'Riya', 'class': 11}. - None — a special value meaning "nothing / no value yet".
Mutable and immutable types
Immutable (cannot change in place)
- int, float, complex, bool
- str
- tuple
- Changing means creating a new object
Mutable (can change in place)
- list
- dict
- set
- Same object is modified; all names see the change
L = [1, 2, 3] M = L # M is another name for the SAME list M.append(4) # list changes in place print(L, M, L is M)
s = 'Hello' s[0] = 'J'
The list example shows that M = L does not copy the list; both names share one list. You will learn proper copying in the list lesson.
Summary & practice
- Variable = name referring to an object; created by assignment.
- Every object has id, type and value.
- Types: int, float, complex, bool, str, list, tuple, set, dict, None.
- Mutable: list, dict, set. Immutable: numbers, str, tuple.
Predict the output: a = [5]; b = a; b.append(6); print(a) and s = 'ab'; t = s; t = t + 'c'; print(s).
Answers: [5, 6] (list is mutable and shared) and ab (strings are immutable; t got a new object).
