Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: Tuples and Data Types
Source: https://www.fatskills.com/python/chapter/python-programming-tuples-and-data-types

Python Programming: Tuples and Data Types

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

We have already mentioned Tuples as Python data type and some details about it. In this chapter, we are going to discuss about basic tuples operations and Python in-built methods and functions for Tuples.

Creating a Tuple
A tuple is a sequence of immutable Python objects. A tuple in Python can be declared by putting different comma-separated values between parentheses.
e.g. days = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); number = (1, 2, 3, 4, 5,6,7,8,9,0 ); chars = ("a", "b", "c", "d", "e");

Accessing elements from Tuples
Elements from the tuple can be accessed by using the square brackets for slicing along with the index to procure value available at that index of the tuple.
E.g. days [1]; number [1:6];

Updating and Adding elements in Tuples
We cannot modify any element of a tuple as tuples are immutable which means we cannot update or change the values or size of tuple elements.

Deleting an element from Tuples
It is not possible to remove an individual tuple element from a tuple as tuples are immutable. However, we can remove an entire tuple by just using the 'del' statement.
All of these tuples operations are demonstrated in the below example. First, we created the tuples. Next, we accessed the elements from the tuples. We cannot update the value of any element in Tuple.

Lastly, we used the 'del' statement to delete the entire tuple, when attempted to print the deleted tuple it throws an error as it no longer exists in memory.

Basic Tuples operations
Like Python strings, we can use + and * operators on tuples for operations like concatenation and repetition respectively.

Below are the tuples operations on Python tuples.

Python Expression

Results

Description

len((1, 2, 3, 4, 5))

5

Length operation.

(1, 2, 3, 4, 5)+ (6, 7, 8, 9, 0)

(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)

Concatenation operation.

('Hello') * 3

('Hello', 'Hello', 'Hello')

Repetition operation.

4 in (1, 2, 3, 4, 5)

TRUE

Membership operation.

for a in (1, 2, 3, 4, 5): print a,

1 2 3 4 5

Iteration operation.

 

Indexing, Slicing, and Matrixes in Tuples
Python tuples are nothing but sequences, therefore indexing and slicing work the same way for tuples as they do for strings. Therefore, below operations are possible.

In the above example, if index is negative then counting will start from right side. If it starts with 0 then counting will be from left side. Like Python String, Python tuples supports slicing and will print the sections of the tuples as per their indexes.

Built-in Tuples Methods & Functions
Below are Python built-in functions for Tuples operations.

 

 

Function

Description

cmp(tuple1, tuple2)

This Python built-in function for tuples compares elements of both tuples.

len(tuple)

This Python built-in function for tuples gives the total length of the tuple.

max(tuple)

This Python built-in function for tuples returns item from the tuple with max value.

min(tuple)

This Python built-in function for tuples returns item from the tuple with min value.

tuple(seq)

This Python built-in function for tuples converts a list into tuple.