When there is already a concept “LIST” then why and what is the need of another term “TUPLE“? And what makes it different from List? if you are a Python Beginner, then it is for sure that such questions might raised in your mind. So, let’s study about it in detail…

In real-world, we considered LIST as any playlist of songs or collection of some videos. But, as per the concept of LIST, it is changeable which means anyone can access your playlist of songs and can easily make changes in your playlist. In such scenario, TUPLE plays a huge role and that simply means it acts as a shield to protect your playlist from making any changes.

There are other examples like grouping together related data such as person’s name, their age, gender and also based on some conditions as in martial status, Job profile, etc. Therefore, there are many more depending on for what purpose you are using it…

Thus, it is the most important difference between LIST and TUPLE, where List is mutable/changeable and Tuple is immutable/unchangeable.

Tuple is represented by () parenthesis and syntactically, a tuple is a comma-separated list of values as shown:

1>>> LetsUpgrade = 'a', 'b', 'c', 'd', 'e'

In fact, it is not necessary to enclose Tuple within parenthesis but to make it identifiable and avoid confusion, we use Tuple in parenthesis (). Now, let us create a Tuple in Python programming as shown:

1

2

3

>>> LetsUpgrade = ('Batch',)

>>> type (LetsUpgrade)

<type 'tuple'>

Now, you would be wondering, why ‘,’ comma is included when there is no next element? Do one thing, just go and type the above code without comma and the result you will see is ‘str‘ which is nothing but string and therefore, we use comma to identify it as a tuple.

Also, remember Tuples are hashable which means it cannot be changed at all and creates a unique hash value to identify it. In simple words, it’s like a unique id which cannot be changed.

Alright! Now, let’s cover some important topics in the Tuple as shown in below:

  • Creating an empty tuple
  • Indexing Lists
  • Creating non-empty tuples
  • Tuples are immutable
  • Slicing & Updating in Tuples
  • Nesting of Tuples
  • Accessing Values in Tuples
  • Tuples in a loop
  • Removing a Tuple
  • Concatenation of Tuples
  • Other Basic Operations in Tuple
  • No Enclosing Delimiters
  • Built-in Tuple Functions
  • Tuple assignment
  • Dictionaries and tuples
  • Multiple assignment with dictionaries

#Creating an empty tuple:

When we need to describe an empty sequence of values, we can use an empty tuple.

1

2

3

# An empty tuple

LetsUpgrade_empty_tuple = ()

print (LetsUpgrade_empty_tuple)

Output:

 ()

#Indexing Lists:

Each item in a Tuple corresponds to an index number, which is an integer value, starting with the index number 0.

For the Tuple COURSES, the index breakdown looks like this:

‘JAVA’‘C++’‘PYTHON’‘R’‘JAVASCRIPT’
01234

The first item, the string 'Java' starts at index 0, and the Tuple ends at index 4 with the item 'Javascript'.

1

2

3

4

print (COURSES[3])

OUTPUT

R

If we call the Tuple COURSES with an index number of any that is greater than 4, it will be out of range as it will not be valid:

1

2

3

4

print(COURSES[5])

Output:

IndexError: Tuple index out of range

The above one was Positive Indexing and now let’s see Negative Indexing as shown below:

For the same Tuple COURSES, the negative index breakdown looks like this

‘JAVA’‘C++’‘PYTHON’‘R’‘JAVASCRIPT’
-5-4-3-2-1

1

2

3

4

print(COURSES[-3])

Output

Python

#Creating non-empty tuples:

1

2

3

4

5

6

7

8

9

# Creating non-empty tuples

# One way of creation

tup = 'LetsUpgrade', 'Students'

print(tup)

# Another for doing the same

tup = ('LetsUpgrade', 'Students')

print(tup)

Output

('LetsUpgrade', 'Students')

('LetsUpgrade', 'Students')

#Tuples are immutable:

1

2

3

4

#Immutable Test

LetsUpgrade = (0, 1, 2, 3)

LetsUpgrade[0] = 4

print(LetsUpgrade)

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-3ecd489d90ff> in <module>
      1 LetsUpgrade = (0, 1, 2, 3)
----> 2 LetsUpgrade[0] = 4
      3 print(LetsUpgrade)

TypeError: 'tuple' object does not support item assignment

#Slicing & Updating in Tuples:

1

2

3

4

5

# SLICING in Tuple

LetsUpgrade = (0 ,1, 2, 3, 'Hi', 6, 'Letsupgrade')

print(LetsUpgrade [2:])

print(LetsUpgrade [::-1])

print(LetsUpgrade [1:4])

Output

(2, 3, 'Hi', 6, 'Letsupgrade')

('Letsupgrade', 6, 'Hi', 3, 2, 1, 0)

(1, 2, 3)

Yes! You are right..we cannot update or make any changes in Tuple as it is Immutable.

#Nesting of Tuples:

1

2

3

4

5

# Nested Tuple creation

Batch1 = (0, 1, 2, 3)

Batch2 = ('LetsUpgrade', 'Student')

Batch3 = (Batch1 , Batch2 )

print(Batch3)

Output:

((0, 1, 2, 3), ('LetsUpgrade', 'Student'))

#Accessing Values in Tuples:

1

2

3

4

5

# Accessing Values

LetsUpgrade = ('AI', 'ML', 2020, 2021)

Batch = (1, 2, 3, 4, 5, 6, 7 )

print ("LetsUpgrade[0]:",LetsUpgrade[0])

print ("Batch[1:5]:",Batch[1:5])

Output

LetsUpgrade[0]: AI

Batch[1:5]: (2, 3, 4, 5)

#Tuples in a loop:

1

2

3

4

5

6

7

#Creating tuples in a loop

Student = ('LetsUpgrade',)

n = 4 #Number of time loop runs

for i in range(int(n)):

    Student = (Student,)

    print(Student)

Output

(('LetsUpgrade',),)

((('LetsUpgrade',),),)

(((('LetsUpgrade',),),),)

((((('LetsUpgrade',),),),),)

(part 2)

-Yogesh Raghupati (Author)