Variables, Assignment & Dynamic Typing
ভ্যারিয়েবল, অ্যাসাইনমেন্ট ও Dynamic Typing — Python-এ নামগুলো আসলে কী?
1. Variables Are Not Boxes — They Are Name Tags
If you are coming from C or Java, you may think of a variable as a box that stores a value. In Python, this mental model is misleading. A Python variable is actually a name tag — it's a label that refers to an object living elsewhere in memory. The same object can have many names, and a name can be detached and re-attached to a new object at any time.
2. Assignment — = Binds a Name to an Object
When you write x = 10, Python does three things:
- Creates an integer object
10somewhere in memory. - Creates (or reuses) the name
xin the current scope. - Binds — makes the name
xpoint to — that object.
x = 10 লিখলে Python তিনটি কাজ করে — মেমরিতে একটি integer object (10) তৈরি করে, x নামটি (নতুন হলে) তৈরি করে, এবং x নামটিকে ঐ object-এর দিকে bind করে।
# Create the name `a` bound to the integer 10
a = 10
print(f"a = {a}, id = {id(a)}")
# Bind another name `b` to the SAME object
b = a
print(f"b = {b}, id = {id(b)}")
print(f"a is b? {a is b}")
# Now rebind `a` to a new object — `b` is unchanged
a = 20
print(f"After a = 20 → a={a}, b={b}")
a, b) একই object-কে reference করছে।
3. Dynamic Typing — Types Belong to Objects, Not Names
In C you write int x = 10; — the type belongs to the variable x. Once declared, x
can only hold integers. In Python, types belong to objects, not names. The name x can point to an
integer today and a string tomorrow. This is called dynamic typing.
int x = 10; লিখলে type-টি variable-এর অংশ হয়ে যায়। কিন্তু Python-এ type object-এর সাথে থাকে, নামের সাথে নয়। একই নাম আজ integer, কাল string — যেকোনো কিছু reference করতে পারে। একে dynamic typing বলে।
x = 42
print(type(x))
x = "Python is easy"
print(type(x))
x = [1, 2, 3]
print(type(x))
# Python checks types at runtime, not at "compile" time
x = 7
# x = x + "hello" # <-- TypeError at runtime
4. is vs == — Identity vs Equality
== asks: "Do these two objects have the same value?"
is asks: "Are these two names pointing to the same object in memory?"
For small integers and short strings, Python caches objects — so is and == may
both return True. But for lists and large objects, they are different.
== জিজ্ঞাসা করে: "মান (value) কি এক?" — আর is জিজ্ঞাসা করে: "মেমরিতে কি একই object?" — এই পার্থক্য না বোঝা Python-এর একটি ক্লাসিক bug-এর উৎস।
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(f"a == b: {a == b}") # True — same values
print(f"a is b: {a is b}") # False — different objects
print(f"a is c: {a is c}") # True — same object
# Rule of thumb: use `is` only to compare with None, True, False
x = None
if x is None:
print("x has no value yet")
5. Mutable vs Immutable Objects
An immutable object cannot be changed after creation — any "change" actually creates a new object. A mutable object can be modified in place.
| Immutable | Mutable |
|---|---|
| int, float, bool | list |
| str | dict |
| tuple | set |
| frozenset | bytearray |
| bytes | Custom classes (usually) |
# str is immutable
s = "hello"
print(id(s))
s = s + " world"
print(id(s)) # different id — new object
# list is mutable
lst = [1, 2, 3]
print(id(lst))
lst.append(4)
print(id(lst)) # same id — modified in place
print(lst) # [1, 2, 3, 4]
6. Multiple Assignment & Unpacking
Python offers elegant shortcuts for assigning multiple names at once.
# Parallel assignment
a, b, c = 1, 2, 3
print(a, b, c)
# Swap without a temp — very Pythonic
a, b = b, a
print(f"After swap: a={a}, b={b}")
# Unpack a list or tuple
point = (10, 20)
x, y = point
print(f"x={x}, y={y}")
# Starred unpacking (PEP 3132)
first, *rest = [1, 2, 3, 4, 5]
print(first, rest) # 1 [2, 3, 4, 5]
# Chained assignment
a = b = c = 0
print(a, b, c)
a, b = b, a — Python-এর সবচেয়ে সুন্দর idiom-গুলোর একটি। C-তে এই কাজ করতে একটি temporary variable লাগে।
7. Naming Rules & Conventions (PEP 8)
Python has strict syntax rules for names and strong social conventions about style.
- Syntax: letters, digits, underscores. Cannot start with a digit. Cannot be a Python keyword (
if,class,import, ...). - Convention: snake_case for variables and functions, PascalCase for classes, UPPER_CASE for constants.
- Leading underscore
_private: hint that the name is internal. - Dunder
__init__: Python-reserved, used for special methods.
8. Vocabulary (শব্দভাণ্ডার)
| Term | Meaning | বাংলায় |
|---|---|---|
| Name / Binding | A label attached to an object. | একটি object-এ লাগানো লেবেল বা নাম। |
| Reference | The pointer-like link from a name to an object. | নাম থেকে object-এর দিকে যাওয়া pointer-তুল্য লিংক। |
| Dynamic typing | Type is checked at runtime, not at declaration time. | Type runtime-এ যাচাই করা হয়, declaration-এর সময়ে নয়। |
| Immutable | Cannot be modified after creation. | তৈরির পর পরিবর্তন করা যায় না। |
| Mutable | Can be modified in place. | জায়গাতেই পরিবর্তন করা যায়। |
id(x) | Memory address of the object x points to. | যে object-কে x reference করছে, তার মেমরি ঠিকানা। |
9. Practice Problems
-
Create three variables
name,age,cityand print them on one line using an f-string.তিনটি variable (name,age,city) তৈরি করে f-string দিয়ে এক লাইনে প্রিন্ট করুন।✨ Show Answer (উত্তর দেখুন)
ans1.pyname = "Rafiq" age = 22 city = "Dhaka" print(f"{name} is {age}, lives in {city}") -
Swap the values of
aandbwithout using a temporary variable.কোনো temporary variable ব্যবহার না করেaওb-এর মান swap করুন।✨ Show Answer (উত্তর দেখুন)
ans2.pya = 5 b = 10 print(f"Before: a={a}, b={b}") a, b = b, a print(f"After : a={a}, b={b}") -
Show the difference between
isand==using two lists that contain the same numbers.একই সংখ্যাযুক্ত দুটি list তৈরি করেisও==এর পার্থক্য দেখান।✨ Show Answer (উত্তর দেখুন)
ans3.pyx = [1, 2, 3] y = [1, 2, 3] print(f"x == y: {x == y}") print(f"x is y: {x is y}") -
Explain in 2-3 sentences why the following code prints
[1, 2, 99]for bothaandb.নিচের কোডেaওbদুটিতেই[1, 2, 99]কেন আসছে — ব্যাখ্যা করুন।✨ Show Answer (উত্তর দেখুন)
Answer: The line
b = adoes not copy the list — it makesbpoint to the same list object thatapoints to. Whena[2] = 99mutates the list in place, the change is visible through both names. To get a true copy useb = a.copy()orb = list(a).b = alist-এর কপি তৈরি করে না —bএকই list object-কে reference করে।a[2] = 99list-কে জায়গাতেই পরিবর্তন করে, তাই দুই নামেই পরিবর্তন দেখা যায়। প্রকৃত copy-র জন্যb = a.copy()ব্যবহার করুন। -
Use starred unpacking to assign the first, last, and middle elements of a list of five integers.পাঁচটি সংখ্যার list থেকে starred unpacking ব্যবহার করে first, last, এবং middle-গুলো আলাদা করুন।
✨ Show Answer (উত্তর দেখুন)
ans5.pynums = [10, 20, 30, 40, 50] first, *middle, last = nums print(f"first={first}") print(f"middle={middle}") print(f"last={last}")
Summary — Module 06
A Python variable is a name bound to an object, not a box that holds a value. Assignment binds (or rebinds) names;
objects can have many names. Types belong to objects (not names), which is why Python is dynamically typed. Mastering the
distinction between is (identity) and == (equality), and between mutable and immutable types, will
save you from a thousand subtle bugs.
is বনাম ==, এবং mutable বনাম immutable — এই দুই পার্থক্য ভালোভাবে মনে রাখলে Python-এর অনেক সূক্ষ্ম বাগ এড়ানো যায়।