Why python slicing is important?
You had known about Indexing. In this blog, you are going to see the slicing in python.
The slicing in the python is the portion extract from the iterable. Iterable are string,list, tuple, set and dictionary of sequence.
These sequences use a square bracket() for slicing.
The syntax for slicing is iterable[start:end].
The start is the position where slicing start.
The end is the position where the slicing finish.
Remember, slicing is not possible in the dictionary and set because of unordered and immutable behaviour.
The start of slicing
The start of slicing is the position where the sequence starts.
You can give any number to start position. For example,
In the above example, you can see that I have used string iterable for slicing.
You can use any iterable for slicing.
In this example, you can see that I have used slicing in the python list.
Remember, The first element position in the sequence is always 0.
For example, in this case, the first element 1’s position is 0.
The end of slicing
At the end of the slicing, the sequence is finished. It’s the last position of the sequence.
In the above example, you can see that the end position is 3.
If you carefully observe then you will found that the last element is last-1. In this case, it’s ‘l’ in 2nd position.
Remember, The last position in slicing always will be last-1 because of the Mathematical formula.
Starting position not included
Suppose in slicing starting position is not included. Only the last position in slicing is included.
What do you think will happen?
In the above position, you can see that starting position is not included.
So, the sequence has started from the 0th position.
Here, you can see that the 0th position element is ‘h’ till the given 3rd position.
Remember, when starting position is not included the sequence start with the 0th position of iterable.
End position not included
When the end position is not included in slicing then what you think will happen?
It will look like [start: ].
In the above example, you can see that the last position is not included only the first position is included.
You can see that the sequence from starting position till the end has come.
Remember, when the last position is not included then from the start position till the last position sequence will come.
You can slice the python sequences using Negative indexing.
The Negative indexing starts from the last of the sequences.
In the above example, you can see that sequence starting from -4 I.e. 2nd element and ending at the last element.
In this case, The last element ‘l’ is -1 in negative indexing.
Copy the list using a slice
Suppose, you have to copy the list using a slice. The new copied list should not affect the old one. What do you think you can do?
For example, you have to create a new list of a number using the old one.
In the above example, you can see that I have created a new list of new_numbers using slicing.
Also, you can see that when I have made changes in the list new_numbers, it doesn’t impact the old one.
Python slicing is important to get some parts of iterable. the iterables are string, tuple and list.
Remember, slicing is not possible in the dictionary and set.
You can also slice using negative indexing. Slicing is a very powerful tool to create a new list.
You can also create a new list using a slice in copy. If you make any changes to the new list, the old one will not be impacted.
Have you found it useful? What do you think? What points I have missed, You think you can cover?
For more, you can visit
"Quality brings clarity"
- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021