What is a Tuple?
- A Tuple is a collection of multiple values stored in a single variable.
- A tuple is written using parentheses ().
numbers = (10, 20, 30, 40, 50)
print(numbers)
Output:
(10, 20, 30, 40, 50)
Why Do We Use Tuples?
- Stores multiple values
- Allows duplicate values
- Maintains order
- Supports indexing
- Supports slicing
- Cannot be modified after creation
Creating a Tuple
- We can create a tuple using parentheses.
numbers = (1, 2, 3, 4, 5)
print(numbers)
Output:
(1, 2, 3, 4, 5)
Creating an Empty Tuple
- We can create an empty tuple using ().
numbers = ()
print(numbers)
Output:
()
Tuple with Different Data Types
- Integer
- Float
- String
- Boolean
Tuple Indexing
- Tuple elements can be accessed using indexes.
- Python uses zero-based indexing.
numbers = (10, 20, 30, 40, 50)
print(numbers[0])
Output:
10
Negative Indexing
- Python also supports negative indexes.
Value: 10 20 30 40 50
Positive: 0 1 2 3 4
Negative: -5 -4 -3 -2 -1
numbers = (10, 20, 30, 40, 50)
print(numbers[-1])
Output:
50
Tuple Slicing
- Slicing is used to get a portion of a tuple.
The syntax is:
tuple[start:stop:step]
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Output:
(20, 30, 40)
Looping Through a Tuple
- We can use a for loop to access every element.
numbers = (10, 20, 30, 40)
for number in numbers:
print(number)
Output:
10 20 30 40
Checking Whether an Element Exists
- We can use the in operator.
numbers = (10, 20, 30, 40)
print(20 in numbers)
Output:
True
Finding Tuple Length
- We can use the len() function to find the number of elements.
numbers = (10, 20, 30, 40, 50)
length = len(numbers)
print(length)
Output:
5
Tuple Methods
- Tuples have fewer methods than lists because tuples cannot be modified.
count()
index()
count() Method
- count() tells us how many times a value occurs in a tuple.
numbers = (10, 20, 20, 30, 20)
print(numbers.count(20))
Output:
3
index() Method
- index() returns the position of the first occurrence of a value.
numbers = (10, 20, 30, 40)
print(numbers.index(30))
Output:
2
Tuple Packing
- Tuple packing means putting multiple values into a tuple without explicitly using parentheses.
numbers = 10, 20, 30
print(numbers)
Output:
(10, 20, 30)
Tuple Unpacking
- Tuple unpacking means assigning tuple elements to multiple variables.
numbers = (10, 20, 30)
a, b, c = numbers
print(a)
print(b)
print(c)
Output:
10 20 30
Nested Tuples
A tuple can contain another tuple.
This is called a nested tuple.
numbers = ((1, 2), (3, 4), (5, 6))
print(numbers)
print(numbers[0])
Output:
(1, 2)
Tuple Immutability
One of the most important features of a tuple is immutability.
Immutable means that after creating a tuple, we cannot change its elements.
numbers = (10, 20, 30)
numbers[0] = 100
- This produces an error because tuple elements cannot be changed.
Converting List to Tuple
- We can convert a list into a tuple using tuple().
numbers = [10, 20, 30, 40]
numbers_tuple = tuple(numbers)
print(numbers_tuple)
Output:
(10, 20, 30, 40)
Converting Tuple to List
- We can convert a tuple into a list using list().
numbers = (10, 20, 30, 40)
numbers_list = list(numbers)
print(numbers_list)
Output:
[10, 20, 30, 40]
Tuple Concatenation
- We can combine two tuples using the + operator.
a = (1, 2, 3)
b = (4, 5, 6)
result = a + b
print(result)
Output:
(1, 2, 3, 4, 5, 6)
Tuple Repetition
- We can repeat tuple elements using the * operator.
numbers = (1, 2)
result = numbers * 3
print(result)
Output:
(1, 2, 1, 2, 1, 2)
Finding Minimum, Maximum and Sum
- For tuples containing numbers, we can use min() and max().
numbers = (10, 20, 5, 40, 30)
print(min(numbers))
print(max(numbers))
print(sum(numbers))
Output:
5 40 105
Tuple vs List
List Tuple
Uses [] Uses ()
Mutable Immutable
Can be changed Cannot be changed
Supports indexing Supports indexing
Supports slicing Supports slicing
More methods Fewer methods
Allows duplicates Allows duplicates
Good for changing data Good for fixed data
Tuple vs Set
Tuple Set
Uses () Uses {}
Ordered Unordered
Supports indexing Does not support indexing
Allows duplicates Does not allow duplicates
Immutable Mutable
Supports slicing No normal indexing/slicing
Good for fixed ordered data Good for unique data
Important Tuple Functions and Methods
Function / Method Purpose
tuple() Creates/converts to a tuple
len() Finds number of elements
count() Counts occurrences
index() Finds the first index
min() Finds minimum value
max() Finds maximum value
sum() Calculates total
Top comments (0)