Variables, Assignment & Dynamic Typing

ভ্যারিয়েবল, অ্যাসাইনমেন্ট ও Dynamic Typing — Python-এ নামগুলো আসলে কী?

Read: ~25 min Beginner 5 practice problems Live code runner

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.

আপনি যদি C বা Java থেকে এসে থাকেন, তাহলে variable-কে হয়তো একটি বাক্স হিসেবে ভাবতেন — যেখানে মান সংরক্ষিত থাকে। কিন্তু Python-এ এই ধারণা বিভ্রান্তিকর। Python-এর variable আসলে একটি নামের ট্যাগ (name tag) — এটি মেমরির অন্য কোথাও অবস্থিত একটি object-কে reference করে। একই object-এর একাধিক নাম থাকতে পারে, এবং একটি নাম যেকোনো সময় পুরনো object থেকে ছিঁড়ে নতুন একটি object-এ attach করা যায়।

2. Assignment — = Binds a Name to an Object

When you write x = 10, Python does three things:

  • Creates an integer object 10 somewhere in memory.
  • Creates (or reuses) the name x in the current scope.
  • Binds — makes the name x point to — that object.
x = 10 লিখলে Python তিনটি কাজ করে — মেমরিতে একটি integer object (10) তৈরি করে, x নামটি (নতুন হলে) তৈরি করে, এবং x নামটিকে ঐ object-এর দিকে bind করে।
names.py
# 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}")
Python: Names point to objects a b int object value: 10 Figure 6.1 — দুটি নাম (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.

C-তে int x = 10; লিখলে type-টি variable-এর অংশ হয়ে যায়। কিন্তু Python-এ type object-এর সাথে থাকে, নামের সাথে নয়। একই নাম আজ integer, কাল string — যেকোনো কিছু reference করতে পারে। একে dynamic typing বলে।
dynamic.py
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-এর উৎস।
identity.py
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.

ImmutableMutable
int, float, boollist
strdict
tupleset
frozensetbytearray
bytesCustom classes (usually)
Immutable object একবার তৈরি হলে আর পরিবর্তন হয় না — প্রতিটি "পরিবর্তন" আসলে নতুন object তৈরি করে। Mutable object জায়গাতেই পরিবর্তন করা যায়।
mutability.py
# 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.

unpack.py
# 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)
Pythonic swap 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.
নাম লেখার নিয়ম: অক্ষর/সংখ্যা/underscore দিয়ে শুরু করা যায়, সংখ্যা দিয়ে নয়। convention অনুযায়ী variable ও function-এ snake_case, class-এ PascalCase, constant-এ UPPER_CASE। নিজের প্রজেক্টে এই নিয়ম মেনে চললে কোড পড়তে সহজ হয়।

8. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
Name / BindingA label attached to an object.একটি object-এ লাগানো লেবেল বা নাম।
ReferenceThe pointer-like link from a name to an object.নাম থেকে object-এর দিকে যাওয়া pointer-তুল্য লিংক।
Dynamic typingType is checked at runtime, not at declaration time.Type runtime-এ যাচাই করা হয়, declaration-এর সময়ে নয়।
ImmutableCannot be modified after creation.তৈরির পর পরিবর্তন করা যায় না।
MutableCan be modified in place.জায়গাতেই পরিবর্তন করা যায়।
id(x)Memory address of the object x points to.যে object-কে x reference করছে, তার মেমরি ঠিকানা।

9. Practice Problems

  1. Create three variables name, age, city and print them on one line using an f-string.
    তিনটি variable (name, age, city) তৈরি করে f-string দিয়ে এক লাইনে প্রিন্ট করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.py
    name = "Rafiq"
    age = 22
    city = "Dhaka"
    print(f"{name} is {age}, lives in {city}")
  2. Swap the values of a and b without using a temporary variable.
    কোনো temporary variable ব্যবহার না করে a ও b-এর মান swap করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans2.py
    a = 5
    b = 10
    print(f"Before: a={a}, b={b}")
    a, b = b, a
    print(f"After : a={a}, b={b}")
  3. Show the difference between is and == using two lists that contain the same numbers.
    একই সংখ্যাযুক্ত দুটি list তৈরি করে is ও == এর পার্থক্য দেখান।
    ✨ Show Answer (উত্তর দেখুন)
    ans3.py
    x = [1, 2, 3]
    y = [1, 2, 3]
    print(f"x == y: {x == y}")
    print(f"x is y: {x is y}")
  4. Explain in 2-3 sentences why the following code prints [1, 2, 99] for both a and b.
    নিচের কোডে a ও b দুটিতেই [1, 2, 99] কেন আসছে — ব্যাখ্যা করুন।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: The line b = a does not copy the list — it makes b point to the same list object that a points to. When a[2] = 99 mutates the list in place, the change is visible through both names. To get a true copy use b = a.copy() or b = list(a).

    b = a list-এর কপি তৈরি করে না — b একই list object-কে reference করে। a[2] = 99 list-কে জায়গাতেই পরিবর্তন করে, তাই দুই নামেই পরিবর্তন দেখা যায়। প্রকৃত copy-র জন্য b = a.copy() ব্যবহার করুন।

  5. 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.py
    nums = [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.

Python variable আসলে object-এর একটি নাম; object-এর সাথেই type যুক্ত থাকে। is বনাম ==, এবং mutable বনাম immutable — এই দুই পার্থক্য ভালোভাবে মনে রাখলে Python-এর অনেক সূক্ষ্ম বাগ এড়ানো যায়।

Next Module → Operators: Arithmetic, Comparison, Logical, Bitwise — Python-এ সব অপারেটর।