Monday, February 18, 2013

Python - Lists

List: It is mutable object (Mutants from X-Men).

On Python Terminal:


>>> l = [1, 3, 2, 4]
>>> dir(l)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> l.sort()
>>> l
[1, 2, 3, 4]   # sorted list

dir - will return a list attributes and methods present in that object.

To sort a list we can use, sort method.

Append - append a new element to list:


>>> l.append(2)
>>> l
[1, 2, 3, 4, 2]
>>> l.append(2)
>>> l
[1, 2, 3, 4, 2, 2]

Count - returns no of occurrences of an element

>>> l.count(2)
3

So, we can conclude here that duplicate elements can be present a list.

Reverse - To reverse elements in a list

>>> l
[1, 2, 3, 4, 2, 2]
>>> l.reverse()
>>> l
[2, 2, 4, 3, 2, 1]

Index - To find the position of the element in list ( Note: Indexing will start from 0)

>>> l.index(1) # Index starts from 0, So 1 positioned at 5th place in above list "l"
5
>>> l.index(4)
2
>>> l.index(2)   #Gives the index of first occurrence of element in a list.
0

Wanna find index from some other starting point, we can mention it in index method
>>> l = [2, 2, 4, 3, 2, 1]
>>> l.index(2, 1) # Find index from 2nd position (Since indexing will start from 0, here 1 is to be considered as 2nd position)
1
>>> l.index(2, 2)
4

Remove - To remove an element from a list

>>> l.remove(1)  # 1 is removed from the list
>>> l
[2, 2, 4, 3, 2]
>>> l.remove(2) # first occurred element of 2 is removed. But not all ( Remember it.... )
>>> l
[2, 4, 3, 2]

Extend - to extend the present list with other list ( Note: Append and Extend are different. See difference below.)

>>> l.extend([9,8,7])    #Extends the list "l" with new list [9,8,7]
>>> l
[2, 4, 3, 2, 9, 8, 7]

>>> l.append([9,8,7])   # Append will consider list [9,8,7] as an element and it will append it to "l"
>>> l
[2, 4, 3, 2, 9, 8, 7, [9, 8, 7]]

Pop - this function will pop out the last element in list.
>>> l
[2, 4, 3, 2, 9, 8, 7, [9, 8, 7]]
>>> l.pop()
[9, 8, 7]
>>> l
[2, 4, 3, 2, 9, 8, 7]








start with python

 Before writing any post on python I want to thank Guido van Rossum, the creator of python. 
I will start with examples.... So don't look for any definitions here. And also this blog is only for python newbies.

I am using ubuntu, So already python 2.7 is installed. Install it from http://www.python.org/

On python Terminal, type help() 

>>> help()

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

Now we will enter into help terminal. Just type keywords in help terminal like below.


help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.


and                 elif                if                  print
as                  else                import              raise
assert              except              in                  return
break               exec                is                  try
class               finally             lambda              while
continue            for                 not                 with
def                 from                or                  yield
del                 global              pass          

So we can be able to see all keywords present in python. Like this we can try topics and modules also.
We can get good python tutorial in terminal itself. Just try this...