Mutable data types in python PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Mutable data types in python


1
Data Types in Python
Swipe
2
Mutable Data Type in Python
Note- Mutable sequences can be changed after
creation. Some of Pythons mutable data types
are lists, byte arrays, sets, and dictionaries.
a.List
Mutable Data Types
b.Bytes arrays
c.Sets
d.Dictionaries
3
List
As you saw earlier, lists are mutable. Heres anot
her example using the append() method a
list(('apple', 'banana', 'clementine'))
print(id(a)) a.append('dates')
print(id(a)) Out 140372445629448 140372445629
448
4
Byte Arrays
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 139
963525979808
5
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)) O
ut 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
Dictionaries
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
7
Topics for next Post
  • Immutable Data Type in Python. Object
    Reusability in Python.
  • Stay Tuned with
Write a Comment
User Comments (0)
About PowerShow.com