0% found this document useful (0 votes)
3 views2 pages

Python Non Primitive Datatypes Mutability

The document discusses non-primitive data types in Python, including lists, tuples, sets, and dictionaries, highlighting their mutability, ordering, indexing, and usage. Lists and dictionaries are mutable, while tuples are immutable, and sets are unordered collections of unique elements. Each data type has specific properties and examples demonstrating their characteristics.

Uploaded by

legendarysakthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python Non Primitive Datatypes Mutability

The document discusses non-primitive data types in Python, including lists, tuples, sets, and dictionaries, highlighting their mutability, ordering, indexing, and usage. Lists and dictionaries are mutable, while tuples are immutable, and sets are unordered collections of unique elements. Each data type has specific properties and examples demonstrating their characteristics.

Uploaded by

legendarysakthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like