Using count and index functions

Using count and index functions

Lesson Details:
September 17, 2020

Video Transcription: In this lecture I am going to teach you count and index methods related with tuples this video is brought to you by Appy Pie’s Academy, okay I'm going to open a new Python file give it a name any name I'm going to give it the name suppose tuple 6 okay so let me first give the comment it's going to be like count and index functions or you can call them methods and we will see what do they do first let me take a tuple cities which is equal to New Delhi comma I'm going to give name of another city suppose Mumbai then let me give her name New Jersey and another name New Delhi so I am repeating New Delhi so we will see that count function what does it do is it will count the total number of occurrences of a particular item so how to use it I would write cities dot count then two arguments are going to be there the first thing I will do is cities dot count I'm going to print the number of counts so I would write print cities dot count New Delhi so what it will do is it will find the occurrence of the string New Delhi from the tuple cities as you can see the output is 2 we have executed the program and we have seen the output is 2 why because New Delhi appears twice in this tuple twice so that is why the count function has it turned to okay so now we will see I'm going to copy and paste it and I'm going to change the name of the city that is the value of the item now I have given the name Kurukshetra which does not happen to be present in the tuple now we will see what happens I'm going to run the program and you can see it's showing zero why because Kurukshetra spring does not appear anywhere in the tuple okay so next what we are going to do is we are going to use print and then let me write cities the name of the tuple dot index I'm going to use the index function cities dot index Mumbai now what does index function do is it will return the index number of the item that has been given as an argument so it will find Mumbai so then it will return its index so index of Mumbai is 1 let's see whether it gives the right result or not yes the result is 1 why because the index number of Mumbai is 1 okay now we will try another thing we will give the name of suppose we are going to give the name of this string New Delhi which happens to be present two times always remember in such cases what happens is it will give the index number of the very first occurrence so in this example 0 is going to be printed and we I'm going to execute the program ok run it so C 0 0 is being 0 is going to be printed because the first occurrence of New Delhi is at index number 0 so cities dot index now I am going to write crook Chaitra I have given the value of an item that does not exist so we are going to execute it yeah you can see there is an error and what is that error the error is tuple dot index X X naught into pal so here value of X's Kurukshetra is not in the tuple so right error messages being displayed so this is how count and index functions related with tuples they work.

loader
Course content