Tuples

Immutable Tuples

Immutable Tuples

Tuples are basically a data type in python. These tuples are an ordered collection of elements of different data types. Furthermore, we represent them by writing the elements inside the parenthesis separated by commas. We can also define tuples as lists that we cannot change. Therefore, we can call them immutable tuples. Hence, tuples are not modifiable in nature. These immutable tuples are a kind of group data type. Moreover, we access elements by using the index starting from zero. Let us study more about immutable tuples.

immutable tuples

Basic Examples of Tuples

                    

Example 1: Tuple with integers as elements

>>>tup = (22, 33, 5, 23)

>>>tup

(22, 33, 5, 23)

Example 2: Tuple with mixed data type

>>>tup2 = ('hi', 11, 45.7)

>>>tup2

('hi', 11, 45.7)

Example 3: Tuple with a tuple as an element

>>>tup3 = (55, (6, 'hi'), 67)

>>>tup3

(55, (6, 'hi'), 67)

Example 4: Tuple with a list as an element

>>>tup3 = (55, [6, 9], 67)

>>>tup3

(55, [6, 9], 67)

If there is only a single element in a tuple we should end it with a comma. Since writing, just the element inside the parenthesis will be considered as an integer.

For example,

                    

>>>tup=(90)

>>>tup

90

>>>type(tup)

<class 'int'>

Correct way of defining a tuple with single element is as follows:

>>>tup=(90,)

>>>tup

(90,)

>>>type(tup)

<class 'tuple'>

Moreover, if you write any sequence separated by commas, python considers it as a tuple.

For example,

>>> seq = 22, 4, 56

>>>seq

(22, 4, 56)

>>>type(seq)

<class 'tuple'>

Browse more Topics Under Tuples and its Functions

Examples of Immutable Tuples

Since we know that tuples are immutable in nature. This means that once we create a tuple we cannot make changes to it. Moreover, if we try to do so it will result in an error.

For example,

                    

>>>tuple1 = (1, 2, 33, 44, 6)

>>>tuple1[4] = 10

TypeError: 'tuple' object does not support item assignment

Difference between List and Tuple

Although both lists and tuples are a type of collection data type in python, they have several differences. These differences between list and tuple are as follows:

List Tuple
 1. Lists are mutable in nature. Thus, we can make changes even after creating it. 1. Tuples are immutable in nature. Thus, we cannot make changes after creating it.
2. Implementing iteration is slower in lists. 2. On the other hand, implementing iteration in tuples is comparatively faster.
3. It consumes more memory area. 3. It consumes memory lesser than the lists.
4. The list data type is better when it comes to performing operations such as insertion and deletion. 4. On the other hand, tuples are better when it comes to accessing the elements.
5. Lists have various built-in functions. 5. Tuples do not have many built-in functions.
6. The occurrence of errors and changes are more likely to occur in the lists. 6. The occurrence of errors and changes are comparatively less likely to occur in the tuples.

Frequently Asked Questions (FAQs)

Q1. What we call tuples as immutable?

A1. We can call tuples immutable tuples as we cannot make changes in a tuple once we create it.

Q2. What is the difference between a list and a tuple?

A2. The basic difference between a list and a tuple is that lists are mutable in nature whereas, tuples are immutable.

Q3. How can we access elements in a tuple?

A3. We access elements in a tuple through indexing and slicing.

Q4. State true or false:

We can create a tuple with a single element as follows:

>>>tup = (30)

A4. False, this will treat the element as an integer value.

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

Leave a Reply

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

Download the App

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

Customize your course in 30 seconds

No thanks.