You are on page 1of 3

2140290_Program2 29/07/22, 2:20 AM

2140290, Aditya Kumar Mishra

Program 2: Mutable and Immutable Types


Mutable Datatype
Mutable is when something is changeable or has the ability to change. In Python,
‘mutable’ is the ability of objects to change their values. These are often the objects that
store a collection of data.

Different types of Mutable objects


1. List
In [1]:
Item1 = ["Nivea Men","Beauty","175.00","2000","Nivea"]
print(Item1, type(Item1))

['Nivea Men', 'Beauty', '175.00', '2000', 'Nivea'] <class 'list'>

In [2]:
Item1.remove("175.00")
Item1

Out[2]: ['Nivea Men', 'Beauty', '2000', 'Nivea']

In [3]:
Item1.insert(1, "17562")
Item1

Out[3]: ['Nivea Men', '17562', 'Beauty', '2000', 'Nivea']

2. Sets
In [4]:
Item2 = {"Vim Bar", "Cleaning", "25.00","1800","Unilever"}
print(Item2, type(Item2))

{'Unilever', '25.00', 'Vim Bar', '1800', 'Cleaning'} <class 'set'>

In [5]:
Item2.add("15622")
print(Item2)

{'Unilever', '15622', '25.00', 'Vim Bar', '1800', 'Cleaning'}

In [6]:
Item2.remove("Unilever")
Item2

Out[6]: {'15622', '1800', '25.00', 'Cleaning', 'Vim Bar'}

3. Dictionary

http://localhost:8888/nbconvert/html/2140290_Program2.ipynb?download=false Page 1 of 3
2140290_Program2 29/07/22, 2:20 AM

In [7]:
Items = {1: "Nivea Men Facewash", 2: "Vim Bar Soap", 3: "Park Avenue Good Mornin
print(Items, type(Items))
print("Accessing a Item using key:")
print(Items[2])

{1: 'Nivea Men Facewash', 2: 'Vim Bar Soap', 3: 'Park Avenue Good Morning'}
<class 'dict'>
Accessing a Item using key:
Vim Bar Soap

In [8]:
Items2 = Items.copy()
print(Items2)
Items.clear()
print(Items)

{1: 'Nivea Men Facewash', 2: 'Vim Bar Soap', 3: 'Park Avenue Good Morning'}
{}

Immutable Datatype
Immutable is the when no change is possible over time. In Python, if the value of an
object cannot be changed over time, then it is known as immutable. Once created, the
value of these objects is permanent

Different Types of Immutable objects


1. Tuples
In [9]:
Items = ("Nivea Men","Beauty","175.00","2000","Nivea")
print(Items, type(Items))
Items2=("Vim Bar", "Cleaning", "25.00","1800","Unilever")
print(Items2)
print(Items+Items2)

('Nivea Men', 'Beauty', '175.00', '2000', 'Nivea') <class 'tuple'>


('Vim Bar', 'Cleaning', '25.00', '1800', 'Unilever')
('Nivea Men', 'Beauty', '175.00', '2000', 'Nivea', 'Vim Bar', 'Cleaning', '
25.00', '1800', 'Unilever')
2. Frozen Sets
In [10]:
Items1=frozenset(Item2)
print(Items1)

frozenset({'1800', '25.00', 'Vim Bar', '15622', 'Cleaning'})

Program

http://localhost:8888/nbconvert/html/2140290_Program2.ipynb?download=false Page 2 of 3
2140290_Program2 29/07/22, 2:20 AM

In [11]:
print("Welcome to DMart Items Login!!!")
ItemsData={"12545":{"Name":"Nivea Men Cream","Price":"175.00","Company":"Nivea"

Itemss={"12545":"123","12684":"234","13569":"345"}

I=int(input("Enter the Item Code:"))

if I in Itemss:
pwd=str(input("Enter the password:"))
if pwd==Itemss[I]:
print("Welcome")
ch= str(input("Do you want to Change Details(Yes/No):"))
if ch=="Yes":
print(ItemsData[I])
while ch=="Yes":
d=int(input("What Do you want to Change: \n1. Name \n2. Price
if d ==1:
x=str(input("Enter the New Name:"))
ItemsData[I]["Name"] = x
print("The Name has been Updated")
print(ItemsData[I])
elif d ==2:
x=str(input("Enter the New Price:"))
ItemsData[I]["Price"] = x
print("The Price has been Updated")
print(ItemsData[I])
elif d==3:
x=str(input("Enter the New Company:"))
ItemsData[I]["Company"] = x
print("The Company Name has been Updated")
print(ItemsData[I])
elif d==4:
x=str(input("Enter the New Stock:"))
ItemsData[I]["Stocks"] = x
print("The Stocks has been Updated")
print(ItemsData[I])
if ch=="No":
print("Logging Out....")
else:
print("Item doesn't exist...")
print("End of the Program")

Welcome to DMart Items Login!!!


Enter the Item Code:16739
Item doesn't exist...
End of the Program

In [ ]:

http://localhost:8888/nbconvert/html/2140290_Program2.ipynb?download=false Page 3 of 3

You might also like