Home >>Python Tutorial >Python Tuples

Python Tuples

Python List Of Tuples

We have mentioned this before in this Python Tutorial that Tuples is basically a collection that is both ordered and unchangeable. It is important for you to remember that there is a certain emphasis that is placed on the word unchangeable here. As a developer who is using this language, it is also important for you to remember that the Python tuples is written with round brackets around them. For example, if you wish to create a tuple then

thistuple = (“apple”, “banana”, “cherry”)

print (thistuple)

 

To Access Tuple Items

You should also remember that you can access a number of different tuple items by referring to the index number of the particular item. For example, if you want to return back to the item that is in the position 1 then

thistuple = (“apple”, “banana”, “cherry”)

print (thistuple [1] )

 

To Change Tuple Values

It should be a fact known to you that once you create any particular tuple value then you cannot change it. This is because of the fact that every tuple value is unchangeable. This is one of the Python tuple methods that can be illustrated with the help of the below-mentioned example

thistuple = (“apple”, “banana”, “cherry”)

thistuple [1] = “blackcurrant”

# The value will remain the same:

print (thistuple)

 

To Loop Through a Tuple

If you ever wish to loop through the various items that are mentioned in a tuple then it is important for you to remember that you can use the for method to perform that particular feature. For example

thistuple = (“apple”, “banana”, “cherry”)

for x in thistuple:

print (x)

If you wish to learn more about this for method in this programming language then we suggest that you should keep reading as we will be discussing that particular feature in our Python for Loops portion of this entire Python Tutorial.  

The Tuple Length

This is one of the Python tuple methods. If you wish to determine the total number of items that are present in a list then it is suggested that you should use the len ( ) method. For example, if you wish to print the number of items that are present in a tuple then

thistuple = (“apple”, “banana”, “cherry”)

print (len (thistuple)

 

To Add Items

It is important for you to remember that once you have created a tuple then you cannot add any other item to it. This is because of the fact that tuples once created are unchangeable. For example

thistuple = (“apple”, “banana”, “cherry”)

thistuple [3] = “orange” # This will raise an error

print (thistuple)

 

To Remove Items

We have mentioned this fact above in the Python list of tuples module that tuples once created are unchangeable. And because of that fact, you cannot remove any particular item from a tuple once it is created. However, you can delete the entire tuple with the help of the del keyword. For example

thistuple = (“apple”, “banana”, “cherry”)

del thistuple

print (thistuple) #this will raise an error because this tuple no longer exists

 

The Tuple() Constructor

It is important for you to remember that in this programming language you can also construct a tuple by using the tuple () constructor. For example

thistuple = tuple ( (“apple”, “banana”, “cherry”) ) # note the double round brackets

print (thistuple)

With this, we finish off our module of tuple Python in this Python Tutorial.


No Sidebar ads