object type help(object) eg. To get help for abs function >>>help(abs) • dir(object) is like help() but just gives a quick list of the defined symbols >>>dir(sys)
0 • Float Division >>>10.0 / 17 0.588235 >>>flot(10) / 17 0.588235 • The first division is an integer division • To avoid integer division, at least one number should be float
by “ , ” constitute a list >>>list = [1, 2, 3, 4, 5, 6] • Items need not to have the same type • Like indexable arrays • Extended at right end • List are mutable (i.e. will change or can be changed) • Example >>>myList = [631, “python”, [331, ”computer” ]]
by ”, ” constitute a list >>>tup = (1, 2, 3, 4, 5, 6) • Nesting is Possible • Outer Parentheses are optional • tuples are immutable (i.e. will never change cannot be changed) • Example >>>myTuple = (631, “python”, [ 331 , ”computer” ])
Can usually just use %s for everything, it will convert the object to its String representation. • eg. >>> "One, %d, three" % 2 'One, 2, three' >>> "%d, two, %s" % (1,3) '1, two, 3' >>> "%s two %s" % (1, 'three') '1 two three'
the same amount to be part of the scope (or indented more if part of an inner scope) • forces the programmer to use proper indentation • indenting is part of the program!
the same amount to be part of the scope (or indented more if part of an inner scope) • forces the programmer to use proper indentation • indenting is part of the program!
: Statements >>> x = 0 >>> while x <= 6 : x = x + 1 print x else : y = x >>> print y The optional else clause runs only if the loop exits normally (not by break)
>>> var = raw_input(“Enter your name & BDay”) • The raw_input(string) method returns a line of user input as a string • The parameter is used as a prompt
in a file named file_name.py. Ex. support.py >>> def print_func( par ): print "Hello : ", par return The import Statement: import module1[, module2[,... moduleN] Ex: >>>import support >>>support.print_func(“world!”);
• We write a doc-string along with the function definition >>> def avg(a, b): … """ avg takes two numbers as input and returns their average""" … return (a + b)/2 >>>help(avg)
radius Function needs to return two values >>>def circle(r): … pi = 3.14 … area = pi * r * r … perimeter = 2 * pi * r … return area, perimeter >>>a, p = circle(6) >>>print a
name and second parameter-"r" is for reading, the "w" for writing and the "a" for appending. eg. >>>fh = open("filename_here", "r") • Closing a file used when the program doesn't need it more. >>>fh.close()
line from the file >>>fh = open("filename", "r") >>>content = fh.readline() • The readlines function returns a list containing all the lines of data in the file >>>fh = open("filename", "r") >>>content = fh.readlines()
the class. The attributes are data members (class variables and instance variables) and methods Code: class Employee: empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount
instances of this class. This can be accessed as Employee.empCount from inside the class or outside the class. • first method __init__() is called class constructor or initialization method that Python calls when a new instance of this class is created. • You declare other class methods like normal functions with the exception that the first argument to each method is self.