Python Unit 1(Lecture 5)
Python Unit 1(Lecture 5)
Programming
(UNIT-1)
Lecture-5
Mutable Data types
• Mutable in Python can be defined as the object that can change or be regarded as
something changeable in nature. Mutable means the ability to modify or edit a value.
• Mutable objects in Python enable the programmers to have objects that can change
their values. They generally are utilized to store a collection of data. It can be regarded
as something that has mutated, and the internal state applicable within an object has
changed.
• Mutable means those objects which can change themselves after we instantiate them.
There are several methods and functions by which we can change the mutable objects.
By using those methods and functions the original objects get modified.
• While doing any changes to the mutable objects the memory at which those objects
are stored remains the same
• Example: list, dictionary, set and user-defined classes.
color = ["Red", "Green", "Blue"]
print(color)
Output:
color[0] = "Black"
color[-1] = "White"
print(color)
Output:
Example:
greeting[0] = 'Hello'
print(greeting)
Output:
Error
Conclusion
• Mutable and immutable objects are handled differently in python. Immutable objects are quicker
to access and are expensive to change because it involves the creation of a copy.
Whereas mutable objects are easy to change.
• Use of mutable objects is recommended when there is a need to change the size or content of
the object.
• Exception : However, there is an exception in immutability as well. We know that tuple in python
is immutable. But the tuple consists of a sequence of names with unchangeable bindings to
objects.
Consider a tuple
• The tuple consists of a string and a list. Strings are immutable so we can’t change its value. But
the contents of the list can change. The tuple itself isn’t mutable but contain items that are
mutable.
Getting the Data Type
• The Data type of any object can be find out using type() function
Example:
x=5
print(type(x))
int(10 + 2j)
#output = Type Error
4. str to int
int(‘15’)
#output = 15
float() : to convert from other type to float
1. int to float
float(10)
#output = 10.0
float(0b111)
#output = 7.0
2. Complex to float
float(10 + 20 j)
#output = Type Error
Note : Complex number can not be converted to float type.
3. Bool to float
float(True)
#output = 1.0
float(False)
#output = 0.0
4. str to float
float(“10”)
#output = 10.0
float(“20.6”)
#output = 20.6
1st case :
complex(10) = 10 + 0j // output
2nd case:
complex(x,y) = x + yj //output
Float to complex
complex(10.5) = 10.5 + 0j
Boolean to complex
complex(True) = 1 + 0j # output
Str to complex
complex(‘10’) = 10 + 0j # output
bool() : to convert from other type to bool
1. Int to bool
bool(10) = True #output
bool(0) = False #output
bool(-10) = True #output
2. Float to bool
bool(0.0) = False #output
bool(0.1) = True #output
bool(1.2) = True #output
3. str to bool
bool(‘True’) = True #output
bool(‘yes’) = True #output
bool(‘ ‘) = False #output(Empty string)
4. Complex to bool
bool(0 + 0j) = False
bool(1 + 0j) = True
Note: Both real and imag no. are zero then only it will result false