Ubuntu insights, Programming in groovy, java, et als!

Friday, December 02, 2011

Tutorial : List Operations in Python

#!usr/bin/python
""" All text within triple quotes is treated as comments in python """
""" This tutorial explains lists in python with the simplest operations as examples"""
""" Standard string concatenation using + operator """
""" Prints the message in a new line """
def printMessage(str):
print ">>>>> "+str
return


""" For loop : A similiar equivalent of each closure in groovy """
""" Note that indents are the only way to tell the interpreter about the blocks """
"""Also note that the below print it, prints elements in same line with space separated i.e a typical equivalent of System.out.print in java"""
def printList(aList):
for it in aList :
print it,
print
return



""" ********************** Start scripting : list operations ********************** """
list = [10, 1, 2, 3, 4, 5, 6]

"""*** Print the elements of the list *** """
list.append(9)
printMessage("Initial elements in the list : ")
printList(list)

"""*** add an element to the end of the list *** """
lastVal = 9
list.append(lastVal)
printMessage("Elements after appending a new element "+str(lastVal)+" at the end of the list : ")
printList(list)

"""*** insert element at index i *** """
insertVal = 8
indice = 7
list.insert(indice, insertVal)
printMessage("Elements after inserting : "+str(insertVal)+" at index : "+str(indice))
printList(list)

"""*** sort the elements ascending order by default *** """
list.sort()
printMessage("Elements after sort : ")
printList(list)

"""*** Reverse the elements : same as groovy *** """
list.reverse()
printMessage("Elements after reversal : ")
printList(list)

"""*** Removes the last element *** """
list.pop()
printMessage("Elements after removing last element : ")
printList(list)

"""*** Removes element at specified index *** """
index=3
list.pop(index)
"""Note the string cast below..A typical toString() equivalent in java"""
printMessage("Elements after removing element of index at : "+str(index))
printList(list)

"""*** Removes element with the value specified *** """
value=9
list.remove(value)
printMessage("Elements after removing the value : "+str(value))
printList(list)

""" *** number of times the element 1 occurs in a *** """
countFor = 1
printMessage("The number of times element : "+str(countFor)+" occurs in the list")
print list.count(countFor)

""" ****************** Some more looping and branch conditions ****************** """

""" *** Find the smallest element in the list using a typical for equivalent of eachWIthIndex groovy closure*** """
small = list[0]
smallestElementIndex = 0
for index, item in enumerate(list):
if item < small :
small = item
smallestElementIndex = index
print  "The smallest element of the list is "+str(small)+" at index "+str(smallestElementIndex)


""" *** While loop implementation *** """
printMessage("A simple while loop in python to convey : ")
sizeOfList = len(list)
i=0
while i<sizeOfList:
print "Python is fun \m/"
i = i + 1

--------------------------------------------------------------------------------------------------------------

Output for the above list operations performed :


>python -u "PythonBasicListOps.py"
>>>>> Initial elements in the list : 
10 1 2 3 4 5 6 9
>>>>> Elements after appending a new element 9 at the end of the list : 
10 1 2 3 4 5 6 9 9
>>>>> Elements after inserting : 8 at index : 7
10 1 2 3 4 5 6 8 9 9
>>>>> Elements after sort : 
1 2 3 4 5 6 8 9 9 10
>>>>> Elements after reversal : 
10 9 9 8 6 5 4 3 2 1
>>>>> Elements after removing last element : 
10 9 9 8 6 5 4 3 2
>>>>> Elements after removing element of index at : 3
10 9 9 6 5 4 3 2
>>>>> Elements after removing the value : 9
10 9 6 5 4 3 2
>>>>> The number of times element : 1 occurs in the list
0
The smallest element of the list is 2 at index 6
>>>>> A simple while loop in python to convey : 
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
Python is fun \m/
>Exit code: 0






0 comments:

Post a Comment