Exercise:Python Loop Tuples Try Again YesNo Next Exercise » What is a correct syntax for looping through the items of a tuple? for x in ('apple', 'banana', 'cherry'): print(x) for x in ('apple', 'banana', 'cherry')
ExampleGet your own Python Server Convert the tuple into a list to be able to change it: x = ("apple","banana","cherry") y =list(x) y[1] ="kiwi" x =tuple(y) print(x) Try it Yourself » Add Items Since tuples are immutable, they do not have a built-inappend()method, ...
Packing a tuple: fruits = ("apple","banana","cherry") Try it Yourself » But, in Python, we are also allowed to extract the values back into variables. This is called "unpacking": Example Unpacking a tuple: fruits = ("apple","banana","cherry") ...
Print the number of items in the tuple: thistuple = ("apple","banana","cherry") print(len(thistuple)) Try it Yourself » Create Tuple With One Item To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple...
Python ReferencePython Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary Module Reference