w3resource

Python Exercise: Get an item of a tuple


Write a Python program to get the 4th element from the last element of a tuple.

Visual Presentation:

Python Tuple: Get an item of a tuple.

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:

Flowchart: Get an item of a tuple

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.



Follow us on Facebook and Twitter for latest update.