Just Tech Me At
March 25, 2023
The tuple is an essential data structure that is commonly used in Python programming. Similar to lists, tuples allow you to store collections of objects in an ordered and indexed manner. Elements in a tuple can be duplicative which is also a property of lists. However, unlike lists, tuples are immutable, meaning that their values cannot be modified once they are created. They are ideal for cases where the stored value should not be modified. In this article, tuple creation and element access will be demonstrated.
Much like the set data structure, you can create a tuple in two ways. You can:
The example below uses the first option of manually specifying the elements of the set and enclosing them in parenthesis.
myTuple = ( 'Florida',
'Illinois',
'Pennsylvania',
'Ohio',
'Georgia',
'North Carolina',
'Michigan',
'Virginia',
'Washington',
'Massachusetts',
'Arizona',
'Indiana',
'Tennessee',
'Missouri',
'Maryland',
'Wisconsin',
'Minnesota',
'Colorado',
'Alabama',
'South Carolina',
'Louisiana',
'Kentucky',
'Oregon',
'Oklahoma',
'Connecticut',
'Iowa',
'Arkansas',
'Mississippi',
'Utah',
'Kansas',
'Nevada',
'New Mexico',
'Nebraska',
'West Virginia',
'Idaho',
'Hawaii',
'Maine',
'New Hampshire',
'Rhode Island',
'Montana',
'Delaware',
'South Dakota',
'North Dakota',
'Alaska',
'Vermont',
'Wyoming',
'New Jersey',
'New York',
'California',
'Texas')
Like lists, tuples are accessed by their index. For tuples (and lists), each element is assigned a number indicating its position. We call this position an "index." The first item of the tuple is indexed as zero, the second is indexed as one, the third is indexed as two, and so on. In our example, Florida is indexed as 0, the first item of the tuple. Texas is indexed as 49, the last item of the tuple. In order to print Texas (or the last item in the tuple), the following syntax is used:
print(myTuple[49])
If, however, there was an attempt to call a function on an item indexed at position 50, an error would be returned as there are only 50 items in the tuple and the last item is indexed at 49.
print(myTuple[50] #should return an error
Because tuples are immutable (they cannot be changed once created), methods for adding, updating, and deleting elements are all unavailable.
Tuples are an important data structure. They provide a lightweight and efficient way to store collections of objects. While they are very similar to lists, their immutability is a distinguishing factor. Immutability can be a useful property. If you need to store a collection of values with the assurance that they will not be changed, tuples are a viable programming option. Needless to say, tuples are an important structure and understanding them will likely be beneficial to you at some point in your Python Developer career.
Visit Educative.io
Polish Your Python Skills. Visit Tutorials Point!
Visit DataCamp Today!
For more articles on Python data structures, see the following:
For a more advanced review of Python Sets, see the following resources: