0% found this document useful (0 votes)
42 views4 pages

Mutability and Immutability

Lists are mutable collections that can contain heterogeneous elements. Elements in a list are accessed via indexes and lists allow insertion, deletion, and updating of elements. Lists are mutable datatypes as their elements can be modified, unlike primitive datatypes like int and string which are immutable.

Uploaded by

Divyanshu Rawat
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)
42 views4 pages

Mutability and Immutability

Lists are mutable collections that can contain heterogeneous elements. Elements in a list are accessed via indexes and lists allow insertion, deletion, and updating of elements. Lists are mutable datatypes as their elements can be modified, unlike primitive datatypes like int and string which are immutable.

Uploaded by

Divyanshu Rawat
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
You are on page 1/ 4

List

In [1]:

# Collections : More than one entity

In [22]:

# List is a collection datatype.


# It is a collection of Heterogenous (different type) Elements.
# List is a Mutable DataType.

# Insert
# Delete
# Update

In [37]:

a = [1,2,3,4,5,6,7,8,9,10] # How to declare of a List ?

In [24]:

# Indexing

# Consider your List as a colony, and elements in it as the houses.


# Now in this colony every house has a address (a number) which is in terms of programm
ing
# known as Index.

In [25]:

a[3] # How to access a element in Python List

# Syntax : <listVariable>[<index of the element you want to access>]

Out[25]:

In [26]:

a[7]

Out[26]:

Mutability and Immutability


In [27]:

# Mutability : Any entity is said to be mutable if I can edit the elements inside it.
In [28]:

# Immutablity : Any entity in which I can't edit it's elements it is immutable.

In [29]:

# Mutable Datatypes : The datatypes in which we can edit the data stored inside them ar
e known as
# Mutable Datatypes.

In [30]:

# Immutable Datatypes : the datatypes in which we can't edit the data stored inside the
m are
# known as immutable Datatypes.

In [31]:

id(a)

Out[31]:

3004529326984

In [33]:

Out[33]:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [34]:

id(a)

Out[34]:

3004529334280

In [35]:

a = ["Divyanshu","Yash"]

In [36]:

id(a)

Out[36]:

3004530273928

In [39]:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [40]:

id(a)

Out[40]:

3004529333768

In [43]:

# How to update an element at a particular index ?

# Syntax : <listVariable>[<index you want to replace>] = <new Value>

In [44]:

Out[44]:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [45]:

a[3] = "Yash"

In [46]:

Out[46]:

[1, 2, 3, 'Yash', 5, 6, 7, 8, 9, 10]

In [47]:

id(a)

Out[47]:

3004529333768

In [ ]:

In [ ]:

In [53]:

# int, bool, float, str : Primitive Datatype


# Primitve datatype are immutable.

In [49]:

x = 2
In [50]:

id(x)

Out[50]:

140714545553840

In [51]:

x = 3

In [52]:

id(x)

Out[52]:

140714545553872

In [ ]:

You might also like