Tuple (‘a’, ‘b’, ‘c’, ‘d’)
Similar to Lists and Dictionary, Tuple is another type of collection in Python. However, tuple has some differences as compared to lists.
– It is immutable. The value cannot be reassigned.
– It uses parenthesis ().
– General syntax: (1,2,3)
– It can use different data type within a tuple.
– It can use indexing and slicing.
– It has two methods associated with tuple, count and index.
– It can be unpacked into several variables.
Why do we want to use tuple when list has more flexibility?
According to the online tutor from the online learning website I am learning right now, tuple is not being used most of the time as compared to list. It is only when we did not want to allow the flexibility of changing the values of the objects, tuple is the choice. It depends on the application’s requirements whether immutability is required. It may provide a convenient source of data integrity.
How to declare a tuple using the parenthesis ()?
It looks quite similar with lists except it is not using square bracket, but it is using (). It allows duplicated values, just like my example below:
How use index and slicing to read the elements in the object?
It is same as lists.
Tuples have two methods associated with,
.count(‘a’) = returns count of number of ‘a’ in the tuple.
.index(‘a’) = returns the position/index of the given element. It returns error if it cannot be found.
Edited on 2019/01/27.
More interesting about tuple with list and for statement. Examples below show how to use for statement to iterate the tuple’s values and how to use list to store a number of tuples and print out the list’s values.
Tuple can do unpacking.
It is something I learned recently and I updated this blog.
How to do unpacking and packing in tuple?
There is a powerful tuple assignment feature that assigns right hand side of values into left hand side. In other way it is called unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.
See the below code for the example of pack and unpack tuple:
#Pack tuple #Tuple declaration a = ("MNNIT Allahabad", 5000, "Engineering") #Unpack tuple #This line of code unpack the values of variable, a (college, student, type_ofcollege) = a print(college) print(student) print(type_ofcollege)
Besides that, we can use the *args argument in tuple unpacking. I think I have not covered what is *args meant in my last 13 days’ entries.
This means that there can be many number of arguments in place of (*args).
All values will be assigned to every variable on left hand side and all remaining values will be assigned to *args. Let look at the sample code below to understand the line above.
# first and last will be assigned to x and z # remaining will be assigned to y x, *y, z = (10, "Geeks ", " for ", "Geeks ", 50) # print details print(x) print(y) print(z) #output of y is ['Geeks ', ' for ', 'Geeks '] # first and second will be assigned to x and y # remaining will be assigned to z x, y, *z = (10, "Geeks ", " for ", "Geeks ", 50) print(x) print(y) print(z) #output of z is [' for ', 'Geeks ', 50]
The value is assigned based on the variable on the left side and the remaining values are assigned to the placeholder *args, in the above example, they are y and z respectively.
Summary of the day:
- Characteristic of a tuple.
- Declare and use of index and slicing to read the elements in the object.
- Two methods associated with tuples, .count() and .index().
- Tuple allows index and slicing
- Tuple unpacking.