Python - Tuples
In Python, you can access elements in a tuple using indexing, just like in lists. Indexing starts from 0 for the first element. Let's see an example program:
# Creating a tuple
my_tuple = (1, 2, 3, 'a', 'b')
# Accessing elements using indexing
first_element = my_tuple[0]
third_element = my_tuple[2]
last_element = my_tuple[-1]
# Displaying the accessed elements
print("First Element:", first_element)
print("Third Element:", third_element)
print("Last Element:", last_element)
The program creates a tuple (my_tuple
) and accesses elements using positive and negative indexing. Positive indexing starts from 0 for the first element, while negative indexing starts from -1 for the last element.
Outputs:
First Element: 1 Third Element: 3 Last Element: b
Negative indexing in Python tuples allows you to access elements from the end of the tuple, counting backward. The last element has an index of -1, the second-to-last has an index of -2, and so on. Let's explore the purpose of negative indexing with an example program:
# Creating a tuple
my_tuple = (10, 20, 30, 40, 50)
# Accessing elements using negative indexing
last_element = my_tuple[-1]
second_last_element = my_tuple[-2]
# Displaying the accessed elements
print("Last Element:", last_element)
print("Second-to-Last Element:", second_last_element)
The program creates a tuple (my_tuple
) and uses negative indexing to access the last and second-to-last elements. Negative indexing simplifies the process of accessing elements from the end of the tuple.
Outputs:
Last Element: 50 Second-to-Last Element: 40
In Python, you can concatenate two tuples using the concatenation operator (+
). This operation creates a new tuple that contains the elements of both tuples. Let's see an example program:
# Creating two tuples
tuple1 = (1, 2, 3)
tuple2 = ('a', 'b', 'c')
# Concatenating two tuples
concatenated_tuple = tuple1 + tuple2
# Displaying the concatenated tuple
print("Concatenated Tuple:", concatenated_tuple)
The program creates two tuples (tuple1
and tuple2
) and concatenates them using the +
operator. The result is a new tuple (concatenated_tuple
) that contains elements from both tuples.
Outputs:
Concatenated Tuple: (1, 2, 3, 'a', 'b', 'c')
In Python, tuple packing and unpacking are concepts related to working with tuples. Tuple packing refers to creating a tuple by combining multiple values, and tuple unpacking involves extracting individual values from a tuple. Let's explore these concepts with an example program:
# Tuple packing
packed_tuple = 10, 20, 30
# Displaying the packed tuple
print("Packed Tuple:", packed_tuple)
# Tuple unpacking
a, b, c = packed_tuple
# Displaying the unpacked values
print("Unpacked Values:")
print("a =", a)
print("b =", b)
print("c =", c)
The program demonstrates tuple packing by creating a tuple (packed_tuple
) without explicitly using parentheses. It then performs tuple unpacking by assigning the values to variables (a
, b
, and c
). The result is an unpacked set of values.
Outputs:
Packed Tuple: (10, 20, 30) Unpacked Values: a = 10 b = 20 c = 30