OpenGurukul : Language : Python


Published on

Python Language Tutorial by OpenGurukul

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

OpenGurukul : Language : Python

  1. 1. Python ByOpen Gurukul
  2. 2. PythonModule : Background 2
  3. 3. Python : Background : HistoryDeveloped by Guido van Rossum.The language is named after the BBC show “Monty Python’s Flying Circus” It has nothing to do with reptiles. 3
  4. 4. Python : Background : WhyAn interpreted language Save time during development as compilation and linking is not necessary. Interactive interpreter – easy to experiment with features of language.Very High Level Language High-level data types built in, such as flexible arrays and dictionaries. 4
  5. 5. Python : Background : WhyCompact & Readable Programs statement grouping is done by indentation instead of beginning and ending brackets no variable or argument declarations are necessary the high-level data types allow you to express complex operations in a single statementModular Allows you to split your program into modules. Comes with a large collection of standard modules. 5
  6. 6. Python : Background : WhyExtensible Easy to link the Python interpreter (-lpython) into an application developed in programming language such as c to perform certain tasks in python.Feature rich Some of the features found in python are available in awk, shell program, perl. 6
  7. 7. PythonModule : Interpreter 7
  8. 8. Python : Interpreter : enter and exitInterpreter is located in /usr/bin/ $ which python python /usr/bin/pythonTo start interpreter, just type $ python python on the command prompt Python 2.7 (r27:82500, Sep 16 2010, 18:03:06)The default primary python [GCC 4.5.1 20100907 (Red Hat prompt is >>> 4.5.1-3)] on linux2To come out of python Type "help", "copyright", "credits" interpreter, type quit() on or "license" for more python prompt. information. >>> quit() $ 8
  9. 9. Python : Interpreter : Interpret fileTo interpret python file $ python fileTo interpret python modules $ python -m modulePython File Extension .py : python file module.pyc : pre-compiled file (generated when we execute import module on python invoked without -O) module.pyo : pre-compiled + optimized (generated when we execute import module on python invoked with -O) 9
  10. 10. Python : Interpreter : Interpret file : ExampleProgram : Output :$ cat $ python hello.pyprint Hello World Hello Worldprint "Hello World" Hello Worldmsg="Hello World" Hello Worldprint msg $$ 10
  11. 11. Python : Interpreter : Executable Python ScriptsProgram : Program :$ cat $ cat!/usr/bin/env python #!/usr/bin/pythonprint Hello World print Hello World$ $Output : Output :$ chmod +x $ chmod +x$ ./ $ ./hello_python.pyHello World Hello World$ $ 11
  12. 12. Python : Interpreter : ModulesTo use a module in python file, Sample Output : we need to use following >>> sys.ps1 import module Traceback (most recent call last):The data in the module can be File "<stdin>", line 1, in <module> referred after import using NameError: name sys is not defined module.variable >>> import sysCovered in detail under >>> sys.ps1 Modules section. >>> >>> sys.ps2 ... >>> 12
  13. 13. Python : Interpreter : Startup FilePYTHONSTARTUP is an Use : environment variable that $ cat ~/ contains location of startup import sys file for python interepreter. sys.ps1=python > The commands in the startup file are executed before the $ first prompt is displayed in $ export interactive mode. PYTHONSTARTUP=~/.pythonrc.p yThe file ~/ is generallu used as a default $ startup file. $ python python > quit() $ 13
  14. 14. Python : Interpreter : Argument PassingProgram : Output :$ cat $ python 20 30import sys argument count : 3argc = len(sys.argv) script : args.pyprint "argument count : " + str(argc) args.pyprint "script : " + sys.argv[0] 20for arg in sys.argv: print arg 30$ $ 14
  15. 15. Python : Interpreter : OS EnvironmentEnvironment Variables Program : can be accessed $ cat within Python. import os home = os.environ.get(HOME) print home $ Output : $ python /home/surikuma $ 15
  16. 16. PythonModule : Introduction 16
  17. 17. Python : Introduction : CommentAnything that follows # is considered a comment in Python.Example :>>> print "hello world" # a comment on the same line as codehello world>>> # comment that will cause it to invoke secondary prompt... print "hello india"hello india>>> 17
  18. 18. Python : Introduction : VariableNo need to declareNeed to assign (initialize) use of uninitialized variable raises exceptionNot typed >>> age = "thirty" >>> age = 20Everything is a "variable" Even functions, classes, modules 18
  19. 19. Python : Introduction : AssignmentUsing Variables :The equal sign (=) is used to assign a value to a variableExample :>>> age = 30>>> age30>>> name = Surinder Kumar>>> nameSurinder Kumar 19>>>
  20. 20. Python : Introduction : underscore variableIn interactive mode, the last Example : printed expression is >>> a = 2 assigned to the variable _ (underscore) >>> b = 5It is a read only variable. >>> a * b 10 >>> c = 20 >>> _ + c 30 >>> 20
  21. 21. PythonModule : Numbers 21
  22. 22. Python : Numbers : Operator //>>> 8/5 # Fractions arent lost by default1.6>>> 7//3 # use // to discard fractional part2>>> 22
  23. 23. Python : Numbers : Functions>>> abs (-2)2>>> round (2.3)2.0>>> 23
  24. 24. PythonModule : Strings 24
  25. 25. Python : Strings : QuotesString can be enclosed in either >>> doesnt single quotes or double "doesnt" quotes. >>> "doesnt"Use as escape sequence as "doesnt" and when required. >>>The string is enclosed in double >>> "Yes," he said. quotes if the string contains a "Yes," he said. single quote and no double quotes, else it’s enclosed in >>> " "Yes," he said." single quotes. "Yes," he said. >>> "Isnt," she said. "Isnt," she said. >>> 25
  26. 26. Python : Strings : raw stringUse n to print a new Example : line. >>> s = a n b >>> print sUse raw-string (r) to a keep n as it is in the b string. >>> s = r a n b >>> print s a n b >>> 26
  27. 27. Python : Strings : Triple QuotesThe multi-line string needs to be generally escaped using .Strings can be surrounded in a pair of matching triple-quotes: """ (triple double quotes) or (triple single quotes).End of lines do not need to be escaped when using triple-quotes, but they will be included in the string. 27
  28. 28. Python : Strings : Triple Quotes : ExampleExample : Example :>>> s = a >>> s = a... b ... b>>> print s >>> print sab a>>> >>> s bab >>> s>>> anb >>> 28
  29. 29. Python : Strings : + and *Two string literals next to Example : each other are >>> s = x y # without + automatically >>> s concatenated xyStrings can be >>> s = s + z concatenated (glued >>> s together) with the + xyz operator >>> s = < + hi * 5 + >Strings can be repeated >>> s with the * operator <hihihihihi> >>> 29
  30. 30. Python : Strings : SubstringStrings can be subscripted Example : (indexed); like in C, the first >>> word=OPEN character of a string has >>> word subscript (index) 0. OPENThere is no separate character type; a character is simply a >>> word[0] string of size one. OIndices may be negative >>> word[3] numbers, to start counting N from the right. >>> word[-1] N >>> 30
  31. 31. Python : Strings : SlicesSubstrings can be Example : specified with the slice >>> word [0:2] notation: two indices OP separated by a colon >>> word [2:4]Slice indices have useful EN defaults; an omitted first >>> word[:2] # first two characters index defaults to zero, an OP omitted second index >>> word[2:] # from 3rd char till end defaults to the size of the EN string being sliced. >>> 31
  32. 32. Python : Strings : ErrorsPython strings cannot be Python catches if the subscript changed. Assigning to an index is out of range. indexed position in the string results in an error.Example : Example :>>> word[0]=C >>> word[5]Traceback (most recent call last): Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <module>TypeError: str object does not IndexError: string index out of range support item assignment >>>>>> 32
  33. 33. Python : Strings : FunctionsThe len() is used to calculate The strip() is used to remove length of the string. leading & trailing whitespace.Example : Example :>>> word >>> spacious .strip()OPEN spacious>>> len(word) >>>4>>> 33
  34. 34. PythonModule : Lists 34
  35. 35. Python : Lists : FeaturesThe list is a compounded data Example : type that is used to group >>> a = [o, g, 10, 20] together values. >>> aThe list can be written as a list of comma-separated values [o, g, 10, 20] (items) between square brackets. >>> a[0]List items need not all have the o same type. >>> a[-2]Like string indices, list indices 10 start at 0 >>> 35
  36. 36. Python : Lists : MutableUnlike strings, which are Example : immutable, it is possible to >>> a = [o, g, 10, 20] change individual elements >>> a[2] = a[2] + 5 of a list. >>> aAssignment to slices is also [o, g, 15, 20] possible, and this can even change the size of the list or >>> a[0:2] = [5, 10] clear it entirely >>> a [5, 10, 15, 20] >>> a[0:2]= [] >>> a [15, 20] 36
  37. 37. Python : Lists : FunctionsThe len() is used to get number Example : of elements in a list. >>> a = [15, 20]The append() is used to add an >>> a element at end of a list. [15, 20] >>> len(a) # number of elements in a list 2 >>> a.append(25) >>> a [15, 20, 25] >>> 37
  38. 38. Python : Lists : Assignment by ReferenceAssignment manipulates Example : references. >>> a = [15, 20]x = y does not make a copy of y >>> b = a >>> a.append(25)x = y makes x reference the object y references >>> a [15, 20, 25]Very useful. >>> bBut be careful. [15, 20, 25] >>> 38
  39. 39. Python : Lists : Assignment by Referencea = [10, 20, 30] a 10 20 30 a b=a 10 20 30 b aa.append(4) 10 20 30 40 b 39
  40. 40. PythonModule : Control Flow 40
  41. 41. Python : Control Flowif condition: while condition: statements statements[elif condition: statements] ... for var in sequence:else: statements statements break, continue else, pass 41
  42. 42. Python : Control Flow : if StatementProgram : Example :$ cat $ python if_stmt.pyprint "Enter 0 or 1: " Enter 0 or 1:x = input() 0if x == 0: zero print zero $ NOTES : There can be zero or moreelif x == 1: elif parts, and the else part is print one optional. The keyword ‘elif‘ is short for ‘else if’, and is useful to avoidelse: excessive indentation. An if ... elif ... elif ... sequence is a print unknown substitute for the switch or case$ statements found in other languages. 42
  43. 43. Python : Control Flow : for StatementProgram : Example :$ cat $ python for_loop_range.pyfor i in range(2) : 0 print i 1$ $Program 2 : Example 2 :$ cat $ python for_loop_list.pya = [10, 20]; 10for x in a: 20 print x $$ 43
  44. 44. Python : Control Flow : range() functionThe built-in function range() is used to Example : generate a sequence of numbers. >>> range(5)It generates arithmetic progressions. [0, 1, 2, 3, 4]If you do need to iterate over a sequence of numbers, it is useful. >>> range(5,10)The given end point is never part of [5, 6, 7, 8, 9] the generated sequence. >>> range(0, 10, 3)It is possible to let the range start at [0, 3, 6, 9] another number (by default it starts from 0) >>>It is possible to specify a different increment also (by default it is 1) 44
  45. 45. Python : Control Flow : break, continue, else statementThe break statement, like in C, breaks out of the smallest enclosing for or while loop.The continue statement, also borrowed from C, continues with the next iteration of the loop.Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers: 45
  46. 46. Python : Control Flow : break, continue : exampleExample : Output :$ cat $ python break_continue.pyfor n in range(1, 5): (1, iter begin) print(n,iter begin) (1, iter end) if n == 2: (2, iter begin) print(n, continue) continue (2, continue) elif n == 3: (3, iter begin) print(n, break) (3, break) break (3, loop end) print(n,iter end) $print(n,loop end) 46
  47. 47. Python : Control Flow : loop else : ExampleExample : Output :$ cat $ python loop_else.pyfor i in range(2): (for : , 0) print(for : , i)else: # for loop (for : , 1) print(for completed : , i) (for completed : , 1) (while : , 1)while i < 3: (while : , 2) print(while : , i) (while completed : , 3) i=i+1else: # while loop $ print(while completed : , i) 47$
  48. 48. Python : Control Flow : pass StatementThe pass statement does Program : nothing. $ cat pass.pyIt can be used when a while True: statement is required syntactically but the program pass requires no action. $It is like a noop in assembly Output : language. $ python ./ ^CTraceback (most recent call last): File "./", line 1, in <module> while True: KeyboardInterrupt 48 $
  49. 49. PythonModule : Functions 49
  50. 50. Python : Functions : Formatdef name(arg1, arg2, ...): """documentation""" # optional doc string statements return # from procedure return expression # from function 50
  51. 51. Python : Functions : Define FunctionThe keyword def introduces a Program : function definition. $ cat func.pyIt must be followed by the def sum(p1, p2): function name and the return p1 + p2 parenthesized list of formal total = sum(10, 20) parameters. print totalThe statements that form the $ body of the function start at the next line, and must be Output : indented. $ python 30 $ 51
  52. 52. Python : Functions : Default ArgumentsIt is also possible to define Program : functions with a variable $ cat number of arguments. def sum(p1, p2 = 20):The most useful form is to return p1 + p2 specify a default value for total = sum(10) one or more arguments. print totalThis creates a function that $ can be called with fewer Output : arguments than it is defined to allow. $ python 30 $ 52
  53. 53. Python : Functions : Keyword ArgumentsFunctions can also be called using Program : keyword arguments of the form argumentname=value $ cat func_arg_keyword.pyIn a function call, keyword arguments def sum(p1, p2): must follow positional arguments. return p1 + p2All the keyword arguments passed total = sum(10, p2=20) must match one of the arguments accepted by the function. print totalThe order of keyword arguments is $ not important. Output :No argument may receive a value $ python more than once. 30 $ 53
  54. 54. Python : Functions : Lambda FormsWith the lambda keyword, small Program : anonymous functions can be created. $ cat func_lambda.pyLambda forms can be used wherever sum = lambda x, y : x + y function objects are required. total = sum(10, 20)They are syntactically restricted to a print total single expression. $This feature is commonly found in functional programming languages Output : like Lisp. It has been added on $ python popular demand. 30 $ 54
  55. 55. Python : Functions : Documentation StringThe first statement of the function Program : body can optionally be a string $ cat literal; this string literal is the function’s documentation string, or def sum(p1, p2): docstring. """sum adds two numbersThe docstring for a function can be and returns total""" accessed by using functionname.__doc__ return p1 + p2 print(sum.__doc__) $ python sum adds two numbers and returns total $ 55
  56. 56. PythonModule : Data Structures 56
  57. 57. Python : Data Structures : List Methodslist.append(x) Add an item to the end of the list; equivalent to a[len(a):] = [x].list.extend(L) Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.list.insert(i, x) Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). 57
  58. 58. Python : Data Structures : List Methodslist.remove(x) Remove the first item from the list whose value is x. It is an error if there is no such item.list.pop([i]) Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)list.index(x) Return the index in the list of the first item whose value is x. It 58 is an error if there is no such item.
  59. 59. Python : Data Structures : List Methodslist.count(x) Return the number of times x appears in the list.list.sort() Sort the items of the list, in place.list.reverse() Reverse the elements of the list, in place. 59
  60. 60. Python : Data Structures : List Methods : Example>>> a = [15, 20, 25] >>> a.pop() # pop an element from end>>> a 15[15, 20, 25] >>> a>>> a.index(20) # index of element [15, 20, 25]1 >>> a.reverse() # reverse the list>>> a.append(15) # add element at end >>> a>>> a [25, 20, 15][15, 20, 25, 15] >>>>>> a.count(15) # number of occurences >>> a.sort() # sort the list2 >>> a>>> [15, 20, 25] >>> 60
  61. 61. Python : Data Structures : List Methods : Example 2>>> a = [15, 20, 25]>>> a[15, 20, 25]>>> a.remove(20) # remove an element>>> a[15, 25]>>> a.insert(1, 20) # insert at an index>>> a[15, 20, 25]>>> a.pop(1) # pop from an index20>>> 61
  62. 62. Python : Data Structures : List as StackThe list methods make it very Example : easy to use a list as a stack, >>> stack = [10, 20] where the last element >>> stack.append(30) # push added is the first element >>> stack.append(40) # push retrieved (“last-in, first-out”). >>> stackTo add an item to the top of the stack, use append(). [10, 20, 30, 40] >>> stack.pop() # popTo retrieve an item from the top of the stack, use pop() 40 without an explicit index. >>> stack.pop() # pop 30 >>> stack [10, 20] 62 >>>
  63. 63. Python : Data Structures : QueuesIt is also possible to use a list as a Example : queue, where the first element >>> from collections import deque added is the first element retrieved (“first-in, first-out”); however, lists >>> queue = deque([30, 40, 50]) are not efficient for this purpose. >>> queueWhile appends and pops from the deque([30, 40, 50]) end of list are fast, doing inserts or pops from the beginning of a list is >>> queue.append(60) # append to right slow (because all of the other >>> queue elements have to be shifted by one). deque([30, 40, 50, 60])To implement a queue, use >>> queue.popleft() # pop from left collections.deque which was 30 designed to have fast appends and pops from both ends. >>> queue.popleft() # pop from left 40 63 >>>
  64. 64. Python : Data Structures : delThere is a way to remove an item Example : from a list given its index instead of >>> a = [10,20,30,40] its value: the del statement. >>> del a[0] # delete an elementThis differs from the pop() method >>> a which returns a value. [20, 30, 40]The del statement can also be used to remove slices from a list or clear >>> del a[0:2] # delete a slice the entire list. >>> aThe del can be used to delete a [40] variable also. Once the variable >>> del a[:] # delete all elements in a list has been deleted, it is an error to refer to that unless a value is >>> a assigned to the variable again. [] >>> >>> del a # delete the variable 64 >>>
  65. 65. Python : Data Structures : TuplesThe lists and strings have many Example : common properties, such as indexing and slicing operations. >>> t = 1, two # parenthesis optionalThey are two examples of sequence >>> t data types (str, bytes, bytearray, (1, two) list, tuple, range). >>> t[0]Since Python is an evolving language, other sequence data types may be 1 added. >>> len(t)There is also another standard 2 sequence data type: the tuple. >>>A tuple consists of a number of values separated by commas.Tuples, like strings, are immutable: it is not possible to assign to the individual items of a tuple. 65
  66. 66. Python : Data Structures : Tuples : Singleton & EmptyA special problem is the construction Example : of tuples containing 0 or 1 items. >>> t = () # empty tupleThe syntax has some extra quirks to accommodate these. >>> tEmpty tuples are constructed by an () empty pair of parentheses >>> t = (h,) # trailing commaA tuple with one item is constructed >>> t by following a value with a comma (it is not sufficient to enclose a (h,) single value in parentheses). >>> t = (h) # incorrect tuple >>> t h >>> 66
  67. 67. Python : Data Structures : Tuples : Sequence UnpackingTo get the values from a tuple is Example : called sequence unpacking and works for any sequence on the >>> t=(1,two) # sequence packing right-hand side. >>> tSequence unpacking requires that (1, two) there are as many variables on the left side of the equals sign as there >>> a,b = t # sequence unpacking are elements in the sequence. >>> a 1 >>> b two >>> 67
  68. 68. Python : Data Structures : SetsA set is an unordered collection with no Example : duplicate elements. >>> vowels = {a,e,i,o,u,a,e}Basic uses include membership testing and eliminating duplicate entries. >>> vowelsSet objects also support mathematical set([a, u, e, i, o]) operations like union, intersection, difference, and symmetric difference. >>> print(vowels)Curly braces or the set() function can be set([a, u, e, i, o]) used to create sets. >>> a in vowelsNote: To create an empty set you have to use set(), not {}; the latter creates an True empty dictionary. >>> b in vowels False >>> 68
  69. 69. Python : Data Structures : Sets OperationsExample : >>> n1=set(123) # set method>>> z1={} # incorrect empty set >>> n2={2,3,4} # curly braces>>> z1 >>> n1{} set([1, 3, 2]) >>> n2>>> z2=set() # correct empty set set([3, 2, 4])>>> z2 >>> n1 & n2 # in bothset([]) set([3, 2])>>> >>> n1 | n2 # in either set([1, 3, 2, 4]) >>> 69
  70. 70. Python : Data Structures : DictionariesAnother useful data type built into Python Example : is the dictionary. >>> d = {one : 1, two : 2} # dictionaryDictionaries are sometimes found in other languages as “associative memories” >>> d or “associative arrays”. {two: 2, one: 1}Unlike sequences, which are indexed by a >>> d[one] range of numbers, dictionaries are indexed by keys, which can be any 1 immutable type; strings and numbers can always be keys. >>> d[three] = 3 # store a new pairIt is best to think of a dictionary as an >>> d unordered set of key: value pairs, with {three: 3, two: 2, one: 1} the requirement that the keys are unique (within one dictionary) >>>The main operations on a dictionary are storing a value with some key and extracting the value given the key. 70
  71. 71. Python : Data Structures : Dictionaries : ExampleIt is also possible to delete a Example : key:value pair with del. >>> d = {three: 3, two: 2, one: 1}If you store using a key that is >>> del d[one] # delete key: value pair already in use, the old value >>> d associated with that key is {three: 3, two: 2} forgotten. >>> d[three] = ||| # overwrite valueIt is an error to extract a value >>> d using a non-existent key. {three: |||, two: 2} >>> >>> d[four] # invalid key : error KeyError: four >>> 71
  72. 72. Python : Data Structures : Dictionaries : FunctionThe keys() method of a Example : dictionary object returns a list >>> d = {three: 3, two: 2, one: 1} of all the keys used in the >>> d.keys() # get list of keys dictionary, in arbitrary order [one, three, two]To check whether a single key >>> d.values() # get list of values is in the dictionary, use the in keyword. [1, 3, 2] >>> one in d # check existence of keyThe values() method can be used to extract just values. True >>> d.items() # in list & tuple formatThe items() method can be used to extract the data in a [(one, 1), (three, 3), (two, 2)] list of tuples format. >>> 72
  73. 73. Python : Data Structures : LoopingFunctions for Dictionary : iteritems()Method iteritems() Example : >>> capital = { India : New Delhi,When looping through UK : London } dictionaries, the key and >>> for k,v in capital.iteritems(): corresponding value can ... print k , v be retrieved at the same time using the iteritems() ... method. India Delhi UK London >>> 73
  74. 74. Python : Data Structures : LoopingFunctions for Sequence : reversed()To loop over a sequence in Example : reverse, first specify the >>> for i in reversed(range(1,10,2)): sequence in a forward ... print i direction and then call ... the reversed() function. 9 7 5 3 1 >>> 74
  75. 75. Python : Data Structures : LoopingFunctions for Sequence : sorted()To loop over a sequence in Example : sorted order, use the >>> country = [IN, UK, AUS] sorted() function which >>> for c in sorted(country): returns a new sorted list ... print c while leaving the source ... unaltered. AUS IN UK >>> country [IN, UK, AUS] >>> 75
  76. 76. Python : Data Structures : LoopingFunctions for Sequence : enumerateWhen looping through a Example : sequence, the position >>> country = [IN, UK, AUS] index and corresponding >>> for i, c in enumerate(country): value can be retrieved at ... print i, c the same time using the ... enumerate() function. 0 IN 1 UK 2 AUS >>> 76
  77. 77. Python : Data Structures : Looping Functions for Sequence : zipTo loop over two or more Example : sequences at the same >>> questions = [country, favorite sports] time, the entries can be >>> answers = [India, Volleyball] paired with the zip() function. >>> for q, a in zip(questions, answers): ... print Your {0} is {1}. . format(q, a) ... Your country is India. Your favorite sports is Volleyball. >>> 77
  78. 78. PythonModule : Python Modules 78
  79. 79. Python : Modules : ScriptIf you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost.Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script.As your program gets longer, you may want to split the script into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program. 79
  80. 80. Python : Modules : DefinitionPython has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter.Such a file is called a module; definitions from a module can be imported into other modules or into the main module.A module is a file containing Python definitions and statements.The file name is the module name with the suffix .py appended.Within a module, the module’s name is available as the value of the global variable __name__. 80
  81. 81. Python : Modules : DefinitionProgram : Output : $ python$ cat >>> import modcalc1# calculator module >>> modcalc1.add(10,20)def add(a, b): 30 return a + b >>> modcalc1.sub(30,10)def sub(a, b): 20 >>> modcalc1.__name__ # module name return a - b modcalc1$ >>> $ 81
  82. 82. Python : Modules : Module Search PathWhen a module named ABC is imported, the interpreter first searches for a built-in module with that name.If not found, it then searches for a file named in a list of directories given by the variable sys.path.The sys.path is initialized from these locations: the directory containing the input script (or the current directory). PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH). the installation-dependent default. 82
  83. 83. Python : Modules : Module Search Path : Example>>> import sys>>> sys.path[, /usr/lib/, /usr/lib/python2.7, /usr/lib/python2.7/plat-linux2, /usr/lib/python2.7/lib-tk, /usr/lib/python2.7/lib-old, /usr/lib/python2.7/lib-dynload, /usr/lib/python2.7/site-packages, /usr/lib/python2.7/site- packages/PIL, /usr/lib/python2.7/site-packages/gst-0.10, /usr/lib/python2.7/site-packages/gtk-2.0, /usr/lib/python2.7/site- packages/setuptools-0.6c11-py2.7.egg-info, /usr/lib/python2.7/ site-packages/webkit-1.0]>>> 83
  84. 84. Python : Modules : Compiled Python FilesAs an important speed-up of the start-up time for short programs that use a lot of standard modules, if a file called spam.pyc exists in the directory where is found, this is assumed to contain an already-“byte-compiled” version of the module spam.The modification time of the version of used to create spam.pyc is recorded in spam.pyc, and the .pyc file is ignored if these don’t match.Normally, you don’t need to do anything to create the spam.pyc file. Whenever is successfully compiled, an attempt is made to write the compiled version to spam.pyc.The contents of the spam.pyc file are platform independent, so a Python module directory can be shared by machines of different architectures. 84
  85. 85. Python : Modules : Compiled Python Files : ExampleProgram : Output : $ python # without -O$ cat >>> import modcalc1 # generates .pyc# calculator module $ ls*def add(a, b): return a + b modcalc1.pyc (generated)def sub(a, b): $ return a - b$ 85
  86. 86. Python : Modules : Optimized Python FilesWhen the Python interpreter is invoked with the -O flag, optimized code is generated and stored in .pyo files.The optimizer currently doesn’t help much; it only removes assert statements.When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode. 86
  87. 87. Python : Modules : Optimized Python Files : ExampleProgram : Output : $ python -O # with -O$ cat >>> import modcalc1 # generates .pyo# calculator module $ ls*def add(a, b): return a + b modcalc1.pycdef sub(a, b): modcalc1.pyo (generated) $ return a - b$ 87
  88. 88. Python : Modules : .pyc and .pyoA program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.When a script is run by giving its name on the command line, the byte code for the script is never written to a .pyc or .pyo file.The startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module.It is possible to have a file called spam.pyc (or spam.pyo when -O is used) without a file for the same module. This can be used to distribute a library of Python code in a form that is moderately hard to reverse engineer. 88
  89. 89. Python : Modules : dir() functionThe built-in function dir() is used to Example : find out which names a module >>> import modcalc1 defines. >>> dir(modcalc1)It returns a sorted list of strings. [__builtins__, __doc__, __file__,Without arguments, dir() lists the __name__, __package__, add, sub] names you have defined >>> import sys currently. >>> dir(sys)To get a list of built-in functions [... ps1, ps2, .... stderr, stdin, stdout,...] >>> import __builtin__ >>> dir() # without argument >>> dir(__builtin__) [__builtins__, __doc__, __name__, __package__, modcalc1, sys] >>> 89
  90. 90. Python : Modules : PackagesPackages are a way of structuring Python’s module namespace by using “dotted module names”.For example, the module name A.B designates a submodule named B in a package named A.Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names. 90
  91. 91. Python : Modules : Packages : DesignSuppose you want to design a collection of modules (a “package”).A possible Hierarchical File system: A # top level package directory # initialize the package A1 # submodule directory # initialize the submodule # some python file A2 # submodule directory # initialize the submodule # some python file 91
  92. 92. Python : Modules : Packages : __init__.pyWhen importing the package, Python searches through the directories on sys.path looking for the package subdirectory.The files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.In the simplest case, can just be an empty file, but it can also execute initialization code for the package. 92
  93. 93. Python : Modules : Packages : import methodsUsers of the package can import individual modules from the package, for example: import sound.effects.echo This loads the submodule sound.effects.echo. It must be referenced with its full name. sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)An alternative way of importing the submodule is: from sound.effects import echo This also loads the submodule echo, and makes it available without its package prefix, so it can be used as follows: echo.echofilter(input, output, delay=0.7, atten=4) 93
  94. 94. Python : Modules : Packages : import methods 2Yet another variation is to import the desired function or variable directly: from sound.effects.echo import echofilter Again, this loads the submodule echo, but this makes its function echofilter() directly available: echofilter(input, output, delay=0.7, atten=4) 94
  95. 95. Python : Modules : Packages : import * from packagefrom sound.effects import *The import statement uses the following convention: if a package’s code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered.__all__ = ["echo", "surround", "reverse"]If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in and then imports whatever names are defined in the package.It is considered bad practice in production code. 95
  96. 96. Python : Modules : Packages : Intra- package ReferencesSupports explicit relative imports with the from module import nameThe explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import.Examples : from . import echo # from current package from .. import formats # from parent package from ..filters import equalizer # from parent pkg / filters 96
  97. 97. PythonModule : Input & Output 97
  98. 98. Python : IO : str & reprConvert any value to string : Example : str() and repr() >>> s = hello, worldnThe str() function is meant to >>> str(s) return representations of hello, worldn values which are fairly human-readable. >>> repr(s) # add quote and backslashThe repr() is meant to generate "hello, worldn" representations which can be read by the interpreter. >>> str(10)The repr() of a string adds 10 string quotes and >>> repr(20) backslashes. 20 >>> 98
  99. 99. Python : IO : methodsThe str.rjust() method of string objects, which right-justifies a string in a field of a given width by padding it with spaces on the left.There are similar methods str.ljust() and methods do not write anything, they just return a new string.If the input string is too long, they don’t truncate it, but return it unchanged;There is another method, str.zfill(), which pads a numeric string on the left with zeros. >>> 12.zfill(5) 00012 >>> 99
  100. 100. Python : IO : rjust : ExampleExample : Example :>>> for x in range(1, 11, 3): >>> for x in range(1, 11, 3):... print x, x*x, x*x*x ... print repr(x).rjust(2),... ... print repr(x*x).rjust(3),111 ... print repr(x*x*x).rjust(4)4 16 64 ...7 49 343 1 1 110 100 1000 4 16 64>>> 7 49 343Note: It is left justified by default. 10 100 1000 >>> NOTE: The trailing comma on lines. 100
  101. 101. Python : IO : str.format()The bracket within the string are Example : called format fields. >>> print Country {}, Capital {} .They are replaced with the objects format(India,New Delhi) passed into the str.format() Country India, Capital New Delhi method. >>> print Country {0}, Capital {1} .A number in the brackets refers to format(India,New Delhi) the position of the object Country India, Capital New Delhi passed into the str.format() method. >>> print Capital of {country} is {capital}.format(If keyword arguments are used in the str.format() method, their ... capital=New Delhi, values are referred to by using country=India) the name of the argument. Capital of India is New Delhi >>> 101
  102. 102. Python : IO : str.format() : colonAn optional : and format specifier can Example : follow the field name. >>> import mathPassing an integer after the : will cause that field to be a minimum >>> print PI : {} .format(math.pi) number of characters wide. PI : 3.14159265359The : allows greater control over how >>> print PI : {:.3f} .format(math.pi) the value is formatted. PI : 3.142 :.3f (3 places after decimal) >>> print => {0:10d} . format(20) :10d (10 places for number) => 20 :10 (10 fields for string) >>> 102
  103. 103. Python : IO : str.format() : old c style formattingThe % operator can also be used for Example : string formatting. >>> import mathIt interprets the left argument much like a sprintf()-style format string to >>> print PI = %5.3f. % math.pi be applied to the right argument, PI = 3.142. and returns the string resulting from this formatting operation. >>>Since str.format() is quite new, a lot of Python code still uses the % operator.However, because this old style of formatting will eventually be removed from the language, str.format() should generally be used. 103
  104. 104. Python : IO : Files : openThe open() returns a file object, and is most commonly used with two arguments : open(filename, mode).The first argument is a string containing the filename.The second argument is another string containing the mode (few characters describing the way in which the file will be used). r when the file will only be read w for only writing (an existing file with the same name will be erased) a opens the file for appending; any data written to the file is automatically added to the end. r+ opens the file for both reading and writing.The mode argument is optional; r will be assumed if it’s omitted. 104
  105. 105. Python : IO : Files : open : ExampleExample :>>> f = open(/tmp/workfile, w)>>> print f<open file /tmp/workfile, mode w at 0xb7700c28>>>> 105
  106. 106. Python : IO : Files : Methods : readTo read a file’s contents, call, which reads some Example : quantity of data and returns it as a string. >>> size is an optional numeric argument. This is the entire file.nWhen size is omitted or negative, the >>> entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are >>> read and returned.If the end of the file has been reached, will return an empty string (""). 106
  107. 107. Python : IO : Files : Methods : readlinef.readline() reads a single line from Example : the file. >>> f.readline()A newline character (n) is left at the end of the string This is the first line of the file.nThe new line character is only omitted >>> f.readline() on the last line of the file if the file doesn’t end in a newline. Second line of the filenThis makes the return value >>> f.readline() unambiguous.If f.readline() returns an empty string, the end of the file has been >>> reached.A blank line is represented by n, a string containing only a single newline. 107
  108. 108. Python : IO : Files : Methods : readlinesThe f.readlines() returns a list containing Example : all the lines of data in the file. >>> f.readlines()If given an optional parameter sizehint, it reads that many bytes from the file and [This is the first line of the file.n, enough more to complete a line, and Second line of the filen] returns the lines from that. >>>This is often used to allow efficient reading of a large file by lines, but without Alternative Approach : having to load the entire file in memory. >>> for line in f:Only complete lines will be returned. ... print line,An alternative approach to reading lines is to loop over the file object. This is This is the first line of the file. memory efficient, fast, and leads to simpler code: Second line of the file >>> 108
  109. 109. Python : IO : Files : Methods : writeThe f.write(string) writes the Example : contents of string to the file. >>> f.write(This is a testn)The f.write() returns None. # Convert to String firstTo write something other than a >>> value = (the answer, 42) string, it needs to be converted to a string first: >>> s = str(value) >>> f.write(s) 109
  110. 110. Python : IO : Files : Methods : seek and tellThe f.tell() returns an integer giving the file Example : object’s current position in the file, measured in bytes from the beginning >>> f = open(/tmp/workfile, r+) of the file. >>> f.write(0123456789abcdef)To change the file object’s position, use, from_what). >>> # 6th byte in a fileThe position is computed from adding >>> offset to a reference point; the reference point is selected by the 5 from_what argument. >>>, 2) # 3rd byte before 0 : from the beginning of the file end 1 : uses the current file position >>> 2 : uses the end of the file as the d reference point.The from_what can be omitted and >>> defaults to 0, using the beginning of the file as the reference point. 110
  111. 111. Python : IO : Files : Methods : closeWhen you’re done with a Example : file, call f.close() to close >>> f.close() it and free up any system >>> resources taken up by Traceback (most recent call last): the open file. File "<stdin>", line 1, in ?After calling f.close(), ValueError: I/O operation on closed attempts to use the file file object will automatically >>> fail. 111
  112. 112. Python : IO : Files : with keywordIt is good practice to use Example : the with keyword when >>> with open(/tmp/workfile, r) as f: dealing with file objects. ... read_data = has the advantage >>> f.closed that the file is properly True closed after its suite >>> finishes, even if an exception is raised on the way.It is also much shorter than writing equivalent try- finally blocks: 112
  113. 113. Python : IO : Files : pickleStrings can easily be written to and read from a file. The other data types needs to be converted to string before writing. Also after reading non-string data types needs to be converted from string.Python provides a standard module called pickle.The pickle module that can take almost any Python object and convert it to a string representation; this process is called pickling.Reconstructing the object from the string representation is called unpickling.Between pickling and unpickling, the string representing the object may have been stored in a file or data, or sent over a network connection to some distant machine. 113
  114. 114. Python : IO : Files : pickle : ExampleIf you have an object x, and a file object f that’s been opened for writing, the simplest way to pickle the object takes only one line of code: pickle.dump(x, f)To unpickle the object again, if f is a file object which has been opened for reading: x = pickle.load(f) 114
  115. 115. Python : IO : Keyboard : input and raw_inputraw_input() : Example : Python 3 : doesnt exist >>> x = input() Python 2 : returns a string. 2*6input(): >>> x Python 2: run the input as a Python expression. 12 Python 3, old raw_input() has >>> x = raw_input() been renamed to input(). 2*6Since getting a string was almost always what we wanted, Python >>> x 3 does that with input(). 2 * 6If you ever want the old behavior, >>> eval(input()) works. 115
  116. 116. PythonModule : Errors & Exceptions 116
  117. 117. Python : Errors : Syntax ErrorsSyntax errors, also known as parsing Example : errors, are perhaps the most common kind of complaint you get while >>> if True print true developing the program. File "<stdin>", line 1The parser repeats the offending line and if True print true displays a little ‘arrow’ pointing at the earliest point in the line where the error ^ was detected. SyntaxError: invalid syntaxThe error is caused by (or at least detected at) the token preceding the >>> arrow. Corrected Example :The File name and line number are printed >>> if True: print true so you know where to look in case the input came from a script. ...In the example, the error is detected at the true keyword print, since a colon (:) is missing before it. >>> 117
  118. 118. Python : Exceptions : ExceptionsEven if a statement or expression is The preceding part of the error message syntactically correct, it may cause an shows the context where the exception error when an attempt is made to happened, in the form of a stack execute it. traceback with file name and line no.Errors detected during execution are called exceptions and are not unconditionally fatal.The last line of the error message indicates what happened. Example :Exceptions come in different types, and the type is printed as part of the >>> 10 * (1/0) message: ZeroDivisionError. Traceback (most recent call last):The string printed as the exception type is File "<stdin>", line 1, in ? the name of the built-in exception that occurred. ZeroDivisionError: integer division or modulo by zeroThe rest of line provides detail based on the type of exception and what caused >>> 118 it.
  119. 119. Python : Exceptions : Exceptions : ExampleExample :>>> 4 + spam*3Traceback (most recent call last): File "<stdin>", line 1, in ?NameError: name spam is not defined>>> 2 + 2Traceback (most recent call last): File "<stdin>", line 1, in ?TypeError: cannot concatenate str and int objects>>> 119
  120. 120. Python : Exceptions : Handling ExceptionsIt is possible to write programs that handle selected exceptions using try block. The try-except block try : block of statements except exception_name : block of statementsThe try statement works as follows. First, the try clause (the statement(s) between the try and except keywords) is executed. If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above. 120
  121. 121. Python : Exceptions : Handling Exceptions : Example$ cat Example :while True: $ python try: x = int(raw_input("x : ")) x:a break # leave loop not a number. retry... except ValueError: x : 10 print "not a number. retry..."print x : {0}.format(x) x : 10$ $ 121
  122. 122. Python : Exception : Handling Exceptions : Multiple ExceptionsA try statement may have more than one Example : except clause, to specify handlers for $ cat different exceptions. import sysAt most one handler will be executed. try :Handlers only handle exceptions that occur in the corresponding try clause, f = open(myfile.txt); i = int(f.readline().strip()) not in other handlers of the same try except IOError as (errno, strerror): statement. print "I/O error({0}): {1}".format(errno, strerror)An except clause may name multiple exceptions as a parenthesized tuple. except ValueError :The last except clause may omit the print "Could not convert data to an integer." exception name(s), to serve as a except : wildcard. It can be used to re-raise the exception (allowing a caller to handle print "Unexpected error:", sys.exc_info()[0] the exception as well). raise $ 122
  123. 123. Python : Exception : Handling Exceptions : else clauseThe try ... except statement has Syntax : an optional else clause, try : which, when present, must follow all except clauses. try blockIt is useful for code that must except : # one or more blocks be executed if the try clause does not raise an exception. except blockThe use of the else clause is else : better than adding additional else block code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try ... except 123 statement.
  124. 124. Python : Exception : Handling Exceptions : else clause : ExampleExample : Output :$ cat $ python /etc/passwd /etc/passwd1import sys /etc/passwd has 57 linesfor arg in sys.argv[1:]: cannot open /etc/passwd1 try: $ f = open(arg, r) except IOError: print cannot open, arg else: print arg, has, len(f.readlines()), lines f.close()$ 124
  125. 125. Python : Exception : Handling Exceptions : Exception ArgumentsWhen an exception occurs, it may have an associated value, also known as the exception’s argument. The presence and type of the argument depend on the exception type.The except clause may specify a variable after the exception name (or tuple). The variable is bound to an exception instance with the arguments stored in instance.args.For convenience, the exception instance defines __str__() so the arguments can be printed directly without having to reference .args.One may also instantiate an exception first before raising it and add any attributes to it as desired. 125
  126. 126. Python : Exception : Handling Exceptions : Exception Arguments : ExampleExample : Output :$ cat $ python try_raise.pytry: <type exceptions.Exception> raise Exception(MyExcpt, MyExcptArg) (MyExcpt, MyExcptArg)except Exception as inst: (MyExcpt, MyExcptArg) print type(inst) # the exception instance name = MyExcpt print inst.args # arguments in .args print inst # __str__ includes args also arg = MyExcptArg name, arg = inst # unpack args $ print name =, name print arg =, arg$ 126
  127. 127. Python : Exception : Handling Exceptions : IndirectlyException handlers don’t just Example : # example : handle exceptions if they def f_fails() : occur immediately in the try x = 1/0 # raise Zero Division Xptn clause, but also if they occur inside functions that are try : called (even indirectly) in the f_fails() try clause. except ZeroDivisionError as detail:If an exception has an print Handling run-time error:, argument, it is printed as the detail last part (‘detail’) of the message for exceptions. Output : $ python Handling run-time error: integer division or modulo by zero 127
  128. 128. Python : Exceptions : Raising ExceptionsThe raise statement allows the Example : # programmer to force a specified try: exception to occur raise NameError(HiThere)The sole argument to raise except NameError : indicates the exception to be print Re-raising Exception raised. This must be either an exception instance or an raise exception class (a class that Output : derives from Exception). $ python try_re_raise.pyIf you need to determine whether Reraising Exception an exception was raised but don’t intend to handle it, a Traceback (most recent call last): simpler form of the raise File "", line 2, in <module> statement allows you to re-raise raise NameError(HiThere) the exception: NameError: HiThere 128 $
  129. 129. Python : Exception : User-Defined ExceptionsPrograms may name their own Example : exceptions by creating a new class MyError(Exception): # subclass exception class. def __init__(self, value): # constructorExceptions should typically be self.value = value derived from the Exception def __str__(self): # string name of class class, either directly or indirectly. return repr(self.value) try :Exception classes can be defined which do anything any other raise MyError(2*2) class can do, but are usually except MyError as e: kept simple, often only offering print Exception occurred, value:, e.value a number of attributes that allow Output : information about the error to be extracted by handlers for the $ python exception. My exception occurred, value: 4 129 $
  130. 130. Python : Exception : Define Clean- up Actions : finallyThe try statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances.A finally clause is always executed before leaving the try statement, whether an exception has occurred or not.When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in a except or else clause), it is re-raised after the finally clause has been executed.The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. 130
  131. 131. Python : Exception : Define Clean- up Actions : finallyProgram : Output :$ cat >>> from try_finally import *def divide(x, y) : >>> divide(2, 1) try : result is 2 result = x / y executing finally clause except ZeroDivisionError: print "division by zero!" >>> divide(2, 0) else: division by zero! print "result is", result executing finally clause finally : >>> print "executing finally clause"$ 131
  132. 132. Python :Exception : Pre-Defined Clean-up ActionsSome objects define standard Program : clean-up actions to be undertaken when the object is with open("myfile.txt") as f: no longer needed, regardless of for line in f: whether or not the operation using the object succeeded or print line failed.The with statement allows objects like files to be used in a way NOTE : that ensures they are always The file f is always closed, even cleaned up promptly and correctly. if a problem was encountered whileOther objects which provide processing the lines. predefined clean-up actions will indicate it in documentation. 132
  133. 133. PythonModule : Classes 133
  134. 134. Python : Classes : Scopes & Namespaces : ExampleExample : do_nonlocal()def scope_test(): print("After nonlocal assignment:", spam) def do_local(): do_global() spam = "local spam" print("After global assignment:", spam) def do_nonlocal(): scope_test() nonlocal spam print("In global scope:", spam) spam = "nonlocal spam" def do_global(): Output : global spam After local assignment: test spam spam = "global spam" After nonlocal assignment: nonlocal spam spam = "test spam" After global assignment: nonlocal spam do_local() In global scope: global spam 134 print("After local assignment:", spam)
  135. 135. Python : Classesclass name: "documentation" statements-or-class name(base1, base2, ...): ...Most, statements are method definitions: def name(self, arg1, arg2, ...): ...May also be class variable assignments 135
  136. 136. Python : Classes : Exampleclass Stack: "A well-known data structure…" def __init__(self): # constructor self.items = [] def push(self, x): self.items.append(x) # the sky is the limit def pop(self): x = self.items[-1] # what happens if it’s empty? del self.items[-1] return x def empty(self): return len(self.items) == 0 # Boolean result 136
  137. 137. Python : Classes : Using ClassesTo create an instance, simply call the class object:x = Stack() # no new operator!To use methods of the instance, call using dot notation:x.empty() # -> 1x.push(1) # [1]x.empty() # -> 0x.push("hello") # [1, "hello"]x.pop() # -> "hello" # [1]To inspect instance variables, use dot notation:x.items # -> [1] 137
  138. 138. Python : Classes : Subclassingclass FancyStack(Stack): "stack with added ability to inspect inferior stack items" def peek(self, n): "peek(0) returns top; peek(-1) returns item below that; etc." size = len(self.items) assert 0 <= n < size # test precondition return self.items[size-1-n] 138
  139. 139. Python : Classes : Subclassing 2class LimitedStack(FancyStack): "fancy stack with limit on stack size" def __init__(self, limit): self.limit = limit FancyStack.__init__(self) # base class constructor def push(self, x): assert len(self.items) < self.limit FancyStack.push(self, x) # "super" method call 139