Tuesday, March 13, 2012

Sorts and Tuples

Hi, In This Post we will discuss something known as Sort and Tuples.

Sort is actually a method, to display the elements of a List from smaller to bigger, lets get it clear with an example.

Open up the IDLE, and create a List.

>>> python=[12,13,15,17,18,25,5]
>>> python
[12,13,15,17,18,25,5]

Okay we have created a List named python, now how do we use the "sort" method, the syntax is still the same for using methods.

list_name.method(), sort does not take any argument.

>>> python.sort()
>>> python
[5, 12, 13, 15, 17, 18, 25]
>>>

Ok so we see here, that the elements are nicely arranged from ascending to descending.

The method "sort" is also same for a String type Sequence.

>>> cobra=list("cobrasnake")
>>> cobra
['c', 'o', 'b', 'r', 'a', 's', 'n', 'a', 'k', 'e']
>>> cobra.sort()
>>> cobra
['a', 'a', 'b', 'c', 'e', 'k', 'n', 'o', 'r', 's']
>>>

The list "cobra" initially had elements 'c', 'o', 'b', 'r', 'a', 's', 'n', 'a', 'k', 'e', and after sorting the list is arranged in order from a-->z

Now here is a small thing to notice, when we use an element which is in capital letter or upper-case, while sorting, the upper case is sorted first, then the lower case is sorted. Check this example.

>>> python=list("PythoNiSasNakE")
>>> python
['P', 'y', 't', 'h', 'o', 'N', 'i', 'S', 'a', 's', 'N', 'a', 'k', 'E']
>>> python.sort()
>>> python
['E', 'N', 'N', 'P', 'S', 'a', 'a', 'h', 'i', 'k', 'o', 's', 't', 'y']
>>>

So we see here that the upper case gets arranged first, then the lower case are arranged in order from A-->Z, a-->z

You must also know that there ia another method os sorting a String, known as "sorted".

>>> sorted("AnnacOnDa")
['A', 'D', 'O', 'a', 'a', 'c', 'n', 'n', 'n']
>>>
>>> ann="AnnacOnDa"
>>> sorted(ann)
['A', 'D', 'O', 'a', 'a', 'c', 'n', 'n', 'n']
>>>

Notice what I did here, You can easily understand, what I did, If you Have read the earlier posts seriously.


Next let us see something known as Tuples, Tuples are more Like Sequences, but are not flexible like Sequences, we cannot add, delete, append a Tuple. I dont know why such a dumb thing is included in Python, but since it is included, so you have to know about it.

Lets see how to create a Tuple

>>> new=(23,45,78,90)
>>> new
(23, 45, 78, 90)
>>>

Looks like Sequence isn't it? There is a difference, can you make it out??

Look at the braces, Sequences are enclosed within Square Brackets, and a Tuple is enclosed within round brackets.

And this is what is done for creating a String type Tuple

>>> snake=('Python', 'Jython', 'Anaconda', 'Cobra')
>>> snake
('Python', 'Jython', 'Anaconda', 'Cobra')

The only thing we can do with a Tuple is call an element to be displayed by referring to its index number.

>>> new1[2]
'Anaconda'
>>> new1[3]
'Cobra'
>>> new1[4]

Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    new1[4]
IndexError: tuple index out of range
>>>

Oh It gave an error, any reason?? I did it on purpose, to show you the error type which says out of index range, i asked python to return me the element in index number 4, and there is no element in index number 4, hence it gave an error.

Ok So i hope this was informative, and Practice it play with the methods, and Don't forget to check my next post.

Thank You.

0 comments:

Post a Comment