Python Non-Primitive Data Types – Mutability &
Features
Non-primitive data types in Python can store multiple values in a single variable. Each type has
unique properties such as mutability, ordering, indexing, and usage scenarios.
1. List
A list is an ordered collection of elements that is mutable.
• Mutable – elements can be changed after creation
• Ordered – maintains insertion order
• Indexed – supports positive and negative indexing
• Allows duplicate values
• Can store mixed data types
• Represented using square brackets []
Example:
nums = [1, 2, 3]
nums[1] = 20 # Allowed
2. Tuple
A tuple is an ordered collection of elements that is immutable.
• Immutable – elements cannot be changed after creation
• Ordered – maintains insertion order
• Indexed – supports indexing
• Allows duplicate values
• Faster than lists
• Represented using parentheses ()
Example:
t = (1, 2, 3)
# t[1] = 20 → Error
3. Set
A set is an unordered collection of unique elements.
• Mutable – elements can be added or removed
• Unordered – no indexing or slicing
• Does not allow duplicate values
• Faster membership checking
• Represented using curly braces {}
Example:
s = {1, 2, 3}
[Link](4)
4. Dictionary
A dictionary stores data in key-value pairs.
• Mutable – values can be updated
• Ordered (Python 3.7+)
• Keys must be unique
• Fast lookup using keys
• Represented using curly braces with key:value pairs
Example:
d = {'id': 1, 'name': 'Sam'}
d['name'] = 'Ram'
Summary of Mutability
• List → Mutable
• Tuple → Immutable
• Set → Mutable
• Dictionary → Mutable