Tuples in Python

A tuple is an immutable sequence type in Python. Tuples are similar to lists, but unlike lists, they cannot be changed after their creation. Tuples are defined by enclosing elements in parentheses ().

Key Characteristics of Tuples

  • Ordered: Tuples maintain the order of elements.
  • Immutable: Once created, the elements of a tuple cannot be changed, added, or removed.
  • Indexed: Elements in a tuple can be accessed using an index, starting from 0.
  • Allow Duplicates: Tuples can contain duplicate values.
  • Heterogeneous: Tuples can contain elements of different data types.

Creating Tuples

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
print(example_tuple)

Output:

(1, 2, 3, 'apple', 'banana', 3.14)

Accessing Tuple Elements

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
print(example_tuple[0])  # Accessing the first element
print(example_tuple[-1]) # Accessing the last elements

Output:

1
3.14

Tuple Built-in Functions

len()

Returns the length (number of elements) in a tuple.

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
print(len(example_tuple))

Output:

6

max()

Returns the largest element in a tuple.

num_tuple = (1, 2, 3, 10, 5)
print(max(num_tuple))

Output:

10

min()

Returns the smallest element in a tuple.

num_tuple = (1, 2, 3, 10, 5)
print(min(num_tuple))

Output:

1

sum()

Returns the sum of all elements in a tuple.

num_tuple = (1, 2, 3, 10, 5)
print(sum(num_tuple))

Output:

21

count()

Returns the number of times a specified element appears in a tuple.

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14, 1, 2, 1)
print(example_tuple.count(1))

Output:

3

index()

Returns the index of the first occurrence of a specified element in a tuple.

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
print(example_tuple.index('banana'))

Output:

4

Example: Tuple Packing and Unpacking

Tuples can be created by packing multiple values into a single variable. Unpacking is the process of assigning the values of a tuple to individual variables.

# Tuple packing
packed_tuple = 1, 2, 3, 'apple', 'banana'

# Tuple unpacking
a, b, c, d, e = packed_tuple

print(packed_tuple)
print(a, b, c, d, e)

Output:

(1, 2, 3, 'apple', 'banana')
1 2 3 apple banana

Modifying Tuples

Convert Tuple to List and Modify

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
example_list = list(example_tuple)
example_list[0] = 'modified'  # Modify the list
example_tuple = tuple(example_list)
print(example_tuple)

Output:

('modified', 2, 3, 'apple', 'banana', 3.14)

Concatenate Tuples

tuple1 = (1, 2, 3)
tuple2 = ('apple', 'banana')
modified_tuple = tuple1 + tuple2
print(modified_tuple)

Output:

(1, 2, 3, 'apple', 'banana')

Slicing and Reassigning

example_tuple = (1, 2, 3, 'apple', 'banana', 3.14)
modified_tuple = example_tuple[:3] + ('orange',) + example_tuple[4:]
print(modified_tuple)

Output:

(1, 2, 3, 'orange', 'banana', 3.14)
What Are People Saying About Us

Read These Life Changing Stories From People Just Like You!