Python Programs on Tuples
Write a python program to demonstrate various operations on tuples.
The python program demonstrates the following operations on tuples.
- Creating tuples
- Accessing elements
- Slicing
- Tuple methods
- Concatenation and repetition
- Membership testing
- Length and iteration
- Unpacking and unpacking with *
Program:
# Demonstrating various operations on tuples in Python
# 1. Creating tuples
empty_tuple = ()
single_element_tuple = (5,)
mixed_tuple = (1, "hello", 3.14, True)
print("1. Created Tuples:")
print("Empty tuple:", empty_tuple)
print("Single element tuple:", single_element_tuple)
print("Mixed tuple:", mixed_tuple)
# 2. Accessing elements
print("2. Accessing Elements:")
print("First element of mixed_tuple:", mixed_tuple[0])
print("Last element of mixed_tuple:", mixed_tuple[-1])
# 3. Slicing
numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print("3. Slicing:")
print("numbers[2:5]:", numbers[2:5])
print("numbers[:5]:", numbers[:5])
print("numbers[5:]:", numbers[5:])
print("numbers[::2]:", numbers[::2]) # Step of 2
print()
# 4. Tuple methods
sample_tuple = (1, 2, 2, 3, 4, 2, 5)
print("4. Tuple Methods:")
print("Count of 2 in sample_tuple:", sample_tuple.count(2))
print("Index of 3 in sample_tuple:", sample_tuple.index(3))
# 5. Concatenation and repetition
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
print("5. Concatenation and Repetition:")
print("Concatenated tuple:", tuple1 + tuple2)
print("Repeated tuple:", tuple1 * 3)
print()
# 6. Membership testing
print("6. Membership Testing:")
print("Is 2 in tuple1?", 2 in tuple1)
print("Is 7 in tuple1?", 7 in tuple1)
print()
# 7. Length and iteration
print("7. Length and Iteration:")
print("Length of mixed_tuple:", len(mixed_tuple))
print("Iterating through mixed_tuple:")
for item in mixed_tuple:
print(item, end=" ")
print("\n")
# 8. Unpacking
print("8. Tuple Unpacking:")
a, b, c = tuple1
print("Unpacked values:", a, b, c)
# Unpacking with *
first, *rest = numbers
print("First:", first, "Rest:", rest)
Output:
1. Created Tuples:
Empty tuple: ()
Single element tuple: (5,)
Mixed tuple: (1, 'hello', 3.14, True)
2. Accessing Elements:
First element of mixed_tuple: 1
Last element of mixed_tuple: True
3. Slicing:
numbers[2:5]: (2, 3, 4)
numbers[:5]: (0, 1, 2, 3, 4)
numbers[5:]: (5, 6, 7, 8, 9)
numbers[::2]: (0, 2, 4, 6, 8)
4. Tuple Methods:
Count of 2 in sample_tuple: 3
Index of 3 in sample_tuple: 3
5. Concatenation and Repetition:
Concatenated tuple: (1, 2, 3, 4, 5, 6)
Repeated tuple: (1, 2, 3, 1, 2, 3, 1, 2, 3)
6. Membership Testing:
Is 2 in tuple1? True
Is 7 in tuple1? False
7. Length and Iteration:
Length of mixed_tuple: 4
Iterating through mixed_tuple:
1 hello 3.14 True
8. Tuple Unpacking:
Unpacked values: 1 2 3
First: 0 Rest: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Empty tuple: ()
Single element tuple: (5,)
Mixed tuple: (1, 'hello', 3.14, True)
2. Accessing Elements:
First element of mixed_tuple: 1
Last element of mixed_tuple: True
3. Slicing:
numbers[2:5]: (2, 3, 4)
numbers[:5]: (0, 1, 2, 3, 4)
numbers[5:]: (5, 6, 7, 8, 9)
numbers[::2]: (0, 2, 4, 6, 8)
4. Tuple Methods:
Count of 2 in sample_tuple: 3
Index of 3 in sample_tuple: 3
5. Concatenation and Repetition:
Concatenated tuple: (1, 2, 3, 4, 5, 6)
Repeated tuple: (1, 2, 3, 1, 2, 3, 1, 2, 3)
6. Membership Testing:
Is 2 in tuple1? True
Is 7 in tuple1? False
7. Length and Iteration:
Length of mixed_tuple: 4
Iterating through mixed_tuple:
1 hello 3.14 True
8. Tuple Unpacking:
Unpacked values: 1 2 3
First: 0 Rest: [1, 2, 3, 4, 5, 6, 7, 8, 9]