0% found this document useful (0 votes)
3 views18 pages

Python Unit 1(Lecture 5)

The document discusses mutable and immutable data types in Python, explaining that mutable objects can change their values while immutable objects cannot. Examples of mutable types include lists and dictionaries, whereas strings and tuples are examples of immutable types. It also covers type conversion methods and how to determine the data type of an object using the type() function.

Uploaded by

upasanabishtiitm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Python Unit 1(Lecture 5)

The document discusses mutable and immutable data types in Python, explaining that mutable objects can change their values while immutable objects cannot. Examples of mutable types include lists and dictionaries, whereas strings and tuples are examples of immutable types. It also covers type conversion methods and how to determine the data type of an object using the type() function.

Uploaded by

upasanabishtiitm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Basics of Python

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:

['Red', 'Green', 'Blue’]

color[0] = "Black"
color[-1] = "White"
print(color)

Output:

['Black', 'Green', 'White']


Immutable
• Immutable objects in Python can be defined as objects that do not change their
values and attributes over time.
These objects become permanent once created and initialized, and they form a critical
part of data structures used in Python.
• Python is used in numbers, tuples, strings, frozen sets, and user-defined classes with
some exceptions. They cannot change, and their values and it remains permanent once
they are initialized and hence called immutable.
• Immutable means those objects which can't change themselves after we initialize
them. There are no methods and functions which can be used to modify those
immutable objects. We have to convert those immutable objects to the mutable once
and then we can modify those objects.
• While doing any changes to the immutable objects, the memory at which these objects
were stored during initialization, gets updated
Example:

int, float, decimal, bool, string, tuple, and range.

Example:

greeting = "Welcome to EyeHunts"

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

• tup = ([3, 4, 5], 'myname')

• 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))

Output <class ‘int’>


Type Conversion
We can convert one type to another with the int(), float(), and
complex() methods:

int() : To convert from other types to int.


1. float to int
int(10.989)
#output = 10
2. Complex to int

int(10 + 2j)
#output = Type Error

Note : Complex number can not be converted to int type.


3. bool to int
int(True)
#output = 1
int(False)
#output = 0

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

Note string should contain either int or float


float(“0xface”)
#output = Error
float(“upasana”)
#output = Error
Complex : To convert from other types to complex.

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

You might also like