Python chapter 3: Tuples in Python

Dilip Kashyap
4 min readJun 26, 2022

Hello friends,

We are going to learn about Tuples in Python. This is very important part to learn, if your beginner in Python programming language.

Tuple is a collection of Python objects separated by commas. These objects assigned inside the () brackets. Tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable (can not modify) unlike lists which are mutable (can be modified).

List of topics covered

  1. Creating Tuples
  2. Tuples Concatenation
  3. Nesting of Tuples
  4. Immutable Tuples
  5. Slicing in Tuples
  6. Deleting a Tuple
  7. Getting Length of a Tuple
  8. Converting list to a Tuple

Creating Tuples

A tuple is created by placing all objects (elements) inside parentheses (), separated by commas. Brackets are optional, however, it is a good practice to use them.

A tuple can have any number of objects and can be of different types (number, float, list, string, etc.).

Example:

# Creating tuples with some values# One way of creation
tuple = 'python', 'Java'
print(tuple)
#output: ('python', 'Java')# Another for doing the same
tuple = ('python', 'Java')
print(tuple)
#output: ('python', 'Java')

Tuples Concatenation

In this method, we can join two or more different type of Tuples into one. Let’s see with example.

Example:

# concatenation of two tuplestuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'Java')
# Concatenating above two
print(tuple1 + tuple2)
#Output: (0, 1, 2, 3, 'python', 'Java')

Nesting of Tuples

In this method, we can add tuple within the tuple, that is why it called nesting of tuples. You can do more with this same concept of tuples like different data types can be added inside the tuples. Let’s see example below.

Example:

# Code for creating nested tuples# Exp 1:
tuple1 = (0, 1, 2, 3)
tuple2 = ('python', 'Java')
tuple3 = (tuple1, tuple2)
print(tuple3)
#output:
((0, 1, 2, 3), ('python', 'Java'))
# Exp 2:
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
#output:
("mouse", [8, 4, 6], (1, 2, 3))

Immutable Tuples

Immutable tuples are nothing but it means that the element assigned to the Tuple can not be changed on the given position.

Example:

#code to test that tuples are immutabletuple1 = (0, 1, 2, 3)
tuple1[0] = 4
print(tuple1)
#output:
Traceback (most recent call last):
File "e0eaddff843a8695575daec34506f126.py", line 3, in
tuple1[0]=4
TypeError: 'tuple' object does not support item assignment

Explanation: In the given example we are assigning the value 4 at the position 0, but there is already we have value so this will not the change the value once already value is available at the given position, however we can add more values at new index positions in Tuple.

Slicing in Tuples

We can access a range of items in a tuple by using the slicing operator colon :.

Example:

# code to test slicingtuple1 = (0 ,1, 2, 3)
print(tuple1[1:])
print(tuple1[::-1])
print(tuple1[2:4])
#Output:
(1, 2, 3)
(3, 2, 1, 0)
(2, 3)

Deleting a Tuple

As we know, we cannot change the elements in a tuple. It means that we cannot delete or remove items from a tuple.

Deleting a tuple entirely, however, is possible using the keyword del.

Example:

# Code for deleting a tupletuple3 = ( 0, 1)
del tuple3
print(tuple3)
#output:
Traceback (most recent call last):
File "d92694727db1dc9118a5250bf04dafbd.py", line 6, in <module>
print(tuple3)
NameError: name 'tuple3' is not defined

Getting Length of a Tuple

In this method, we can get the length of the defined Tuple. To get this we use len keyword before the Tuple. Let’s see this with example.

Example:

# Code for printing the length of a tupletuple2 = ('python', 'Java')
print(len(tuple2))
#output:
2

Converting list to a Tuple

We can also convert the any list into the Tuple. Let’s see below.

Example:

# Example for converting a list and a string into a tuplelist1 = [0, 1, 2]
print(tuple(list1))
print(tuple('python')) # string 'python'
#output:
(0, 1, 2)
('p', 'y', 't', 'h', 'o', 'n')

Advantages of Tuple over list

Since tuples are very similar to the list, both are used in similar situations. However, there are some advantages to using a tuple over the list. Listed below are some of the key benefits:

  1. We usually use tuples for different (different) types of data and a list of similar (same) data types.
  2. Since tuples are irreversible, repetition by tuple is faster than list. So there is a small improvement in performance.
  3. Tuples containing fixed elements can be used as a dictionary key. With lists, this is not possible.
  4. If you have static data, using it as a tuple will ensure that it remains secure in writing.

If you are interested to learn Google Apps Script and automate your Google Workspace ? must try this e-Book on “Google Apps Script: A Beginners Guide

I would be glad if you follow and upvote on medium platform for more such learning articles. Thanks a lot!

For any query, please drop an email at: dilipkashyap.sd@gmail.com

--

--

Dilip Kashyap

My goal is to share solutions to the problems I have encountered during software programming.