Nearby lessons

25 of 108

Python - Tuple Data Type

Introduction

The tuple data type is almost the same as the list data type.

The main difference is that a tuple is immutable.

This means its values cannot be changed after the tuple is created.

Features of Tuple Data Type

  1. Insertion order is preserved.
  2. Duplicate values are allowed.
  3. Heterogeneous values are allowed.
  4. Immutable.
  5. Elements are enclosed within parentheses ( ).

Creating a Tuple

Tuple elements are enclosed within parentheses ( ).

Example - Creating Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10, 20, 30, 40)

Tuple Allows Duplicate Values

Tuples allow duplicate values.

Example - Duplicate Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10, 20, 10, 30)

Tuple Allows Heterogeneous Values

A tuple can store different types of values.

Example - Heterogeneous Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10, 10.5, 'durga', True)

Accessing Tuple Elements

Tuple supports both positive and negative indexing.

Example - Indexing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
40

Tuple Index Representation

Element 10 20 30 40
Positive Index 0 1 2 3
Negative Index -4 -3 -2 -1

Tuple Slicing

Tuple supports slicing.

Syntax:

tuple[start:end]

Example - Tuple Slicing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(20, 30)

Tuple is Immutable

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

Example - Invalid Modification

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError: 'tuple' object does not support item assignment

append() Method is Not Supported

Tuples do not support the append() method because tuples are immutable.

Example - append()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
AttributeError: 'tuple' object has no attribute 'append'

remove() Method is Not Supported

Tuples do not support the remove() method.

Example - remove()

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
AttributeError: 'tuple' object has no attribute 'remove'

Iterating Tuple Elements

We can iterate through tuple elements using a loop.

Example - Iteration

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30
40

Length of Tuple

Use the len() function to find the number of elements in a tuple.

Example - len() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3

Tuple Repetition

We can repeat tuple values using the * operator.

Example - Tuple Repetition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10, 20, 10, 20)

Nested Tuple

A tuple can contain another tuple.

Example - Nested Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10, 20, (30, 40))
(30, 40)

Membership Operators

We can check whether an element exists in a tuple using:

  • in
  • not in

Example - Membership Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Important Note

A tuple is called the Read-Only Version of a List.

This is because tuple values cannot be modified after creation.

Difference Between List and Tuple

List Tuple
Mutable Immutable
Uses [ ] Uses ( )
append() supported append() not supported
remove() supported remove() not supported

Example - List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[100, 20]

Example - Tuple

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
TypeError

Advantages of Tuple

  1. More secure than a list because data cannot be changed.
  2. Faster than a list.
  3. Useful for storing fixed data.
  4. Helps maintain data integrity.

When to Use Tuple?

Use a tuple when:

  • Data should not change.
  • A read-only collection is required.
  • Better performance is needed.

Key Points

  • Tuple stores multiple values in a single object.
  • Insertion order is preserved.
  • Duplicate and heterogeneous values are allowed.
  • Tuple is immutable.
  • Tuple supports indexing, slicing, iteration, and membership operators.
  • Tuple does not support append() and remove().

Quick Summary

Feature Description
Data Type tuple
Mutable No
Duplicates Allowed Yes
Heterogeneous Values Yes
Insertion Order Preserved
Growable No
Symbol ( )

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Range Data Type