Tuples – Python

Hello Techies!!!

Hope you guys are doing great. Waking up everyday to learn and apply something new is so exciting.

In last tutorial, we learned about a way to store data using a collection called as List.

Today, we will look into something called a tuple. Tuple is similar to list storage. It is a collection which is ordered, unchangeable (immutable) and also allows to store duplicate data.

Tuples are represented with parenthesis or round brackets ( ).

Let’s play around with tuples by writing some python code and use it’s built-in functions to do bunch of stuff.

Let’s take an example of a mixed data type tuple :

mixed_tuple = (“red”, 1, 12.4, “yellow”, “white”)

DEALING WITH TUPLE :

ACCESS, CHANGE AND ITERATE ELEMENT FROM TUPLE:

tuple-1.JPG

If you look at the file on the left half of pic which shows python code.

It explains two ways to define a tuple. First you can directly define it using round brackets and passing values into it and other one is to use a constructor to define a tuple.

If you don’t know what constructor is, don’t worry !! , we will learn about that when we start learning object oriented programming using python.

Next is the way to access element of a tuple, which is same as list.

Next describes two ways to loop through the tuple, but since you can clearly see how easy it is to use for loops when it comes to iterating through collections. ( This was mentioned when we learned about loops ).

First, It is for loop, we defined a variable which will take the values of elements as we go through elements of tuple one by one and print that on console using print statement.

Second, It is while loop, first we defined a counter variable, then we defined a condition such that counter will not exceed the length of the tuple, then print element at that counter variable index, DON’T FORGET TO INCREMENT THE COUNTER VARIABLE OTHERWISE THE LOOP WILL KEEP ON RUNNING.

WHY WILL THE LOOP KEEP ON RUNNING?

This will happen because the condition (i< length of tuple) will always remain true, since i=0 always and never changing in the process.

Last, we tried to change an element in the tuple but failed. As, you must have read above this is the property of tuple that we can’t change elements.

Tuple is immutable ( unchangeable )

LENGTH OF TUPLE, CHECKING IF ITEM EXISTS IN TUPLE, ADDING, DELETING AN ELEMENT :

tuple-2.JPG

Let’s understand what is going on in above code step by step.

We defined a tuple named mixed_tuple. What does it tell us? It tells us in python, we can define a tuple with different data types together (integer, float, string types are present here). It also tells us that we can have duplicate values in a tuple (“yellow” occurs twice)

We can check if certain element exists exactly like we checked in lists.

There are some built-in functions with special abilities, just like yellow sun gives superman his abilities, python gives tuple some abilities of its own 🙂

index() function can tell you first occurring index of any element that exists in the tuple. If it does not exists, then it will throw an error.

count() function tells us number of times a certain element has occurred in the tuple.

We used del keyword to delete the whole tuple. We can’t delete a specific element as we know tuples are unchangeable. Same is the reason for unable to add any new element at specific index of tuple.

WHERE TO USE TUPLE?

We can use or define when we know nobody should be allowed to change or add the elements and we should have easy access to elements.

P.S. properties of the collections are the key to decide when to use which collection.

This is all you need to know to get start with tuples. Don’t forget to experiment with this collection yourself by making different tuples and using it in your code.

See you guys in next tutorial. Have a great day !!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.