Untitled
unknown
plain_text
2 years ago
799 B
5
Indexable
# Define a tuple
tup = (1, 2, 3, 4, 5)
# Reverse the tuple using slicing
reversed_tup = tup[::-1]
# Print the reversed tuple
print(reversed_tup)
def swap_tuples(tup1, tup2):
# Check if both tuples have the same length
if len(tup1) != len(tup2):
return "Error: Tuples must have the same length."
# Swap the tuples using a list comprehension
swapped_tuples = [(tup2[i], tup1[i]) for i in range(len(tup1))]
# Unpack the swapped tuples into two separate tuples
swapped_tup1, swapped_tup2 = zip(*swapped_tuples)
return swapped_tup1, swapped_tup2
# Test the function
tup1 = (1, 2, 3)
tup2 = (4, 5, 6)
swapped_tup1, swapped_tup2 = swap_tuples(tup1, tup2)
print("Original tuples:", tup1, tup2)
print("Swapped tuples:", swapped_tup1, swapped_tup2)Editor is loading...
Leave a Comment