Intensity Coding
  • HOME
  • TUTORIALS
    • Python
    • Natural Language Processing
    • Generative AI
  • ABOUT US
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms and Conditions
    • Disclaimer

Python

Mutable Vs Immutable Objects And Collection in Python

Python
Python
  • Introduction To Python
  • Write Your First Python Program
  • Indentation And Comments in Python
  • Variables in Python
  • Data Types in Python
  • Booleans and Number in Python
  • Operators in Python
  • Mutable Vs Immutable Objects And Collection in Python
  • Python String
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Control Statements
  • Python Functions
  • Python Lambda Functions
  • Python Exception Handling
  • Python File Handling
  • Python Package and PIP
  • Python Modules
  • Python Keywords
  • Python Built In Method
  • Python Regular Expressions (RegEx)
  • Python JSON
  • Python Datetime

Python Mutable vs Immutable Objects¶

  • In Python, data types can be categorized as either mutable or immutable.
  • An immutable object can’t be changed after it is created.
Class Description Immutable
bool Boolean value Yes
int integer Yes
float floating-point number Yes
list mutable sequence of object No
tuple immutable sequence of object Yes
str character string Yes
set unordered set of distinct objects No
frozenset immutable form of set class Yes
dict associative mapping NO

Python Immutable Data Types:¶

  • Numbers (int, float, complex): Numeric data types are immutable. When you perform operations that change their value, a new object is created in memory.
In [ ]:
x = 10
y = x  # y references the same 10
print("id of x: ",id(x))
print("id of y: ", id(y))
x += 1  # Now x is 11, a new object is created
print("New id of x: ",id(x))
id of x:  10751144
id of y:  10751144
New id of x:  10751176
  • Strings: Strings are immutable. You can't change the characters of a string once it's created. When you modify a string, a new string is created.
In [ ]:
my_str = "Intensity"
print("id of my_str: ",id(my_str))
my_str += " Coding"  # Creates a new string with "Intensity Coding"
print("New id of my_str: ",id(my_str))
id of my_str:  137843795649776
New id of my_str:  137843795581712
  • Tuples: Tuples are also immutable. You can't change their elements after creation.
In [ ]:
my_tuple = (1, 2, 3)
# This is not allowed: my_tuple[0] = 10

Python Mutable Data Types:¶

  • Lists: Lists are mutable. You can change, add, or remove elements from a list after creation.
In [ ]:
my_list = [1, 2, 3]
my_list.append(4)  # Modifies the list in place
print(my_list)
[1, 2, 3, 4]
  • Dictionaries: Dictionaries are mutable. You can modify the key-value pairs in a dictionary.
In [ ]:
my_dict = {'name': 'Raj', 'age': 35}
my_dict['age'] = 31  # Changes the 'age' value
print(my_dict)
{'name': 'Raj', 'age': 31}
  • Sets: Sets are mutable. You can add or remove elements from a set.
In [ ]:
my_set = {1, 2, 3}
my_set.add(4)  # Modifies the set to include 4
print(my_set)
{1, 2, 3, 4}

Python Collections¶

  • There are four collection data types in the Python programming language:

1. List

  • Lists are ordered and mutable(changeable) collections of data.
  • Allows duplicate members.
  • They are created using square brackets [ ].
In [ ]:
my_list = [1, 2, 3, 4, 5]

2. Tuple

  • Tuples are ordered and immutable(unchangeable) collections of data.
  • Allows duplicate members.
  • They are created using parentheses ( ).
In [ ]:
my_tuple = (1, 2, 3, 4, 5)

3. Set

  • Sets are unordered collections of unique elements which is unchangeable and unindexed.
  • No duplicate members.
  • Set items are unchangeable, but you can remove and/or add items whenever you like.
  • They are created using curly braces { } or with the set() constructor.
In [ ]:
my_set = {1, 2, 3, 4, 5}

4. Dictionary

  • Dictionaries are collections of key-value pairs.
  • It is ordered and changeable.
  • No duplicate members.
  • They are created using curly braces { } with key-value pairs.
In [ ]:
my_dict = {"name": "Raj", "age": 35, "city": "Jaipur"}
Category
Python Natural Language Processing Generative AI
Tag's
Python Natural Language Processing

Quick Links

  • Home
  • Tutorials
  • About Us
  • Contact Us

Tutorials

  • Python
  • Natural Language Processing
  • Generative AI

Tags

  • Python
  • Natural Language Processing

About Intensity Coding

  • Intensity Coding is your learning hub for Data Science, Machine Learning, Deep Learning, NLP, and Generative AI. From beginner to expert level, we offer clear tutorials, real-world examples, and up-to-date insights. Our content simplifies complex topics to help you grow in your AI journey.
Intensity Coding

© 2025 Intensity Coding. All Rights Reserved.

Privacy Policy Terms and Conditions Disclaimer