Python Exercise: Get an item of a tuple
Python tuple: Exercise-7 with Solution
Write a Python program to get the 4th element from the last element of a tuple.
Visual Presentation:
Sample Solution:
Python Code:
# Create a tuple containing a sequence of items
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
# Print the contents of the 'tuplex' tuple
print(tuplex)
# Get the 4th element of the tuple (index 3)
item = tuplex[3]
# Print the value of the 'item'
print(item)
# Get the 4th element from the end of the tuple (index -4)
item1 = tuplex[-4]
# Print the value of the 'item1'
print(item1)
Sample Output:
('w', 3, 'r', 'e', 's', 'o', 'u', 'r', 'c', 'e') e u
Flowchart:
Python Code Editor:
Previous: Write a Python program to convert a tuple to a string.
Next: Write a Python program to create the colon of a tuple.
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://198.211.115.131/python-exercises/tuple/python-tuple-exercise-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics