Immutability in Python TRB CSI › Python Programming › immutability

Immutability in Python

Immutability என்பது ஒரு object உருவாக்கப்பட்ட பிறகு அதன் original value-ஐ அதே object-ல் மாற்ற முடியாத தன்மை. Simple definition: An immutable object cannot be changed after it is created. Tamil mixed explanation: ஒரு value memory-ல் object …

Premium — locked Intermediate 22 min 212 cards 48 programs 8 MCQs v4
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

IMMUTABILITY IN PYTHON Immutability என்பது Python-ல் மிகவும் முக்கியமான concept. குறிப……

Premium This card is part of the premium lesson. Unlock
Definition

Immutability என்பது ஒரு object உருவாக்கப்பட்ட பிறகு அதன் original value-ஐ அதே object-ல் மாற்ற முடியாத தன்மை. ……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

name = "Python" இங்கே "Python" ஒரு String object. அ……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

இந்த இரண்டு words-ஐ முதலில் புரிந்துகொள்ள வேண்டும். Mutab……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

List is mutable. a = [10, 20,……

Premium This card is part of the premium lesson. Unlock

Original list change ஆனது. ஆனால் String:……

Premium This card is part of the premium lesson. Unlock
Concept
Core concept

Immutability என்பதை புரிந்துகொள்ள ஒரு மு……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இதைப் பார்த்து: Integer mutable என்று நினைக்கக்கூடாது. இங்கே 10 object change ஆகவில்லை. x என்ற variable வேறு ……

Premium This card is part of the premium lesson. Unlock
Explanation
Object and variable

Python-ல் variable என்பது value வைத்திருக்கும் traditio……

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
இதில்
Concept

x -> 100 இப்போது: x = 200 என்றால்: x -> 200……

Premium This card is part of the premium lesson. Unlock
Flow

Immutability flow: Object Created | v Value Stored | v Modific……

Premium This card is part of the premium lesson. Unlock
Explanation
Immutable data types in python

Python-ல் commonly immutable data types: int float complex bool……

Premium This card is part of the premium lesson. Unlock
Explanation
Mutable data types

Common mutable types: list dict s……

Premium This card is part of the premium lesson. Unlock

List mutable என்பதால் origina……

Premium This card is part of the premium lesson. Unlock
Explanation
String immutability

Python String immutable. text……

Premium This card is part of the premium lesson. Unlock
Explanation
Why string cannot be modified

String: text = "Python" Characters: P y t h o n 0 1 2 3 4 5 நாம்: text[0] = "J" என்று எழுதினால் Pyth……

Premium This card is part of the premium lesson. Unlock
Explanation
How to change an immutable string

String-ஐ direct modify செய்ய முடியாது. ஆனா……

Premium This card is part of the premium lesson. Unlock

இங்கே original "Python" objec……

Premium This card is part of the premium lesson. Unlock
Explanation

text = "Python" text "Python" string object-ஐ reference செய்கிறது. text[1:] Out……

Premium This card is part of the premium lesson. Unlock
Important
Important difference

இந்த இரண்டு statements ஒன்றல்ல. text[0] = "J" மற்றும்: text = "Java" First: Exis……

Premium This card is part of the premium lesson. Unlock
Explanation
Program 1

name = "Python"…

Premium This card is part of the premium lesson. Unlock
Explanation

name = "Java"…

Premium This card is part of the premium lesson. Unlock

இதைப் பார்த்து String mutable என்று நினைக்கக்கூடாது. இங்கே……

Premium This card is part of the premium lesson. Unlock
Explanation
Memory view

Conceptually: Before: name | v "Python" After: ……

Premium This card is part of the premium lesson. Unlock
Explanation
Integer immutability

Integer immutable.…

Premium This card is part of the premium lesson. Unlock
Program
Example
Program
இங்கே

10 object-ஐ 11 ஆக modify செய்யவில்லை. Calculation: 10 + ……

Premium This card is part of the premium lesson. Unlock
Explanation
Using id function

Python-ல் object identity பார……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Conceptually two different object identitie……

Premium This card is part of the premium lesson. Unlock
Important

Exact id() values system/run-……

Premium This card is part of the premium lesson. Unlock
Real Life Example
String id example

text = "Python" print(id(text)) text = text + " Programming" print(id(t……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

a = "Hello"…

Premium This card is part of the premium lesson. Unlock
Question

Original String modified ஆகிறதா? Answer: No. New……

Premium This card is part of the premium lesson. Unlock
Explanation
String methods and immutability

String methods கூட original Str……

Premium This card is part of the premium lesson. Unlock

ஏன்? upper() new String return செய்கிறது.……

Premium This card is part of the premium lesson. Unlock
Dry Run
text = "python" text.upper()
Explanation
Example

text = "Python" text.replace(……

Premium This card is part of the premium lesson. Unlock

replace() original String-ஐ m……

Premium This card is part of the premium lesson. Unlock
Explanation
String methods return new objects

String immutable என்பதால் இந்த methods பொதுவாக புதிய String return ……

Premium This card is part of the premium lesson. Unlock
Explanation
Tuple immutability

Tuple immutable. numbers = (1……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError: 'tuple' object does not support it……

Premium This card is part of the premium lesson. Unlock
Comparison
Tuple vs list

List: numbers = [10, 20, 30] numbers[0] = 100 Allowed. Tuple:……

Premium This card is part of the premium lesson. Unlock
Concept
Important tuple concept

Tuple immutable என்றாலும் tup……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

data = ([10, 20], 30) இங்கே outer object: Tuple Im……

Premium This card is part of the premium lesson. Unlock

இது மிகவும் முக்கியமான advanc……

Premium This card is part of the premium lesson. Unlock
Explanation
Why above tuple changed

Actually tuple structure change ஆகவில்லை. Tuple still contains: first element -> same list ……

Premium This card is part of the premium lesson. Unlock
Explanation
Not allowed

data = ([10, 20], 30) data[0] = [100, 200] Error வரும……

Premium This card is part of the premium lesson. Unlock
Explanation
Allowed

data = ([10, 20], 30) data[0]……

Premium This card is part of the premium lesson. Unlock

Outer tuple immutable. Inner ……

Premium This card is part of the premium lesson. Unlock
Comparison
Mutability vs reassignment

இந்த concept exam-ல் மிகவும் முக்கியம். Mutation: Existing object-ஐ change செய்வது. Reassignment: Variable எந……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Mutable list example

a = [1, 2, 3] b = a a.append(……

Premium This card is part of the premium lesson. Unlock

Why? a மற்றும் b same List ob……

Premium This card is part of the premium lesson. Unlock
Flow
Reference flow

a -------| | v [1, 2, 3] ^ | b -------| a.append(4) ச……

Premium This card is part of the premium lesson. Unlock
Explanation
Immutable string reference

a = "Python" b = a a = "Java"……

Premium This card is part of the premium lesson. Unlock
Explanation

Initially: a ----| v "Python" ^ | b ----| பிறகு: a ……

Premium This card is part of the premium lesson. Unlock
Explanation
Function and immutability

Immutable objects function-க்கு pass ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

change(a)…

Premium This card is part of the premium lesson. Unlock

Why? x = x + 10 மூலம் function-க்குள் x புதிய integer o……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Mutable function example

def change(values): values.append(4……

Premium This card is part of the premium lesson. Unlock

List mutable என்பதால் functio……

Premium This card is part of the premium lesson. Unlock
Comparison
Comparison in functions
Explanation
Hashability connection

Immutability மற்றும் Hashability இரண்டு related concepts. Hashable object என்றால் அதன் hash value s……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

String and Integer keys valid.…

Premium This card is part of the premium lesson. Unlock
Explanation
List cannot be dictionary key

data = { [1, 2]: "value" }…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError: unhashable type: 'list' L……

Premium This card is part of the premium lesson. Unlock
Explanation
Tuple as dictionary key

Tuple-ல் உள்ள எல்லா items-மும……

Premium This card is part of the premium lesson. Unlock
Program
Example
Important
Important exception

All immutable objects are has……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

x = ([1, 2], 3) இந்த tuple itself immutable structure கொண்டது.……

Premium This card is part of the premium lesson. Unlock
Explanation
Frozenset

Normal set mutable.…

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

a.add(4)…

Premium This card is part of the premium lesson. Unlock

frozenset immutable version o……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இதற்கு: a.add(4) செய்ய முடியா……

Premium This card is part of the premium lesson. Unlock
Explanation
Bytes and bytearray

bytes immutable. bytearray mu……

Premium This card is part of the premium lesson. Unlock
Program
Example

bytes items direct-a modify ச……

Premium This card is part of the premium lesson. Unlock
Explanation
Why immutability is useful

Immutability Python-ல் பல adv……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Data Safety…

Premium This card is part of the premium lesson. Unlock
Explanation

ஒரு object accidentally modif……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Predictable Behaviour…

Premium This card is part of the premium lesson. Unlock
Explanation

Value unexpectedly change ஆகா……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Hashing…

Premium This card is part of the premium lesson. Unlock
Explanation

Immutable values dictionary k……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Sharing Objects…

Premium This card is part of the premium lesson. Unlock
Explanation

Same immutable object பல refe……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Easier Debugging…

Premium This card is part of the premium lesson. Unlock
Explanation

Value mutation குறைவாக இருப்பத……

Premium This card is part of the premium lesson. Unlock
Explanation
String slicing and immutability

String slicing original strin……

Premium This card is part of the premium lesson. Unlock

Actually Python case-sensitive ஆக இருப்பதால் ……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Uppercase example

text = "python" result = text……

Premium This card is part of the premium lesson. Unlock

இதுதான் String immutability-க……

Premium This card is part of the premium lesson. Unlock
Explanation
Common confusion 1

x = "Hello" x += " World"…

Premium This card is part of the premium lesson. Unlock
Question

+= original String-ஐ modify செய்கிறதா? No. Strings immutable. இ……

Premium This card is part of the premium lesson. Unlock
Explanation
Common confusion 2

x = 10 x += 5 இதுவும் 10 object-ஐ modify……

Premium This card is part of the premium lesson. Unlock
Explanation
Common confusion 3

Tuple immutable என்றால் அதன் உள்ளே எதையும் change செய்ய முடியாது என்பது completely correct அல்ல. Tuple ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 1

text = "Python" text[0] = "J"…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError Reason: String immu……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 2

t = (10, 20, 30) t[1] = 100…

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error

TypeError Reason: Tuple immut……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 3

number = 10 number[0] = 20 Int……

Premium This card is part of the premium lesson. Unlock
Comparison
Mutable vs immutable complete comparison
Comparison
Identity vs value

Python-ல் இரண்டு different concepts: V……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

a = "Python" b = "Python"…

Premium This card is part of the premium lesson. Unlock

இதன் meaning: Both have equal values. is என்பது same object identity-ஐ test செய்கிறது. ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Concept
Object creation concept

Immutable object-ல் ஒரு operation res……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Flow: x -> 10 10 + 5 | v 15 y……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Mutation example

x = [10, 20] x.append(30) Flow: Before: x -> [10, ……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Reassignment example

x = [10, 20] x = [100, 200] இங்கே old list modify ஆகவில்லை. Variable n……

Premium This card is part of the premium lesson. Unlock
MCQ

Which of the following is imm……

Premium This card is part of the premium lesson. Unlock
TRB Point

TRB, NET, SET exams-க்கு remember: String = Immutable Tuple = Immutable Integer = Immu……

Premium This card is part of the premium lesson. Unlock
Interview Point
Question

What does immutable mean in Python? Answer: An immutable object is an object whose value or inter……

Premium This card is part of the premium lesson. Unlock
Interview Point
Question

If String is immutable, how can this work? x = "Python" x = "Ja……

Premium This card is part of the premium lesson. Unlock
Interview Point
Question

Why are immutable objects useful as dictionary keys? Answer: Dictionary keys depend on a sta……

Premium This card is part of the premium lesson. Unlock
MCQ

Which Python data type is mut……

Premium This card is part of the premium lesson. Unlock
MCQ

What happens? s = "Python" s[……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is immutable?…

Premium This card is part of the premium lesson. Unlock
MCQ

What is the output? s = "pyth……

Premium This card is part of the premium lesson. Unlock
Mcq 5

What is the output? s = "pyth……

Premium This card is part of the premium lesson. Unlock
MCQ

Which statement is correct?…

Premium This card is part of the premium lesson. Unlock
MCQ

Which data type is mutable?…

Premium This card is part of the premium lesson. Unlock
Mcq 8

What happens? t = (1, 2, 3) t[1……

Premium This card is part of the premium lesson. Unlock
Mcq 9

What is the output? a = "Hell……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is the immutable versio……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 1

Predict: text = "Computer" text.replace("C", "K……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 2

Predict: text = "Computer" text =……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 3

Predict: a = [1, 2] b = a a.append(3) print……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 4

Predict: a = "Hi" b = a a = a……

Premium This card is part of the premium lesson. Unlock

Reason: A new String is created fo……

Premium This card is part of the premium lesson. Unlock
Remember

Mutable = Modify same object Immutable = Cannot modify sam……

Premium This card is part of the premium lesson. Unlock
Concept
Very important concept

Do not say: "Immutable variable cannot change." Correct statement: "Immutable object can……

Premium This card is part of the premium lesson. Unlock
TRB Point
Question

x = "Python" x += "3" print(x)…

Premium This card is part of the premium lesson. Unlock

Does this prove String is mut……

Premium This card is part of the premium lesson. Unlock

A new String "Python3" is cre……

Premium This card is part of the premium lesson. Unlock
Explanation
Advanced point

Immutability is about an object's observable state. For built-in immutable types like: int str tuple ……

Premium This card is part of the premium lesson. Unlock
Explanation
Complete concept map

Python Objects | +--------------------+ | | Mutable Immutable ……

Premium This card is part of the premium lesson. Unlock
Summary

Immutability என்பது: ஒரு object create செய்யப்பட்ட பிறகு அதன் value அல்லது internal state-ஐ direct-a modify ச……

Premium This card is part of the premium lesson. Unlock
Explanation
Short description

Immutability means that once an object is created, its value cannot be changed directly. In Python, String, T……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program