Snake Is The Record Keeping Officer

Table of contents

“Hanji kaise hai aap sabhi” , I hope you are imagining things while learning real stuff. Imagining things from different perspectives makes learning easy and fun. So, let's understand one more real concept of our snake (Python) with some playful imagination.
Tuples: Records Of Country’s Official Data
Imagine our snake, who went to the library in this article, studied very hard and became the Chief Records Officer in the central ministry. His job is to maintain and add new records related to the country's economy, defense, education, etc., in a well-organized file. Remember, each file contains records for only one year. Once a year is completed, the snake has to take a new file to add the records for the next year's data.
So, whatever happens in the country in a particular year will be put in that yearly file by our snake.
Imagine a year has passed, and the snake has completely filled his file with records of what happened in our country during that year. For example, let's say our GDP growth last year was 7.7%. The snake observed this and wrote it down in the file. Or, the unemployment rate increased in the past year by 50%. The snake again observed this and wrote it down in the file.
Now the question is, can our snake change those data figures and replace them with other figures as he wishes?
You guessed it right—he cannot. Why? Because it is something that really happened in the past, and we cannot change the past, nor can our snake change it. Last year, the GDP grew by 7.7%, and that's a fact. The snake cannot change this fact, especially since it will be placed in the official record file.
The story is over, now imagine that file where our snake cannot change or update the data as a Tuple in Python.
Technically, a tuple is a sequence type data structure just like lists (to learn more about lists, click on this article). However, we cannot update or change a tuple once it's created; tuples are immutable (to understand mutables and immutables, refer to this article). Think of it like a file where our snake cannot change the records from the previous year.
To create a tuple in Python, use ‘()’ and assign its reference to a custom-made variable.
Just like lists we can put any type of data inside our tuple. We can give a whole list inside our tuple or another tuple inside a tuple also knows as nested tuples. See below..
Operation On Tuples (Snake Arranges The Records)
Our snake officer successfully filled all the official data in the file for the year 2024. What can he do with that file after completing it? Let’s see through some code...
Insertion
Tuples are immutable and cannot be changed once created. Hence, we cannot insert a new data once we created a tuple directly.(Snake cannot put data once he submitted the alreary filled file to the PMO)
But, if we change our tuple to list then we can insert an element and again change back that list to a tuple. But here I am talking only about direct methods without converting one class object to another class object.
Deletion
Unlike lists, we cannot delete an element from a tuple because it is immutable. (For example, Snake cannot delete the data from the file because it is something that was true for the year 2024, and the government will make decisions for 2025 by looking at the data of 2024).
But we can delete the whole tuple using the 'del' keyword. (Here, don't imagine Snake deleting the whole file; that would be a very big mess for the policymakers.)
Traversal (Snake Revisting The Data)
Traversal means moving from one data element to another in our data structure (in this article, tuples), or we can say visiting each data element one by one.
Why do we need to do traversal? The answer is to search for specific data and retrieve it from where it is located. We will discuss searching after traversal.
Just as in lists, we can traverse the whole tuple using two methods.
Using For Loop
We can access each data element present inside the tuple using a for loop. See the code below...
Using Slicing ([start:end:step])
We can use slicing to get the data elements present inside the tuple, but it will return a tuple, not a single data element. (Technically, slicing is not used for traversal, but we can use it to know the data elements present in our tuple or list.)
We can use slicing if we want to traverse a tuple within a specific range.
Imagine our snake officer is looking for some data from the 2024 file. He knows the data should be between July and October 2024, so he will check the data for those months and ignore the rest of the file. That's what slicing does.
Searching And Accessing (Snake Looks For Specific Data Inside FIle And Got It)
We do traversal to search for the required data element. Just like in lists, in tuples we can do search operation using ‘in’ operator and ’index()’ method. Let’s discuss them..
Searching
In Operator (in)
It is used to check if the data is present inside a tuple or not. If the data is present then it will return true, else it will return false (want to know more about operators follow this)
Using index()
First, the snake checks if the data is present inside the file (tuple) or not. Once he finds the data is present, he will determine the position of that data inside the file to access it and show it to the higher-ranking officers.
The index() method takes a single attribute, i.e., the data element whose position you want to know.
Accessing
Our snake knows the position where the inflation data of the previous year is located inside the file. But he must access it to present it to the higher officials. How will he do that? Let's see.
Using Index
Just like in lists and strings we can access the data inside a tuple using the index value which starts from 0.
Using Negative Index
Just like lists and strings, if we want to access the data from the opposite side of a tuple, i.e., from right to left, we can do that using a negative index. The negative index starts from -1, meaning the last data element has an index of -1. Let’s see some code.
Sorting(Can’t Do It Directly)
As I said earlier, tuples are immutable; hence, sorting cannot be done directly on tuples. However, we can sort by converting our tuple to a list first, then applying the sort() method, and finally changing that list back to a tuple.
Official_file = ('GDP 2024', 'Unemployment Rate 2024', 'Inflation 2024', 'Budget 2024')
file_list = list(Official_file) #Changing our tuple to list first
file_list.sort() #applying sort method on the list
file_tuple = tuple(file_list) # Again changing the list to the tuple
print(file_tuple) #Output -> ('Budget 2024', 'GDP 2024', 'Inflation 2024', 'Unemployment Rate 2024')
Updating
Again, we cannot directly update data inside our tuple once we have created it. But indirectly, we can update our data inside a tuple. First, we have to convert it to a list, then use list methods for updating, and again convert that updated list back to a tuple.
Official_file = ('GDP 2024', 'Unemployment Rate 2024', 'Inflation 2024', 'Budget 2024')
file_list = list(Official_file)
file_list[0] = 'Per Capita Income' #Updating the list
file_tuple = tuple(file_list) # Changing updated list to tuple
print(file_tuple) #Output-> ('Per Capita Income', 'Unemployment Rate 2024', 'Inflation 2024', 'Budget 2024')
Unpacking (Assigning Data To Different Officers At PMO)
Imagine our officer snake submitting the file of 2024 data at the Prime Minister’s Office. There are different officers in the PMO with various areas of expertise, so our snake cannot give the whole file to a single officer. It is the duty of our officer snake to distribute the data from the file to the PMO officials according to their expertise (some officials may specialize in managing the economy, so the snake hands over the inflation and budget data to officials managing economic policies, and the unemployment rate data to someone working on policies for the youth, and so on).
What the snake is doing above is unpacking the file, i.e., the tuple.
Technically, unpacking a tuple means each value inside the tuple is assigned to differentnt variables directly. However, the number of elements inside the tuple should match the number of declared variables.
What if the number of elements inside the tuple is more than the number of declared variables?
Then use ‘*’ before any variable, and it will take the list of elements present inside the tuple as its value. Let’s see this in code...
Operators and Statistical Methods In Tuple (+, *, min(), max(), count())
Let’s take two numerical tuples to understand the concept of operators and statistical methods in tuple
Operators
Just like in lists, tuples also uses operators for merging two tuples into single one, or comparing two tuples with one another
Concatenation(+)
As we study in lists, if you use ‘+’ between two tuples, it will create a new tuple, and this new tuple will have the elements of both the previous tuples (concatenation).
Repetition(*)
If you use ‘*’ with a number, it will create a new tuple with the elements of the previous tuple, repeating them the number of times you specified.
Comparison(<, >, ==, >=, <=)
We can use comparison operators to compare two tuples with one another, just like we compare two numbers, two strings, or two lists.
Remember, Python compares sequentially. This means it first compares the elements at index 0 in each tuple. If the comparison is true between those two elements, it will return the output without checking the next index. If the comparison is false, it will check the elements at the next index, i.e., 1.
Let’s code...
Statistical Methods In Tuple
Similar to lists, tuples also have some statistical methods to compute the maximum value, minimum value, count the frequency of a particular data inside the tuple, or add all the data elements present inside a tuple. Let’s discuss them in detail with some code.
min()
It returns the minimum element present in the tuple.
max()
It returns the maximum element present in the tuple.
count()
It counts the frequency of a data element present inside the tuple. It takes the data element whose frequency we want to find as an argument.
sum()
It works only if all the data elements present inside the tuple are numeric type. It adds up all the numeric data present inside a tuple and returns the result.
Difference Between Lists And Tuples
There are mainly four points that make Lists different from Tuples. These points are:
-
Lists are mutable, meaning we can change the elements inside a list even after its creation. However, we cannot change the elements inside a tuple once it is created.
Speed
Lists are slower compared to Tuples in performing different operations.
Methods
Lists have more built-in methods that we can use as needed, whereas tuples have fewer methods compared to lists.
Syntax
Lists are initialized using square brackets ‘[]’ while Tuples are initialized using parentheses.
Conclusion
This article uses a playful imagination of a snake as a Chief Records Officer to explain Python tuples. It highlights tuples as immutable data structures, ideal for storing records that cannot be changed, similar to official yearly files. The article covers tuple operations like traversal, searching, accessing, sorting, updating, and unpacking, and explains their limits due to immutability. It compares tuples with lists, noting that lists are mutable, slower, and have more built-in methods, while tuples are faster and use parentheses for initialization.
Thanks for reading for more articles click here . I am always open for feedbacks hope you all give me some , don’t forget to share with other learners.
#ChaiCode
Subscribe to my newsletter
Read articles from Nikhil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nikhil
Nikhil
Write code and poems