hello guys welcome to the eighth tutorial on Python programming for beginners and in this blog I am going to show you how to use slicing in Python so as the name suggests slicing we can use it for you know separating a slice from a list so for example I have a list like this and I will call it my list and it contains some integer values from 0 to 9 and I press ENTER and it locates this 0 to 9 values to my list and I can just see this by printing the list so it has 0 to 9 values now suppose I want to print only or I want only the values of from 4 to 7 how can I get this these values from my list so what I can do is I can just use my list and then in the square bracket I will start from the value which I want to print so for example 4 and then I give this : and then I give the ending value till of whatever value I want to print for example 8 and I press ENTER and print from 4 to just before the value you want to print so you if you remember this index of the list starts from 0 right so index is 0 here 1 2 3 4 5 6 7 8 9 right so to print your values this starting value whenever you write it includes the index of the value you want to print ok so whenever you write for it will include 0 1 2 3 4 so print the value from four that will go to eight four five six seven eight but it excludes this value and it only prints up to seven okay now suppose you want to print something like you want to print to the end from five to nine how you can do it you can just do it like my list and you can just write five and because this will print if you write it here nine this will print up to eight right because whenever we have written it here it was printing up to seven value right and remember this is the index not the values these values can be string datatype or any other data type right so this these value has nothing to do with these index values okay these are the position which you want to print okay so in order to print from five to nine you need to add one more to your slice and then it will print from five to nine okay so remember if you want to go from some value in the middle to the end just add one to the index here okay so you want to print nine then just print ten here now as I have to already told you that you not only can have the list or you can own not only can print the index slice the index for the integer array or list but any string list also so for example I have a list like this of character for example characters and then I want to print or slice these list characters right so how you can do it I will just write name my list names and then in the square bracket I will just write to 2 or 3 for example I want to print 2 to 3 it will just print second value because it skips the third value right so 0 1 2 it includes the 2 and it skips the value which you give here so it will only print to see here right for example I want to go to the end I will just write 2 2 4 that gives this like this now for example are there is a short way of doing this if you want to print from 5 to the end what you can do I have this my list here I will copy and paste it here in the square bracket what you can do you can just write 5 and semicolon and leave the other part of this slice and when you press ENTER it will just print the values from 5 to 9 to the end of your list ok now for example you do the same thing but you leave the first part and just go up to the last part it will print from start to the end ok so for example I want to print once again : or to the 5th value and it will print from the starting to the 5th value ok now this will be true for the negative sequence also so I have already told you in the last blog that you can in the list you can have the index in negative index the backward of from the back right so what you can do is you can just use this list and you can just say - 4 2 - 2 and press Enter it will give you the values from so this is minus 1 minus 2 and it will skip this value and print here and then we have minus 1 minus 2 minus 3 minus 4 and minus 4 is this one right so it will print this one it will print this one but it will not print this one because
it will skip this right as you have seen in this positive of forward forward indexing in a similar way it will this also works for the blank sequence for example - 6 - blank it will print from 4 to 9 okay it will be same for if you leave the first part and print want to print the last part ok now if you want to print the whole array you just can do it like this but you can also do it like just give a square and give this semicolon here that will also give you the whole array okay now there is one more thing you can do with your list is for example I have my list and I want to print the values for example I want to print 0 3 6 in some sequence ok so you want to skip some values in there you want to print only some other values so what you can do here you can just write 0 which is a starting index and took 10 so you want to print from 0 to the end and there is a optional value you can give here and you can give the value here which you want to skip so for example you want to skip 0 1 2 3 here so you want to print 0 3 & 6 & 9 what you can do here you can skip the value by 2 okay and when you print here it will print 0 2 4 6 8 okay so it prints here and it skips the first value and second value it prints and skips the third value and fourth value it prints and just like this right so for example I can once again skip two values at a time instead of 2 I can write 3 here and press ENTER I will give 0 3 6 9 okay and there is a very short way of doing this is you can just write the skip value number of spa index you want to skip and just leave these things or these parts blank here and will print the same value okay so this is optional it's not necessary to use this but if you use this it will skip you know those values like this okay and you can do it with the negative index also for example you can just leave this and write 2 here it will print from the backward to the front right so it when you write negative 2 it will start from 9 and then negative 2 it will print 7 and then 5 & 3 & 1 okay so if you want to print backward just give this sequence in negative okay once again you want to print backward skipping third value or three values just do it like this nine six three zero okay so in this way you can use sequence in Python there is more to sequence which well I will discuss in later blogs but that's it for sequence in Python I hope you enjoyed this blog
Post a Comment