Python 3: Mutable, Immutable… everything is object!

Sergio Steben Arias Quintero
3 min readMay 27, 2020

Introduction

The basis of the OOP are objects. You can imagine objects as a new type of data whose definition is given in a structure called class.

Let’s see it with an example:

We are going to see the classes as if they were cookie molds and the objects with the cookies themselves, all cookies made from the same mold have the same shape, each taking on individual attributes after baking. Things like color, texture, taste … can be very different.

ID

The id() function returns identity of the object. All objects in Python has its own unique id. The id is assigned to the object when it is created.it is also known as the memory address where the object is located.

Example

>>> x = (‘apple’, ‘banana’, ‘cherry’)
>>> y = id(x)
>>> print(y)
47836384565144

Type

A data type is the form of representation of data and indicates how it can be handled. This includes imposing restrictions on the data, such as what values they can take and what operations can be performed.

To find out what type an object is, we use the built-in function type ()

Numbers:

We can distinguish two types of numbers:

Integers (int): That do not have a decimal part and range from less infinity to more infinity.

Example:

>>> a = 1

>>> type(a)
<class ‘int’>

Floating or Decimals(float): Numbers that have a decimal part written with a period.

Example:

>>> pi = 3.14
>>> type(pi)
<class ‘float’>

Booleans (bool):

Boolean data types are used to represent true and false, using the True or False keyword respectively. This type of data is very important in controlling the flow of a program.

Example:

>>> 5 > 3
True

>>> type(True)
<class ‘bool’>

Text strings

Strings (str): They are always defined in single or double quotes:

Example:

>>> x = “Hello World”
>>> print(x)
Hello World

>>> type(x)
<class ‘str’>

Collections

Python integrates a lot of collections to handle data. A collection allows several objects to be grouped under the same name.

Lists (list): A list is a collection of objects: integers, floats, complexes, strings, etc. A list is delimited using [] and its elements must be separated by commas. Its elements can be accessed by indicating the index of the desired element.

Example:

>>> languages = [“Python”, “Java”, “C”, “C++”]
>>> languages[0]
‘Python’
>>> languages[1]
‘Java’

>>> type(languages)
<class ‘list’>

Tuples (tuple): They are collections very similar to lists with the peculiarity that they are immutable. A tuple is delimited using () and its elements must be separated by commas:

Example:

>>> languages = (“Python”, “Java”, “C”, “C++”)
>>> languages[0]
‘Python’
>>> languages[1]
‘Java’

>>> type(languages)
<class ‘tuple’>

Dictionary (dict): A dictionary is made up of two parts: a key (word) and a value (definition). Keys must always be a primitive data type. The key and the value are separated with : . A dictionary is delimited using {} and its elements must be separated by commas.

To access any of the values, we must indicate its key in square brackets.

Example:

>>> d = {“Python”: 1991, “C”: 1972, “Java”: 1996}
>>> d[“Java”]
1996

>>> type(d)
<class ‘dict’>

Mutable objects

It is not possible to change, modify or update the content to an immutable data type, although as with any variable it is possible to assign a new value to it.

Immutable objects

They are all those to whom it is possible to change, modify or update their content. The most common mutable objects are lists, dictionaries, and sets used to store collections of data.

Why does it matter and how differently does Python treat mutable and immutable objects

Immutability is a very powerful concept that can help us simplify the complexity of understanding our code and, therefore, reduce the probability of unexpected errors.

it also offers greater security than mutable objects.

--

--