Mutable vs Immutable Objects in Python
In Python, everything is an object. Every variable in python holds an instance of an object. There are two types of objects in python i.e. Mutable and Immutable objects. Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime. An object whose internal state can be changed is mutable. On the other hand, immutable doesn’t allow any change in the object once it has been created.
built-in mutable Objects are:
Lists
Sets
Dictionaries
built-in immutable Objects are:
Numbers (Integer, Float, Complex & Booleans)
Strings
Tuples
Examples of mutable Objects
# lists are mutable
fruits = ["apple", "mango", "grapes"]
print(fruits)
fruits[0] = "watermeleon"
fruits[-1] = "bananna"
print(fruits)
Output:
["apple", "mango", "grapes"]
["watermeleon", "mango", "bananna"]
Examples of immutable Objects
a=111
print("a before change is",a)
print("the id of a before change is",id(a))
a=222
print("a after change is",a)
print("the id of a after change is",id(a))
Output:
a before change is 111
the id of a before change is 9796768
a after change is 222
the id of a after change is 140032307887024
#Example 2
a = "Hello, World!"
a[0]='a'
print(a)
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in
TypeError: 'str' object does not support item assignment
#Example 3
# tuples are immutable
a = (0, 1, 2, 3)
a[0]=8
print(a)
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in
TypeError: 'tuple' object does not support item assignment
Examples of mutable Objects
# lists are mutable fruits = ["apple", "mango", "grapes"] print(fruits) fruits[0] = "watermeleon" fruits[-1] = "bananna" print(fruits) Output: ["apple", "mango", "grapes"] ["watermeleon", "mango", "bananna"]Examples of immutable Objects
a=111 print("a before change is",a) print("the id of a before change is",id(a)) a=222 print("a after change is",a) print("the id of a after change is",id(a)) Output: a before change is 111 the id of a before change is 9796768 a after change is 222 the id of a after change is 140032307887024 #Example 2 a = "Hello, World!" a[0]='a' print(a) Output: Traceback (most recent call last): File "./prog.py", line 2, in
Write a Python program to read a word and print the number of letters, vowels and percentage of vowels in the word using a dictionary
vowels = 'aeiou'
ip_str = 'Hello, welcome to python programming'
# casefold converts upper case to lowercase
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
write a python program to find the largest word in a file. get the filename from the user
def longest_words(filename):
with open(filename, 'r') as infile:
words = infile.read().split()
return(max(words, key=len))
a=input("enter filename")
print(longest_words(a))
6. develop a python program to search the string 'laptop' in "sales.txt" file and print its line along with the line number.
def search_str(file_path, word):
with open(file_path, 'r') as file:
c=0
# read all content of a file
x = len(file.readlines())
print('Total lines:', x)
file.seek(0,0)
for index in range(x):
line = next(file)
if word in line:
print("string exist in a file at Line No %d - %s" % (index, line))
c+=1
if(c==0):
print("string doesnot exist in a file")
search_str(r'123.txt', 'laptop')
def search_str(word):
file=open("123.txt", 'r')
c=0
x = len(file.readlines())
print('Total lines:', x)
file.seek(0,0)
for index in range(x):
line = next(file)
if word in line:
print("string exist in a file at Line No %d - %s" % (index, line))
c+=1
if(c==0):
print("string doesnot exist in a file")
search_str('sandeep')
def search_str(word): file=open("123.txt", 'r') c=0 x = len(file.readlines()) print('Total lines:', x) file.seek(0,0) for index in range(x): line = next(file) if word in line: print("string exist in a file at Line No %d - %s" % (index, line)) c+=1 if(c==0): print("string doesnot exist in a file") search_str('sandeep')
Nice Blog Thanks for sharing this post with a lot of useful information. I would like to get updates from you. Keep blogging..
ReplyDeleteInformation Technology
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
I think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information for free
ReplyDeleteJTAG
Keep sharing such a valuable information. Thanks..
ReplyDeleteInformation Technology
Great article, I hope that you will going to post another one.
ReplyDeleteInformation Technology
Great article, I hope that you will going to post another one.
ReplyDeleteInformation Technology
Great article, I hope that you will going to post another one.
ReplyDeleteInformation Technology
Great article, I hope that you will going to post another one.
ReplyDeleteInformation Technology
Great article, I hope that you will going to post another one.
ReplyDeleteInformation Technology
Best Online Summer Internship Training
ReplyDeleteSummer Training in Noida
Python Training in Noida
Machine Learning Training in Noida
Data Science Training in Noida
Digital Marketing Training in Noida
Best Online Training Company
Best Online Internship Training Company
very informative post. Python Training In Amravati
ReplyDeleteThanks for sharing article .
ReplyDeletePython Course in Solapur
Thanks for sharing the valuable information.
ReplyDeleteEnigmaspark
IT Jobs Support
Load Runner Job Support
Thanks for sharing the valuable information.
ReplyDeleteEnigmaspark
IT Jobs Support
Load Runner Job Support