Pacific-Design.com

    
Home Index

1. Python

2. Structures

Python / Structures /

#!/usr/bin/python
import random

class DataStructures():

    #----------------------------------------------------------------#
    # Tupple - imutable and fast sequence used for constants
    #----------------------------------------------------------------#
    def myTupple(self):
        iTupple = ("kevin", "thomas", "duraj")
        print "iTupple All =", iTupple  
        print "iTupple first two", iTupple[:2] 

        if "duraj" in iTupple: # search "duraj" in iTupple
            print "duraj is in iTupple "

        print random.choice(iTupple) # print random element

    #----------------------------------------------------------------#
    # List -  mutable with many features append, remove, sort ...
    #----------------------------------------------------------------#
    def myList(self):
        iList = ["kevin", "thomas", "shell", "duraj", "agoura"]
        print "iList All =", iList 
    
        iList.append("python");  print "append python =", iList  
        iList.remove("shell");   print "remove shell =", iList  
        del iList[0:2];          print "del first two =", iList  
        iList.sort();            print "sort list =", iList  
        iList.reverse();         print "reverse =", iList
        iList.insert(2, "2nd");  print "insert 2nd =", iList
        iList.pop(2);            print "pop second =", iList

     
    #----------------------------------------------------------------#
    # Dictionary -  based on key/value pair
    #----------------------------------------------------------------#
    def myDictionary(self):

        iDict = { 'kevin': 1978, 'thomas': 3456, 'duraj': 7532 }
        iDict['munich'] = 1988
        iDict['munich'] = 2011 # replaces previous value
        print iDict, "\n"

        for key, value in iDict.iteritems():
            print "key=", key, "value=", value

if __name__ == "__main__":
    ds = DataStructures()
    ds.myTupple()
    ds.myList()
    ds.myDictionary()