hello guys welcome to the seventh blog tutorial on Python programming for beginners and in this blog I'm going to show you what are lists and how to use lists in Python so as the name suggests list is a collection of values and how you can declare list in Python you just need to give the name of the list for example I want to have the list of names so I can give the variable name for list as names is equal to and then the square bracket and this square bracket will contain the collection of values and it's totally valid that this collection can you know initialize as zeros value that 0 values so the list can have 0 values also and it can have maximum values for example tens or hundreds of values so for example I initialize this list like this and this list is initialized as a blank list ok and when I call this names list once again it's a empty list because I haven't provided any value to the list ok so it's totally valid that you initialize your list empty now to initialize lists with some values what you can do is you can just write name of the list once again and then you can give the values inside your list like this so for example I want to have the collection of names comma some other name comma some other name and this can go on and on and on right so it depends upon you what a number of values you want to have in the list and when I press enter your name variable which is a list is initialized with these values now when you call this names variable once again it has these three values now there is a term called index in Python for lists which indicates the position of the values in your list so the position of this first name in the list in as any other good language starts from 0 it doesn't start from 1 but it starts from 0 and this is called the index of your list so the index of this list or the index of this name mark is 0 index of this name John is 1 and index of this name Julie is 2 and on the basis of index you can even call these values so for example I want to print out this first value called mark I can just try the name of my list names and then just give the index so the index 0 is 1 mark right and when I press ENTER it returns me the value mark in the same way when I call index is equal to 2 names - can you give me Julie because it starts from 0 1 2 right now there is a very interesting thing in Python is that you can even have index backwards also right so the forward index starts from 0 1 2 and the backward index starts from - 1 - 2 - 3 and if this has more values - 4 - 5 and so right because why this starts from minus 1 and this forward index starts from my zero because you cannot have minus zero from backwards right so this index will be minus one this index will be minus two this in legs will be minus three and you can even call these values on the basis of backward index also for example I want to print this name Julie on the basis of backward index I can just do it like this I can just give the backward index which is minus one which starts from minus 1 right backward index start from minus 1 and this prints Julie once again so always remember if you want to start from or you want to have the forward index just start from 0 1 2 and if you want to you know have the values from the backward index you can you start from minus 1 minus 2 minus 3 and so on okay for example you want to call name mark you just need to give names and the backward index minus 3 that will give mark right now in order to add values more values to your list what you can do is you can just call the variable or your list name and then you just write dot and
just write append and in this bracket you just pass the value which you want to add to your list so I want to add one more value to my name list and I press ENTER now this name which is a name of the list dot append which is a function built-in function in python has appended or added one more values to my name names list and now when I print my names list it has four values and it has added this extra name to my list okay now for example you want to add lists into a list so for example I have some other list called age for example ages for age and I have some edges here 23 comma 12 comma 32 comma for example 11 okay so I have a list of names and I have the list of age right and I assign this list and now I want to add the age to my a names list how can I add I can just write names which is the name of my name list night dot extend and in the bracket you pass your second list so you can just pass this like this and press ENTER and it's totally valid and now when you call this name list you have extra list of age here so there is an interesting fact about Python list is it can contain different data types so it's not necessary that you your list only if you initialize list by string values it only has to contain you know string values you and your list can contain string values also or the integer values or float values or any other data type values and it's totally fine in Python okay so you can define depth you know different data type in list also okay so just remember this now you must have or you could have done the same thing for example you want to add or first of all I will show you how you can remove items from a list okay so to remove our item from a list what you need to do is for example my list name is names I want to remove some items so I will just write names dot remove and in the bracket I will just give the item value for example I want to remove this name Patrick from the list okay I will just say name which is the name of my list dot remove and then the value which you want to remove and press Enter and once again I will call names and now the value Patrick is gone from the list right so in this way you can remove lists from your you know not remove list but remove values from oil from your list in Python now for example you want to print a list you can just call print method so it's you know you can call your list using this print method also so you can just write names and press ENTER and we'll give print the list of name like this only okay now in order to print multiple lists for example I have the list called names and I have initialized this list called ages also right so I can just separate my list by comma and I will just add one more list and it will print two lists from me first list is four names till here and ii list of ages still here right so you can either you use single list or multiple lists or print them like this okay now in order to get you know I have shown you how you can get the maximum value minimum value or the length of the strings in the last blog right on the list also you can call length matter Ally N and you can find the length of the list like you just call le n built-in function and just pass the list name as an argument right I will give the length of the string which is seven right one two three four five six seven right so if the list contains seven values it will return seven this is the length of the list okay now you can even print for example max if it applies to your list you can even call this method I will give an error because my list is a mixed list right but the same method Max and I applied to the list called age it will give me the answer 42 right because my list of age only contains the integer values and the maximum values of this is 32 okay so you can also apply built-in function on lists also so I think in this way you can use list in Python I hope your enjoyed this blog
Post a Comment