Getting familiar in working with a Python tuple
A Python tuple is a collection of immutable Python heterogeneous objects, where each of the values is separated by a comma and the entire collection is enclosed between curve brackets. Just like Python lists, tuples are indexed by their positions as indices.
A Python list is mutable while a Python tuple is immutable
The major difference between a tuple and a list is that a tuple is immutable, whereas a list is mutable. This means that once a tuple is created, it cannot be changed or updated like lists.
list = [1,2,3,4]
list.append(5)
print(list)
#OUTPUT [1, 2, 3, 4, 5]
tuple = 1,2,3,4
tuple.append(5)
#OUTPUT AttributeError: 'tuple' object has no attribute 'append'
To change the elements of a tuple the variable containing a tuple needs to be reassigned. Similarly many list functions that aim to change the list and not make a new list do not work with tuples. Hence, lists are a mutable collection of objects while tuples are an immutable collection of objects.
tuple = 1,2,3,4,5
If a tuple contains a mutable object like lists, then the list in the tuple can be changed but the tuple cannot be changed.
Tuples, once created can also be integrated into Dictionaries, although lists cannot be. This is because Dictionaries allow only immutable objects, and since tuples are immutable. When the collection of objects is heterogeneous, it is advantageous to create tuples, instead of lists, since tuples are serially structured.
Concatenating Python tuples
Adding tuples
Although tuples are immutable and cannot be concatenated like lists but two tuples can be added to get a new tuple that will consist of all the objects of the tuples being added.
t1 = 'a','b','c','d'
t2 = 1,2,3,4
print(t1+t2)
#OUTPUT
('a', 'b', 'c', 'd', 1, 2, 3, 4)
Repeating the occurrence of a tuple object
Like a list, a tuple can consist of multiple occurrences of the same object. To repeat the same object in a tuple simply multiply it by the number of occurrences required.
r = ('tuple',)*5
print(r)
#OUTPUT
('tuple', 'tuple', 'tuple', 'tuple', 'tuple')
Slicing a Python tuple
Slicing literally means precisely cutting an object into smaller pieces. These smaller objects can have different purposes in an algorithm. Just like lists & strings a tuple can also be sliced by its index.
earth = 'plants', 'animals', 'birds', 'rocks', 'water', 'air'
living = earth[:3]
nonliving = earth[3:]
withlegs = earth[1:3]
visible = earth[:-1]
reverse = earth[::-1]
print(living)
#OUTPUT
('plants', 'animals', 'birds')
print(nonliving)
#OUTPUT
('rocks', 'water', 'air')
print(withlegs)
#OUTPUT
('animals', 'birds')
print(visible)
#OUTPUT
('plants', 'animals', 'birds', 'rocks', 'water')
print(reverse)
#OUTPUT
('air', 'water', 'rocks', 'birds', 'animals', 'plants')
Extracting the elements of a Python tuple to variables
Extracting all of the objects to multiple unique variables is easy in Python.
earth = 'plants', 'animals', 'birds', 'rocks', 'water', 'air'
a,b,c,d,e,f = earth
*a,b,c,d = earth
print(c,f)
#OUTPUT birds air
print(a,c,d)
#OUTPUT ['plants', 'animals', 'birds'] water air
I am an interdisciplinary educator, researcher, and technologist with over a decade of experience in applied coding, educational design, and research mentorship in fields spanning management, marketing, behavioral science, machine learning, and natural language processing. I specialize in simplifying complex topics such as sentiment analysis, adaptive assessments and data visualizatiion. My training approach emphasizes real-world application, clear interpretation of results and the integration of data mining, processing, and modeling techniques to drive informed strategies across academic and industry domains.
I am a management graduate with specialisation in Marketing and Finance. I have over 12 years' experience in research and analysis. This includes fundamental and applied research in the domains of management and social sciences. I am well versed with academic research principles. Over the years i have developed a mastery in different types of data analysis on different applications like SPSS, Amos, and NVIVO. My expertise lies in inferring the findings and creating actionable strategies based on them.
Over the past decade I have also built a profile as a researcher on Project Guru's Knowledge Tank division. I have penned over 200 articles that have earned me 400+ citations so far. My Google Scholar profile can be accessed here.
I now consult university faculty through Faculty Development Programs (FDPs) on the latest developments in the field of research. I also guide individual researchers on how they can commercialise their inventions or research findings. Other developments im actively involved in at Project Guru include strengthening the "Publish" division as a bridge between industry and academia by bringing together experienced research persons, learners, and practitioners to collaboratively work on a common goal.
Discuss