Python Tutorial

Thursday, August 26, 2010

Python list

'''
   This Code use python list syntax
'''

myList=list()

myList.append(1)
myList.append(5)
myList.append(1)
myList.append(2)
myList.append(3)

'''
   Access list data
'''
for i in range(0,len(myList)):
    print myList[i]

'''
   Counting occurance of a data/object
'''
print "myList.count(1) ",myList.count(1)

'''
   Remove a list object
   - remove only first occured value
'''
myList.remove(1)
print myList

myList.append(1)

'''
   Reverse objects of list
'''
print myList
myList.reverse()
print myList

'''
   Find a data/object to list
   - return the lowest index of object
   - raise exception if data not found
'''
print "myList.index(5): ",myList.index(5)

'''
   Sort objects of list
'''

myList.sort()
print myList

'''
   Insert a data/object into list
'''
myList.insert(2,100)
print myList






Output:
1
5
1
2
3
myList.count(1)  2
[5, 1, 2, 3]
[5, 1, 2, 3, 1]
[1, 3, 2, 1, 5]
myList.index(5):  4
[1, 1, 2, 3, 5]
[1, 1, 100, 2, 3, 5]

0 comments:

Post a Comment