USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Tuples and Their Usage

Lesson 14/35 | Study Time: 40 Min

Not all data is meant to be changed. When you have a collection of values that should remain fixed throughout a program — such as coordinates, dates, or configuration settings, a tuple is the right choice.

A tuple in Python is an ordered, immutable collection, meaning once it is created, its contents cannot be altered. Tuples are lightweight, fast, and ideal for protecting data integrity.

Creating a Tuple

Tuples are defined using parentheses (), with items separated by commas. Like lists, they can hold any data type.

Important: A single-item tuple must have a trailing comma. ("hello") is a string, not a tuple.

Accessing Tuple Items

Tuples use the same indexing and slicing rules as lists — zero-based positive indexing and negative indexing from the end.

Tuples are Immutable

The key characteristic of a tuple is that it cannot be modified after creation. You cannot add, remove, or change its items.


python

coordinates = (10, 20)

coordinates[0] = 50     # TypeError: 'tuple' object does not support item assignment


This immutability is intentional, it protects data that should not change during program execution.

What You Can Do With Tuples

Even though tuples are immutable, several operations are still available:


Tuple Packing and Unpacking

Packing means storing multiple values into a tuple. Unpacking means extracting those values into individual variables — a very clean and readable Python feature.


Unpacking is commonly used when a function returns multiple values.


Iterating Over a Tuple

Tuples can be looped through just like lists.





Converting Between Tuple and List

Sometimes you need to modify a tuple temporarily. You can convert it to a list, make changes, then convert it back.



Tuple vs List — When to Use Which


Common Use Cases of Tuples in Python and AI

Tuples are particularly useful in the following scenarios:


1. Storing fixed configurations — model hyperparameters, image dimensions.

2. Returning multiple values from a function — clean and readable.

3. Dictionary keys — tuples can be used as keys; lists cannot.

4. Data records — representing a row of data (name, age, score).


Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.