print 9 + 5 print 9 - 5 print 9 / 5 print 9.0 / 5 print 9 ** 2 print 9 % 4 x = 2+3j y = 5 print x + y t = -5 print abs(t) t = 2+ 3j print abs(t) t = 2 + 3j print t.real print t.imag x = "Python primer" # Double quotes print x x = 'python primer' # Single quotes print x x = '"Go there", he said"' # Quotes of one kind protect the other print x x = """ Python primer 10 October 2015 """ # Triple quotes hold multi line strings print x # String methods x = "python primer" print x.capitalize() print x.upper() print x.lower() print x.center(50, "*") print x * 2 print "%d + %d"%(5, 4) # Old style string formatting print "{} + {}".format(5,4) # New style string formatting print x + 2 x = "python primer" print x[0] print x[1] print x[0:7] print x[0:10:2] print x[-1] print x[-7:-1] print x[-1:-7:-1] x[0] = "T" # Will error out since strings are immutable in python # Lists x = [1, 2, 3] print x y = ["IEEE", "Python", 10, 10, 2015] print y print y[0] y[0]= 'ieee' print y print y[0] print y[-1] y.append("Calicut") print y y.extend([10, 11, 12]) print y print "Kozhikode" in y print "Calicut" in y # Sets - Unique elements, unordered x = set([]) print x x = {1, 2, 3} print x # print x[0] # This will error out since sets are unordered print 5 in x print 3 in x x.add(10) print x x.add(10) print x x = {1,2} y = {2,3} print x.union(y) print x.intersection(y) print x - y # Dictionaries x = {"name" : "Noufal", "class" : "python", "place" : "Calicut"} print x["name"] print x["class"] # print x["date"] # Will not work since there is no key "date" x["strength"] = 50 print x print x.keys() print x.values() print x.items() x = "Python" for i in x: print i print "-"*10 x = [1,2,3,4] for i in x: print i for i in range(10): print "5 x %d = %d"%(i, i*5) x = 10 if x > 5: print "Yes, it's greater" else: print "Nope. It's lesser" if x > 10: print "Too large" elif x < 5: print "Too less" else: print "Just right" # While loop x = [1,2,3,4,5] while x: print x x.pop() # Functions def double(x): return x * 2 print double(10) print double("abc") print double([1,2,3]) # print double(dict(x=1, y=2)) # Will error out since you can't multiple a dictionary with a number def add(first, second): return first + second print add(5, 6) print add(5) # Fizz bizz def fizzbizz(n): for i in range(1, n+1): if i % 15 == 0: print "fizzbizz" elif i % 5 == 0: print "bizz" elif i% 3 == 0: print "fizz" else: print i fizzbizz(20) def palindrome(s): return s == s[::-1] print palindrome("malayalam") print palindrome("abba") print palindrome("Calicut") def panagram(s): for i in "abcdefghijklmnopqrstuvwxyz": if i not in s.lower(): return False return True print panagram("the quick brown fox jumps over the lazy dog") print panagram("the quick brown fox jumped over the lazy dog") def freq(s): counts = dict() for i in s: if i in counts: counts[i] += 1 else: counts[i] = 1 for i in counts: print "{} :: {}".format(i, counts[i]) freq("she sells sea shells on the sea shore!") import math class Point(object): def __init__(self, x, y): self.x = x self.y = y def dist(self): return math.sqrt(self.x **2 + self.y **2) def __repr__(self): return "Point({}, {})".format(self.x, self.y) def __add__(self, other): return Point(self.x + other.x, self.y + other.y) p0 = Point(3,4) print p0.x, p0.y print p0.dist() #print p0.quad() # I should get 1 #print Point(-2, 3).quad() # I should get 2 #print Point(-2, -3).quad() # I should get 3 #print Point(2, -3).quad() # I should get 4 print p0 p1 = Point(1, 2) print p0 + p1 class Shape(object): def area(self): raise NotImplementedError() def perimeter(self): raise NotImplementedError() class Rectangle(Shape): def __init__(self, l, b): self.length = l self.breadth = b def area(self): return self.length * self.breadth def perimeter(self): return 2*(self.length + self.breadth) class Square(Rectangle): def __init__(self, s): self.side = s super(Square, self).__init__(s, s) s = Square(5) print s.area()