PYTHON BEGINNER'S GUIDE | TUPLES IN PYTHON

 In my last blogpost we learnt about our first container in Python, Lists. In this article, we will learn about another container, TUPLES.



TUPLE:

 A tuple is similar to list, the only difference is that the values in list are mutable i.e. changeable, but the values in tuple are immutable.

Elements in tuple are seperated by commas EVEN IF IT ONLY CONTAINS ONE ELEMENT. PARENTHESIS () are used to assign values, instead of SQUARE BRACKETS [] (which are used for lists.

Like I said earlier in my last blog,.PAY CLOSE ATTENTION TO WHICH BRACKETS YOU USE. Each set of brackets denotes a different container in Python. Like lists use SQUARE BRACKETS [] and Tuples use PARENTHESIS (). (Don't complain later on, that you cannot change the values in your lists, just because you used parenthesis instead of square brackets. Oh and by the way this is not a list, LOL, Get it? Because it is in parenthes.... OK Never mind)

An example of a Tuple in Python is shown below:





 So if you want to print a specific value from the tuple you can do so in the following way:

                                 

(Oh...and one more thing SQUARE BRACKETS[] ARE STILL USED TO DENOTE AN INDEX VALUE, EVEN IN TUPLES)

And the output will be:

                                  

FUNCTIONS:

Just like Lists Tuple has its own functions. But it has only two functions:

count():

This function counts the number of repetitions of a specified value in a tuple. For example:

                                

The output will be:

                                


index():

This function gives the index number of a specified value in a tuple.

Note: It gives the index of the first occurrence of the specified value in a tuple if there are multiple occurrences of the value. 

For example:

                               
                 


The output will be:


                             



CONGRATULATIONS !!! You have learnt about your second container in Python, Tuple. I know this article is really short, but trust me this is all you need to know about Tuple, FOR NOW. Also, soon we will be done with all this b******t and get into some basic programming. I know you guys are eager to do some real programming, but bear with me because these basics are really important and without them you are not going anywhere.


If you are a beginner, intermediate, advanced or just someone interested in programming, feel free to join our telegram channel and be among people like you:


And do you know the best part? Joining it is FREE !!!

So go ahead click on the link and I will see you there. 

HOPE YOU HAVE AN AWESOME DAY AHEAD !!! 



Post a Comment

0 Comments