Python Exercise: Reverse a tuple
Python tuple: Exercise-18 with Solution
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/tuple/python-tuple-exercise-18.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics