Tuesday, March 6, 2012

Slicing


In this post I am going to discuss about an interesting topic, Slicing in Python.

Slicing means extracting out elements from a Sequence or List

Pay a close attention in this Topic as it will be used many a times in future posts.

So lets see how do we Slice.

As usual open up the Python IDLE, and lets make a Sequence


Syntax:
variablename[x:y] , where x is the starting index number and y is the ending index number.

>>>
>>> python=[0,1,2,3,4,5,6,7,8,9]
>>> python[0:4]
[0, 1, 2, 3]
>>>

okay we see here that variable python is assigned number 0 through 9

To extract elements we need to instruct the python the starting point(x) and ending point (y), as we can see here after creating the sequence, we instructed the python that, start from index 0, and go till index 4.

Python is little funny here, it displays till one less than the ending point, so ending point index 4 means actually go till index 3, and when we typed python[0:4]

Python did exactly what we wanted, it started from index 0 and went till index 3 and displayed the elements 0,1,2,3

Lets say we want the elements from 5 to 8

we need to instruct the python the starting point and the ending point

So index of 5 is 5 and then ending index number would be one more than the actual result we want so ending would be 9, to display till 8.

>>> python[5:9]
[5, 6, 7, 8]
>>>

So what if we want we want to display till 9?? we dont have a 10th number here, no problem, index 10 is not assigned in the list, and index 0 through 9 is defined with an element. so to display the elements from 5 to 9, we will have to type one more than the existing index number of the last element. so in this case it would be 10.

>>> python[5:10]
[5, 6, 7, 8, 9]
>>>

Or there is one more to get the same result, to display all the elements till end.

>>> python[5:]
[5, 6, 7, 8, 9]
>>>

See here we have not mentioned anything about the ending index, so Python assumes that user wanted me to display all elements from the starting point defined till end.

There is a shortcut to display all elements in a sequence.

>>> python[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

The first and last index number not defined, so Python assumes that user wanted me to display all the elements in the sequence.
We can only use this when we want to display all elements in a sequence.

This was how Python calculated from left to right, as i mentioned in the last post, Python also knows to count backwards. Check this out.

>>> python[-5:-1]
[5, 6, 7, 8]
>>>

And we want to result to be displayed till the last element, we need to make a null index.

>>> python[-5:]
[5, 6, 7, 8, 9]
>>>

And if we want to display elements from the starting element to a specified index, counting backwards, check this out, need to make the starting index null.

>>> python[:-4]
[0, 1, 2, 3, 4, 5]
>>>


So i hope This much was clear for you, next there is one more way, suppose we want the python, to display the elements in increment of z

Syntax:
variablename[x:y:z], z is the increment number

>>> python[0:10:2]
[0, 2, 4, 6, 8]
>>> python[1:10:2]
[1, 3, 5, 7, 9]
>>>

Okay we have seen now how increment by z works, in this case increment was by 2, lest see how decrement works, in case of counting backward.

>>> python[:-1:--2]
[0, 2, 4, 6, 8]
>>>
>>> python[::--2]
[0, 2, 4, 6, 8]
>>>
>>> python[-8:-1:--2]
[2, 4, 6, 8]
>>> python[-8:-1:--3]
[2, 5, 8]
>>>

The same works for strings, here are few examples.

>>> house=['mom','dad','bro','sis','dog']
>>> house[0:4]
['mom', 'dad', 'bro', 'sis']
>>> house[:]
['mom', 'dad', 'bro', 'sis', 'dog']
>>> house[-4:-1]
['dad', 'bro', 'sis']
>>> house[-4:]
['dad', 'bro', 'sis', 'dog']
>>> house[::-2]
['dog', 'bro', 'mom']
>>> house[-4::--2]
['dad', 'sis']
>>>

Here is one more example with random numbers:

>>> test=[4,2,6,8,1,3]
>>> test[1:4]
[2, 6, 8]
>>> test[:6:2]
[4, 6, 1]
>>> test[:6]
[4, 2, 6, 8, 1, 3]
>>>

Ok So i Hope This was Informative For You, and Hope You have somewhat enjoyed the post. Practise This, Play around with this.

Here is a task for you:

1. check=[5,2,7,8,3,4,46,23,11,0]
    a: Display number from 8 to 0
    b: Display numbers in every two counts, forward and backward

2. Play=['Python','easy','hard', 'interesting', 'practice']
    a: Display from hard to practice
    b: Display Python to Practice in backward method

If you can make this, post it in the comment box below.

Thank You, and Don't Forget to Check my next post.

3 comments:

  1. 1- =================================

    >>> sheck=[5,2,7,8,3,4,46,23,11,0]

    >>> sheck[3:]
    [8, 3, 4, 46, 23, 11, 0]

    >>> sheck[::2]
    [5, 7, 3, 46, 11]

    >>> sheck[::-2]
    [0, 23, 4, 8, 2]

    2- =================================

    >>> Play=['Python','Easey','Hard','Interesting','Practice']

    >>> Play[2:]
    ['Hard', 'Interesting', 'Practice']

    >>> Play[-5:]
    ['Python', 'Easey', 'Hard', 'Interesting', 'Practice']


    ======================


    Thanks Brother for Tuts
    :)

    ReplyDelete