Python Tuples
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Python Tuples

Kishore V

Python Tuples

A tuple is a built-in Python data structure used to store multiple values inside a single variable.

Python provides four main collection data types:

  • List
  • Tuple
  • Set
  • Dictionary

Each collection type has unique behavior and use cases.

What Is a Tuple?

A tuple is:

  • Ordered – items maintain their position
  • Immutable – values cannot be changed after creation
  • Allows duplicates

Tuples are created using round brackets ().

Creating a Tuple

('mango', 'orange', 'grapes')

Tuple Characteristics

Ordered

Items inside a tuple have a fixed order that never changes.

10

Immutable

Once a tuple is created, its elements cannot be modified.

# No output, but removing the # will cause a TypeError

Allows Duplicate Values

Tuples can store repeated elements.

('Paris', 'London', 'Paris', 'Tokyo')

Tuple Length

To find how many elements a tuple contains, use the len() function.

3

Creating a Tuple with One Item

A single-item tuple must include a trailing comma, otherwise Python treats it as a standard string.

<class 'tuple'> <class 'str'>

Tuple Items and Data Types

Tuple elements can be of any data type.

('John', 28, True, 5.9)

Checking Tuple Data Type

<class 'tuple'>

Using the tuple() Constructor

('car', 'bike', 'bus')

Note: Notice the use of double brackets when converting another collection into a tuple.

Accessing Tuple Elements in Python

You can retrieve values from a tuple by referring to their index position using square brackets [].

Access by Index

cat

Negative Indexing

Mars

Accessing a Range of Items

You can extract multiple items by specifying a start index and an end index.

('green', 'yellow', 'purple')

Slice from the Beginning

('Jan', 'Feb', 'Mar')

Slice Until the End

('UK', 'Canada', 'Australia')

Using Negative Index Ranges

(30, 40, 50)

Checking if an Item Exists

Python is available in the tuple.

Updating Tuples in Python

Tuples in Python are immutable, which means once they are created, you cannot directly modify, add, or remove their elements. However, Python provides safe workarounds to achieve these operations.

Modifying Tuple Elements (Workaround)

Convert the tuple into a list, make the required change, and convert it back into a tuple.

('yellow', 'green', 'blue')

Adding New Items to a Tuple

('Delhi', 'Mumbai', 'Chennai', 'Kolkata')
('Python', 'Git', 'Docker', 'Kubernetes')

Removing Items from a Tuple

('Math', 'History')

Deleting a Tuple Entirely

# Tuple object is deleted

Unpacking Tuples in Python

When you assign multiple values to a tuple, the process is called tuple packing. Python also allows you to extract those values into separate variables, which is known as tuple unpacking.

Basic Tuple Unpacking

Python JavaScript Go

Using the Asterisk (*) Operator

When the tuple contains more values than variables, use the * operator to collect extra values into a list.

red green ['blue', 'yellow', 'purple']

Asterisk in the Middle

Laptop ['Tablet', 'Phone', 'Smartwatch'] TV

Looping Through Tuples in Python

Tuples are iterable objects, which means you can loop through their elements using different looping techniques in Python.

Iterating Directly Over Tuple Values

Django Flask FastAPI

Looping Using Index Positions

Index: 0 Value: 85 Index: 1 Value: 90 Index: 2 Value: 78 Index: 3 Value: 92

Iterating with a While Loop

India Japan Germany

Choosing the Right Loop

Loop Type Best Use Case
for loop Simple iteration over values
for with index When index is required
while loop Manual control over iteration

Joining Tuples in Python

Python allows you to combine and repeat tuples using simple operators. These operations always create new tuples, because tuples are immutable.

Combining Multiple Tuples

('x', 'y', 'z', 10, 20, 30)

Joining More Than Two Tuples

('Mon', 'Tue', 'Sat', 'Sun', 'Holiday')

Repeating Tuple Elements

The * operator allows you to repeat the elements of a tuple a specified number of times.

('A', 'B', 'A', 'B', 'A', 'B')

Tuple Methods in Python

Tuples in Python are immutable, which means they have very few built-in methods. Unlike lists, tuples support only two main methods for working with their data.

Method Purpose
count() Counts occurrences of a value
index() Returns the index of the first match

count() Method

3

index() Method

1

Handling Errors Safely

Before using index(), it’s a good practice to check whether the value exists.

2
Our website uses cookies to enhance your experience. Learn More
Accept !