Nearby lessons
25 of 108Python - 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
- Insertion order is preserved.
- Duplicate values are allowed.
- Heterogeneous values are allowed.
- Immutable.
- Elements are enclosed within parentheses
( ).
Creating a Tuple
Tuple elements are enclosed within parentheses ( ).
Example - Creating Tuple
main.py
(10, 20, 30, 40)
Tuple Allows Duplicate Values
Tuples allow duplicate values.
Example - Duplicate Values
main.py
(10, 20, 10, 30)
Tuple Allows Heterogeneous Values
A tuple can store different types of values.
Example - Heterogeneous Values
main.py
(10, 10.5, 'durga', True)
Accessing Tuple Elements
Tuple supports both positive and negative indexing.
Example - Indexing
main.py
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
(20, 30)
Tuple is Immutable
Once a tuple is created, its values cannot be modified.
Example - Invalid Modification
main.py
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
AttributeError: 'tuple' object has no attribute 'append'
remove() Method is Not Supported
Tuples do not support the remove() method.
Example - remove()
main.py
AttributeError: 'tuple' object has no attribute 'remove'
Iterating Tuple Elements
We can iterate through tuple elements using a loop.
Example - Iteration
main.py
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
3
Tuple Repetition
We can repeat tuple values using the * operator.
Example - Tuple Repetition
main.py
(10, 20, 10, 20)
Nested Tuple
A tuple can contain another tuple.
Example - Nested Tuple
main.py
(10, 20, (30, 40)) (30, 40)
Membership Operators
We can check whether an element exists in a tuple using:
innot in
Example - Membership Operators
main.py
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
[100, 20]
Example - Tuple
main.py
TypeError
Advantages of Tuple
- More secure than a list because data cannot be changed.
- Faster than a list.
- Useful for storing fixed data.
- 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()andremove().
Quick Summary
| Feature | Description |
|---|---|
| Data Type | tuple |
| Mutable | No |
| Duplicates Allowed | Yes |
| Heterogeneous Values | Yes |
| Insertion Order | Preserved |
| Growable | No |
| Symbol | ( ) |