### Introduction to Python examples def nums1(): """Number Examples""" print "hello" print "0xFF = " + str(0xFF) print "0377 = " + str(0377) print print "1/2 = " + str(1/2) print "1.0/2.0 = " + str(1.0/2.0) print "float(1)/2 = " + str(float(1)/2) print print "2**100 = " + str(2**100) print print "1j**2 = " + str(1j**2) def nums2(): """Why we use str in nums2""" print "0377 = " * 0377 print 0xFF print "0xFF:" + 0xFF def strings(): """String examples""" print "hello world" print "hello" + " " + "world" x = "hello world" print "x = " + x print "x[0] = " + x[0] print "x[10] = " + x[10] print "x[-1] = " + x[-1] print "j" + x[1:11] ## From 1 up to but not including 11 print "Length of x is " + str(len(x)) if("e" in x): print "e is in the string." else: print "e is not in the string." print '"' print "Usage\n\t-f filename\n\t-o output" print """Usage -f filename -o output""" def lists(): """List Examples""" a = [99, "Bottles of Beer", ["on", "the", "wall"]] print "a = " + str(a) print "a[0] = " + str(a[0]) ## Must use str print "a[1] = " + a[1] print "a[2] = " + str(a[2]) ## Must use str print print "a[2][1:2] = " + str(a[2][1:3]) print "a[2][1] + a[2][2] = " + a[2][1] + " " + a[2][2] print "len(a) = " + str(len(a)) print print a a[0] = 98 print a print print "a[1:2] = " + str(a[1:2]) print a a[1:2] = ["Bottles", "Of", "Beer"] print a print print a a = [99, "Bottles of Beer", ["on", "the", "wall"]] print "a[1:3] = " + str(a[1:3]) a[1:3] = ["Bottles", "Of", "Beer"] print a def dictionaries(): d = {"duck": "eend", "water": "pond"} print "d = " + str(d) print 'd["duck"] = ' + d["duck"] del d["water"] print print "d = " + str(d) d["back"] = "rug" d["duck"] = "duckling" print "d = " + str(d) print print "d.keys() = " + str(d.keys()) print "d.values() = " + str(d.values()) print "d.items() = " + str(d.items()) print "d.items()[1] = " + str(d.items()[1]) def tuples(): tup1 = ("hey", "you") tup2 = "hey", "you" a,b = tup2 print "tup1 = " + str(tup1) print "tup2 = " + str(tup2) print "a = " + a print "b = " + b class ClassVars (object): x = 5 def setx1(self, y): self.x = y def setx2(self, y): ClassVars.x = y def getx1(self) : return self.x def getx2(self) : return ClassVars.x def instanceVars(): a = ClassVars() b = ClassVars() print "a.getx1 = " + str(a.getx1()) print "a.getx2 = " + str(a.getx2()) print "b.getx1 = " + str(b.getx1()) print "b.getx2 = " + str(b.getx2()) print b.setx2(10) print "a.getx1 = " + str(a.getx1()) print "a.getx2 = " + str(a.getx2()) print "b.getx1 = " + str(b.getx1()) print "b.getx2 = " + str(b.getx2()) print a.setx1(3) print "a.getx1 = " + str(a.getx1()) print "a.getx2 = " + str(a.getx2()) print "b.getx1 = " + str(b.getx1()) print "b.getx2 = " + str(b.getx2()) def foo(x): return 1/x def bar(x): try: print foo(x) except ZeroDivisionError, message: print "Can't divide by zero:", message def files(filename): ## Be aware \n are encoded into strings f = open(filename, "r") print "filename has " +str(f.fileno()) + " lines" for line in f: print line, f.seek(0,0) lines = f.readlines() print("lines = ") + str(lines) f.close()