KEMBAR78
Mutable data types in python | PDF
Data Types in Python
Swipe
Mutable Data Type in Python
Mutable
Data Types
a.List
b.Bytes arrays
c.Sets
d.Dictionaries
Note:-
Mutable sequences can be changed after creation. Some of
Python’s mutable data types are: lists, byte arrays, sets, and
dictionaries.
As you saw earlier, lists are mutable. Here’s another
example using the append() method:
a = list(('apple', 'banana', 'clementine'))
print(id(a))
a.append('dates')
print(id(a))
[Out:]
140372445629448
140372445629448
List
Byte arrays represent the mutable version of bytes
objects. They expose most of the usual methods of
mutable sequences as well as most of the methods of the
bytes type. Items are integers in the range [0, 256).
b = bytearray(b'python')
print(id(b))
b.replace(b'p', b'P')
print(id(b))
[Out:]
139963525979808
139963525979808
Byte Arrays
Sets
Python provides two set types, set and frozenset. They are
unordered collections of immutable objects.
c = set((‘San Francisco’, ‘Sydney’, ‘Sapporo’))
print(id(c))
c.pop()
print(id(c))
[Out:]
140494031990344
140494031990344
As you can see, sets are indeed mutable. Later, in the
Immutable Data Types section, we will see that frozensets
are immutable.
Represents an unordered collection of keys and values. The
Dictionary data type is optimized for fast lookup of values.
The following methods are available on instances of the
Dictionary data type
Dictionaries
Immutable Data Type in Python.
Object Reusability in Python.
Stay Tuned with
Topics for next Post

Mutable data types in python

  • 1.
    Data Types inPython Swipe
  • 2.
    Mutable Data Typein Python Mutable Data Types a.List b.Bytes arrays c.Sets d.Dictionaries Note:- Mutable sequences can be changed after creation. Some of Python’s mutable data types are: lists, byte arrays, sets, and dictionaries.
  • 3.
    As you sawearlier, lists are mutable. Here’s another example using the append() method: a = list(('apple', 'banana', 'clementine')) print(id(a)) a.append('dates') print(id(a)) [Out:] 140372445629448 140372445629448 List
  • 4.
    Byte arrays representthe mutable version of bytes objects. They expose most of the usual methods of mutable sequences as well as most of the methods of the bytes type. Items are integers in the range [0, 256). b = bytearray(b'python') print(id(b)) b.replace(b'p', b'P') print(id(b)) [Out:] 139963525979808 139963525979808 Byte Arrays
  • 5.
    Sets Python provides twoset types, set and frozenset. They are unordered collections of immutable objects. c = set((‘San Francisco’, ‘Sydney’, ‘Sapporo’)) print(id(c)) c.pop() print(id(c)) [Out:] 140494031990344 140494031990344 As you can see, sets are indeed mutable. Later, in the Immutable Data Types section, we will see that frozensets are immutable.
  • 6.
    Represents an unorderedcollection of keys and values. The Dictionary data type is optimized for fast lookup of values. The following methods are available on instances of the Dictionary data type Dictionaries
  • 7.
    Immutable Data Typein Python. Object Reusability in Python. Stay Tuned with Topics for next Post