Python Exercise: Reverse a tuple
Write a Python program to reverse a tuple.
Visual Presentation:
Sample Solution:
Python Code:
# Create a tuple containing a single string
x = ("w3resource")
# Reverse the characters in the string by using the 'reversed' function,
# and then convert the reversed object back into a tuple and print it.
y = reversed(x)
print(tuple(y))
# Create another tuple containing a sequence of numbers
x = (5, 10, 15, 20)
# Reverse the order of the elements in the tuple using the 'reversed' function,
# and then convert the reversed object back into a tuple and print it.
y = reversed(x)
print(tuple(y))
Sample Output:
('e', 'c', 'r', 'u', 'o', 's', 'e', 'r', '3', 'w') (20, 15, 10, 5)
Flowchart:
Python Code Editor:
Previous: Write a Python program to unzip a list of tuples into individual lists.
Next: Write a Python program to convert a list of tuples into a dictionary.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics