This article entitle Python Tuple is a continuation of the previous article entitled Python Lists.
Python tuples are object collections and sequences that are ordered and immutable. Tuples are similar to a list but still there are differences between them tuples can not be modified, unlike lists which can be modified and lists are using square brackets while tuples use a parenthesis.
What are Python Tuples?
In Python, tuples store multiple items into a single variable.
Tuple indices also start at 0 just like a string and they will be concatenated, sliced, and so on.
How to create a tuple Python?
To create a tuple we must place all the elements or items inside of the parenthesis and then separate by a comma.
Further, a tuple is a collection that can have multiple numbers of elements or items and can have different tuple types such as (float, integer, string, list, and so on.)
tupOne = ('glenn', 'jude', 1996, 2011); tupTwo = (10, 20, 30, 40, 50); tupThree = "x", "y", "z", "w";
Access values in tuples in Python
Accessing values in the tuple is not complex we simply use the square brackets for slicing along with an index or indexes in order to get the value in that index.
tupOne = ('glenn', 'jude', 1996, 2011); tupTwo = (10, 20, 30, 40, 50, 60, 70); print ("tupOne: ", tupOne); print ("tupTwo[1:5]: ", tupTwo[1:5]);
Updating tuples in Python
Updating the entire elements of an empty tuple is not allowed for the reason of tuples are immutable. But you are able to take some portions of existing elements to change.
tupOne = (26, 21.16); tupTwo = ('Glenn', 'Jude'); # Following action is not valid for tuples # tupOne = 200; # So let's create a new tuple as follows tupThree = tupOne + tupTwo; print (tupThree);
(26, 21.16, 'Glenn', 'Jude')
Delete tuple elements in Python
To delete an individual tuple element we need to use the (del statement).
tups = ('Glenn', 'Jude', 1996, 2012); print (tups); del tups; print ("After deleting tup : "); print (tups);
('Glenn', 'Jude', 1996, 2012) After deleting tup : Traceback (most recent call last): File "<string>", line 5, in <module> NameError: name 'tups' is not defined
Basic tuples operations in Python
Basic tuples operations respond to + and * operators similar to a string they are concatenation and repetition here too except on the results are a new tuple and not a string data type.
Further, tuples is responds to all the general sequence operations that are used on the strings.
|len((1, 2, 3))||3||Length|
|(1, 2, 3) + (4, 5, 6)||(1, 2, 3, 4, 5, 6)||Concatenation|
|(‘Hi!’,) * 4||(‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’)||Repetition|
|3 in (1, 2, 3)||True||Membership|
|for x in (1, 2, 3): print x,||1 2 3||Iteration|
Indexing, matrixes, and slicing in Python
Indexing, matrixes, and slicing can work the same way for the tuples to print because they are also sequences as they will do on the strings.
L = ('spam', 'Spam', 'SPAM!')
|L||‘SPAM!’||Offsets start at zero|
|L[-2]||‘Spam’||Negative: count from the right|
|L[1:]||[‘Spam’, ‘SPAM!’]||Slicing fetches sections|
Built-in tuple functions in Python
|cmp(tuple1, tuple2)||Compares elements of both tuples.|
|len(tuple)||Gives the total length of the tuple.|
|max(tuple)||Returns item from the tuple with max value.|
|min(tuple)||Returns item from the tuple with min value.|
|tuple(seq)||Converts a list into tuple.|
Tuple length in Python
The len() function is used to determine how many elements are there on the tuple and lists in Python.
tupleNames = ("Glenn", "Jude", "Adones", "Paul") print(len(tupleNames))
A tuple() constructor in Python
We can also use the tuple() constructor in Python to make a new set of tuples and add items.
tupleNames = ("Glenn", "Jude", "Adones", "Paul") # note the double round-brackets print(tupleNames)
('Glenn', 'Jude', 'Adones', 'Paul')
tuples items duplicate in Python
Tuples are allowed to duplicate items since they are indexed.
tupleNames = ("Glenn", "Jude", "Adones", "Paul", "Glenn") print(tupleNames)
('Glenn', 'Jude', 'Adones', 'Paul', 'Glenn')
In summary, you have read about Python Tuple. We also discussed in this article what are Python Tuples, how to create a tuple, access values in tuples, update and delete tuples, basic tuples operations, Indexing, matrixes, and slicing, built-in tuple functions, tuple length, tuple() constructor, and duplicate items in tuples.
I hope this article about Tuple in Python programming language could help you a lot to continue pursuing learning this powerful programming language.
If you want to learn more check out my previous and latest articles for more career-changing articles.