Using range in a List

Using range in a List

Lesson Details:
September 17, 2020

Video Transcription: In the continuation of the previous lecture now we are going to see some more functions related with lists this video is brought to you by Appy Pie’s Academy, so I have created another program called lists to dot py and I'm going to take a list called countries then I'm giving the first value as India second value as America then third value as say New Zealand and the fourth value let it be say let it be Inlet and finally I am going to have another country our neighboring country Nepal a beautiful country and then I'm going to print countries within square brackets I'm going to write 1 : 4 see it's a kind of range so when I'm saying one column for what does it mean is I'm going to print all the items starting from index number 1 till index number 3 mind 8 knot index number 4 tail index number 3 so it will be 4 minus 1 so in the above statement output will be America because America is at index number one New Zealand and England because in the last England will be printed because it is at index number 3 4 - 1 3 so okay now let me execute the program go to run then run or you can right-click on the program name then click on run see America New Zealand England this is the output right America attend X number one New Zealand and then at the next number three its England that will be printed okay let's proceed further now I will say print all items in the list so the method is I would write print name of the list countries that's it I'm not mentioning any index I am simply writing the name of the list that is countries right click on the program name click on run and you will see all the items are getting printed see India America New Zealand inclined a path all the values are getting printed okay so then an alternative method is going to be discussed an alternative way to the above statement that is if we want to print all the items of a list what is an alternative method that is print and within square brackets simply write : again we are not mentioning any index we are simply writing : right click on the program name run oops there seems to be some error okay sorry I forgot to give the name of the list that is countries okay so far so good again right-click on the program name run fine and here is the output all the values are of the list are getting printed right so let's move ahead and now I am going to write print countries then 2 : that's it so in the range I have just given the starting index and I have not mentioned the ending index so what will happen is all the items starting from index number two that is the third item starting from index number two till the end they will be printed see it carefully so I'm giving the comment also in the above in the sorry in the below statement all the items from index two to last will be printed because I have not mentioned the index number of the last item starting is two so at index number two is New Zealand so all the values New Zealand England Nepal all these values will be printed so in order to see it from the program see the output yeah New Zealand England a power all these values are getting printed starting from index number two till the end okay so now I think we should proceed with another example so below all the items all the items from beginning to from beginning to index three will be printed now we won't be mentioning from beginning to index three are printed so what I would be writing is countries : for now I have mentioned the last index number which will be 4 minus 1 that is 3 and it will start from the starting and India America New Zealand and England will be printed so because England has the index number 3 okay the beginning was not mentioned so it will start from the beginning India America New Zealand England all these values have been printed started from the it started from the beginning and it went till the index number item of the index number three.

loader
Course content