Tuples

Tuple Assignment

Introduction

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. Moreover, we access elements by using the index starting from zero. We can create a tuple in various ways. Here, we will study tuple assignment which is a very useful feature in python.

Tuple Assignment

In python, we can perform tuple assignment which is a quite useful feature. We can initialise or create a tuple in various ways. Besides tuple assignment is a special feature in python. We also call this feature unpacking of tuple.

The process of assigning values to a tuple is known as packing. While on the other hand, the unpacking or tuple assignment is the process that assigns the values on the right-hand side to the left-hand side variables. In unpacking, we basically extract the values of the tuple into a single variable.

Moreover, while performing tuple assignments we should keep in mind that the number of variables on the left-hand side and the number of values on the right-hand side should be equal. Or in other words, the number of variables on the left-hand side and the number of elements in the tuple should be equal. Let us look at a few examples of packing and unpacking.

tuple assignment

Tuple Packing (Creating Tuples)

We can create a tuple in various ways by using different types of elements. Since a tuple can contain all elements of the same data type as well as of mixed data types as well. Therefore, we have multiple ways of creating tuples. Let us look at few examples of creating tuples in python which we consider as packing.

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

Tuple Assignment (Unpacking)

Unpacking or tuple assignment is the process that assigns the values on the right-hand side to the left-hand side variables. In unpacking, we basically extract the values of the tuple into a single variable.

Example 1

                    

>>>(n1, n2) = (99, 7)

>>>print(n1)

99

>>>print(n2)

7

Example 2

                    

>>>tup1 = (8, 99, 90, 6.7)

>>>(roll no., english, maths, GPA) = tup1

>>>print(english)

99

>>>print(roll no.)

8

>>>print(GPA)

6.7

>>>print(maths)

90

Example 3

                    

>>> (num1, num2, num3, num4, num5) = (88, 9.8, 6.8, 1)

#this gives an error as the variables on the left are more than the number of elements in the tuple

ValueError: not enough values to unpack

(expected 5, got 4)

Frequently Asked Questions (FAQs)

Q1. State true or false:

Inserting elements in a tuple is unpacking.

A1. False

Q2. What is the other name for tuple assignment?

A2. Unpacking

Q3. In unpacking what is the important condition?

A3. The number of variables on the left-hand side and the number of elements in the tuple should be equal.

Q4. Which error displays when the above condition fails?

A4. ValueError: not enough values to unpack

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.