Python Tuples

Python Tuples

kishore V


Python Tuples

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

Try it Yourself

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

Example

Try it Yourself

Tuple Characteristics

Ordered

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

Try it Yourself

Immutable

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

Try it Yourself

Allows Duplicate Values

Tuples can store repeated elements.

Try it Yourself

Tuple Length

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

Try it Yourself

Creating a Tuple with One Item

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

Try it Yourself

Without Comma (Not a Tuple)

Try it Yourself

Tuple Items and Data Types

Tuple elements can be of any data type.

Examples

Try it Yourself

Mixed Data Types in a Tuple

Try it Yourself

Checking Tuple Data Type

From Python’s perspective, tuples are objects of type tuple.

Try it Yourself

Using the tuple() Constructor

You can also create a tuple using the tuple() function.

Try it Yourself

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

Each element in a tuple has an index number.

Indexing starts from 0, meaning the first item is at position 0.

Example: Access an Item by Index

Try it Yourself

Negative Indexing

Python allows negative indexing, which means counting from the end of the tuple.

-1 → last element

-2 → second last element

Example: Access the Last Item

Try it Yourself

Accessing a Range of Items

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

Note: The start index is included, but the end index is excluded.

Example: Extract a Subset of a Tuple

Try it Yourself

Slice from the Beginning

If you omit the starting index, slicing begins from the first element.

Example

Try it Yourself

Slice Until the End

If you omit the ending index, slicing continues to the last element.

Example

Try it Yourself

Using Negative Index Ranges

Negative indexes can also be used when slicing a tuple.

Example: Slice Using Negative Indexes

Try it Yourself

Checking if an Item Exists

To check whether a value exists inside a tuple, use the in keyword.

Example: Membership Test

Try it Yourself

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)

Since tuples cannot be edited directly, the common solution is:

  1. Convert the tuple into a list

  2. Make the required change

  3. Convert it back into a tuple

Example: Change an Existing Value

Try it Yourself

Adding New Items to a Tuple

Method 1: Convert Tuple to List

This is the most flexible approach for adding one or more items.

Example: Append a New Element

Try it Yourself

Method 2: Join Two Tuples

You can also add elements by creating a new tuple and merging it with the existing one.

Note: Remember to include a comma when creating a single-item tuple.

Example: Extend a Tuple Using Concatenation

Try it Yourself

Removing Items from a Tuple

Direct removal is not allowed because tuples are immutable.

Again, the solution is to convert the tuple into a list.

Example: Remove an Item

Try it Yourself

Deleting a Tuple Entirely

If you no longer need a tuple, you can delete it completely using the del keyword.

Example: Delete the Tuple Object

Try it Yourself

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.

Packing a Tuple

Packing happens when multiple values are grouped into a tuple.

Example: Creating a Packed Tuple

Try it Yourself

Basic Tuple Unpacking

You can assign each element of a tuple to individual variables in a single line.

Note:The number of variables must match the number of tuple items.

Example: Unpack Values into Variables

Try it Yourself

Using the Asterisk (*) Operator

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

Collect Remaining Values into a List

Example: Unpacking with an Asterisk at the End

Try it Yourself

Asterisk in the Middle

You can also place the * operator in the middle of variable names.

Python will assign values until the remaining variables can be filled.

Example: Capture Middle Values

Try it Yourself

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

The simplest way to loop through a tuple is by using a for loop.

Example: Loop Through Tuple Elements

Try it Yourself

Looping Using Index Positions

Sometimes, you may need access to the index of each item.

In such cases, use range() together with len().

Example: Access Items Using Index

Try it Yourself

Iterating with a While Loop

A while loop can also be used when you want full control over the index value.

Example: Loop Using While

Try it Yourself

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

You can merge two or more tuples using the + operator.

The result is a new tuple containing elements from all tuples in order.

Example: Join Two Tuples

Try it Yourself

Joining More Than Two Tuples

You can also chain multiple tuples together.

Example

Try it Yourself

Repeating Tuple Elements

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

Example: Duplicate Tuple Values

Try it Yourself

Practical Example

Example: Generate Repeated Data

Try it Yourself

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

The count() method returns how many times a specific value appears in a tuple.

Example: Count Occurrences of a Value

Try it Yourself

index() Method

The index() method finds the first position of a specified value in a tuple.

Note: If the value does not exist, Python raises an error.

Example: Find Index of an Element

Try it Yourself

Handling Errors Safely

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

Example: Safe Index Lookup

Try it Yourself

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