Mutable and Immutable Variables

Mutable and Immutable Variables

Data Types in Python

Data types in python identify the type of data that a variable can store. Besides, a variable is basically an object or an element that we store in the memory. This variable stores different types of values. Hence, the data type decides the type of a value. Every value in python belongs to a different data type. Moreover, there are various types of data types in python. These are as follows Number: int, float, complex, Boolean, Sequence: string, list, tuple, Set, None, Mapping: dictionary. We can also divide these variables into mutable and immutable variables.

Mutable and Immutable Variables

We can divide all these different variables into mutable and immutable variables. Sometimes we may need to change the value of a variable after creating it. In python, some variables can change their value after creation while some cannot. Therefore, if a variable can change its value it is mutable in nature. Otherwise, if it cannot change its value after creation it is immutable in nature. Moreover, if we try to change the value of an immutable variable then, the old variable destroys and a new variable is created with the same name and new value. Hene, mutable variables can change their state or content. Whereas immutable variables cannot change their state or content.

Mutable Variables

Such variables can change their value or state. Therefore, once we assign them the value during declaration we can make changes in the future if a need arises. Examples of mutable variables in Python are as follows:

  • Lists
  • Dictionary

Example of a list

                    

# creating a list

>>> list1 = [ 1, 33, 'hello', 45.3 ]

# printing list items

>>> print (list1)

[ 1, 33, 'hello', 45.3 ]

# changing value at index 0

>>> list1 [0] = 'hi'

>>> print (list1)

[ 'hi', 33, 'hello', 45.3 ]

Immutable Variables

Such variables cannot change their value or state. Therefore, once we assign them the value during declaration we cannot make changes in the future if a need arises. Moreover, if we try to change an immutable variable python gives an error. Examples of immutable variables in Python are as follows:

  • int
  • float
  • bool
  • complex
  • Strings
  • tuples
  • sets

Example of a tuple

                    

# creating a tuple

>>> tuple1 = ( 1, 33, 'hello', 45.3 )

# printing tuple items

>>> print (tuple1)

( 1, 33, 'hello', 45.3 )

# making changes in the tuple

>>> tuple1 [0] = 22

>>> print (tuple1)

TypeError: ‘tuple’ object does not support item assignment

Hence, we cannot assign a new value to an old tuple else python gives an error.

Frequently Asked Questions (FAQs)

Q1. What is a data type?

A1. Data types in python identify the type of data that a variable can store.

Q2. What is a variable in python?

A2. A variable is basically an object or an element that we store in the memory. This variable stores different types of values.

Q3. What are the different data types in python?

A3. Different data types in Python are as follows:

  • Number: int, float, complex
  • Boolean
  • Sequence: string, list, tuple
  • Set
  • None
  • Mapping: dictionary

Q4. What are mutable variables?

A4. Mutable variables can change their value or state.

Q5. What are immutable variables?

A5. Immutable variables can change their value or state.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

Mutable and Immutable Variables
  • Mutable and Immutable Variables

Leave a Reply

Your email address will not be published. Required fields are marked *

Browse

Mutable and Immutable Variables
  • Mutable and Immutable Variables

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.