C++ map erase: Tuples are data structures in Python that allow elements of multiple data types to be surrounded in parenthesis.
For Example: (10, 30, “btechgeeks”, 40)
Printing Reverse Tuple in Python
There are many methods to reverse a tuple. Let us now see them one by one:
- Reversing the tuple using Slicing
- Reversing the tuple using reversed() Method
- Reversing the tuple using the For Loop
Method #1: Reversing the tuple using Slicing
Approach:
- Give the tuple as static input and store it in a variable
- Print the given tuple.
- Reverse the given tuple using slicing and print the result.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as static input and store it in a variable gvn_tupl = (10, 30, "btechgeeks", 40) # Print the given tuple. print("The given tuple is:\n", gvn_tupl) print() # Reverse the given tuple using slicing and print the result print("The Reversed tuple is:") print(gvn_tupl[::-1])
Output:
The given tuple is: (10, 30, 'btechgeeks', 40) The Reversed tuple is: (40, 'btechgeeks', 30, 10)
Method #2: Reversing the tuple using reversed() Method
Approach:
- Give the tuple as static input and store it in a variable
- Print the given tuple.
- Reverse the given tuple using the reversed() method and store it in another variable
- Print the Reversed tuple.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as static input and store it in a variable gvn_tupl = (10, 30, "btechgeeks", 40) # Print the given tuple. print("The given tuple is:\n", gvn_tupl) print() # Reverse the given tuple using the reversed() method and store it in another variable reverse_tupl = tuple(reversed(gvn_tupl)) # Print the Reversed tuple print("The Reversed tuple is:") print(reverse_tupl)
Output:
The given tuple is: (10, 30, 'btechgeeks', 40) The Reversed tuple is: (40, 'btechgeeks', 30, 10)
Method #3: Reversing the tuple using the For Loop
Approach:
- Give the tuple as static input and store it in a variable
- Print the given tuple.
- Take a variable and initialize it with a new empty tuple
- Iterate in the given tuple in reverse order using the for loop
- Append each element of the given tuple to the above created new tuple
- Print the Reversed tuple.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as static input and store it in a variable gvn_tupl = (10, 30, "btechgeeks", 40) # Print the given tuple. print("The given tuple is:\n", gvn_tupl) print() # Take a variable and initialize it with a new empty tuple rev_tupl = () # Iterate in the given tuple in reverse order using the for loop for itr in range(len(gvn_tupl)-1, -1, -1): # Append each element of the given tuple to the above created new tuple rev_tupl = rev_tupl + (gvn_tupl[itr],) # Print the Reversed tuple print("The Reversed tuple is:") print(rev_tupl)
Output:
The given tuple is: (10, 30, 'btechgeeks', 40) The Reversed tuple is: (40, 'btechgeeks', 30, 10)