Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Unit 2 – Python Basics  ·  Lesson 12

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.

Python
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)
Ananya 16 5.4 True Next year Ananya will be 17
namevariable (label)refers to'Ananya'str object in memory
A variable name points to an object
⚠️ = is not "equals"

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

Python
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)
5 six 7.0 0 0 0 9 3

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.

Python
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))
True 15 10 False

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
Integers are immutable: changing x makes a new object

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)
The family tree of Python data types
Python
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}))
<class 'int'> <class 'float'> <class 'complex'> <class 'bool'> <class 'str'> <class 'NoneType'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'>

Numbers: int, float, complex, bool

TypeStoresExamples
intWhole numbers of any size0, 45, -300, 2**100
floatNumbers with a decimal point3.14, -0.5, 1.5e3 (=1500.0)
complexReal + imaginary part (j)3+4j
boolTrue or False (a kind of int: True = 1)marks >= 33
Python
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([]))
3.5 3 1267650600228229401496703205376 0.30000000000000004 3.0 4.0 5.0 2 0 False False True False

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".
marks →900851772643
A list keeps values in order with positions 0, 1, 2 …

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
Very important for exams
Python
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)
[1, 2, 3, 4] [1, 2, 3, 4] True
Python
s = 'Hello'
s[0] = 'J'
TypeError: 'str' object does not support item assignment

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.
✅ Practice

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

Frequently Asked Questions

Do we need to declare the type of a variable in Python?
No. Python is dynamically typed: the type is decided by the value you assign. The same name can later refer to a value of another type.
Which data types are mutable in Python?
List, dictionary and set are mutable — they can be changed in place. int, float, complex, bool, str and tuple are immutable.
What does id() return?
A unique number for an object during its lifetime (in CPython, its memory address). Two names have the same id when they refer to the same object.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicOperators, Expressions & Precedence Your progress: 12 / 29 (41%) · Lesson 13 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free