3. Python : Background : History
Developed by Guido van Rossum.
The language is named after the BBC show
“Monty Python’s Flying Circus”
It has nothing to do with reptiles.
www.opengurukul.com 3
4. Python : Background : Why
An 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.
www.opengurukul.com 4
5. Python : Background : Why
Compact & 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 statement
Modular
Allows you to split your program into modules.
Comes with a large collection of standard modules.
www.opengurukul.com 5
6. Python : Background : Why
Extensible
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.
www.opengurukul.com 6
8. Python : Interpreter : enter and exit
Interpreter is located in /usr/bin/ $ which python
python /usr/bin/python
To 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 linux2
To come out of python Type "help", "copyright", "credits"
interpreter, type quit() on or "license" for more
python prompt. information.
>>> quit()
$
www.opengurukul.com 8
9. Python : Interpreter : Interpret file
To interpret python file
$ python file
To interpret python modules
$ python -m module
Python 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)
www.opengurukul.com 9
10. Python : Interpreter : Interpret file :
Example
Program : Output :
$ cat hello.py $ python hello.py
print 'Hello World' Hello World
print "Hello World" Hello World
msg="Hello World" Hello World
print msg $
$
www.opengurukul.com 10
12. Python : Interpreter : Modules
To 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 sys
Covered in detail under
>>> sys.ps1
Modules section.
'>>> '
>>> sys.ps2
'... '
>>>
www.opengurukul.com 12
13. Python : Interpreter : Startup File
PYTHONSTARTUP is an Use :
environment variable that $ cat ~/.pythonrc.py
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
y
The file ~/.pythonrc.py is
generallu used as a default $
startup file. $ python
python > quit()
$
www.opengurukul.com 13
15. Python : Interpreter : OS
Environment
Environment Variables Program :
can be accessed $ cat env.py
within Python. import os
home = os.environ.get('HOME')
print home
$
Output :
$ python env.py
/home/surikuma
$
www.opengurukul.com 15
17. Python : Introduction : Comment
Anything that follows # is considered a comment in Python.
Example :
>>> print "hello world" # a comment on the same line as code
hello world
>>> # comment that will cause it to invoke secondary prompt
... print "hello india"
hello india
>>>
www.opengurukul.com 17
18. Python : Introduction : Variable
No need to declare
Need to assign (initialize)
use of uninitialized variable raises exception
Not typed
>>> age = "thirty"
>>> age = 20
Everything is a "variable"
Even functions, classes, modules
www.opengurukul.com 18
19. Python : Introduction : Assignment
Using Variables :
The equal sign ('=') is used to assign a value to a variable
Example :
>>> age = 30
>>> age
30
>>> name = 'Surinder Kumar'
>>> name
'Surinder Kumar'
www.opengurukul.com 19
>>>
20. Python : Introduction : underscore
variable
In interactive mode, the last Example :
printed expression is
>>> a = 2
assigned to the variable _
(underscore) >>> b = 5
It is a read only variable. >>> a * b
10
>>> c = 20
>>> _ + c
30
>>>
www.opengurukul.com 20
25. Python : Strings : Quotes
String can be enclosed in either >>> 'doesn't'
single quotes or double "doesn't"
quotes. >>> "doesn't"
Use as escape sequence as "doesn't"
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.'
>>> ' "Isn't," she said.'
' "Isn't," she said.'
>>>
www.opengurukul.com 25
26. Python : Strings : raw string
Use n to print a new Example :
line. >>> s = ' a n b'
>>> print s
Use raw-string (r) to
a
keep n as it is in the
b
string.
>>> s = r' a n b'
>>> print s
a n b
>>>
www.opengurukul.com 26
27. Python : Strings : Triple Quotes
The 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.
www.opengurukul.com 27
28. Python : Strings : Triple Quotes :
Example
Example : Example :
>>> s = 'a >>> s = '''a
... b' ... b'''
>>> print s >>> print s
ab a
>>> >>> s b
'ab' >>> s
>>> 'anb'
>>>
www.opengurukul.com 28
29. Python : Strings : + and *
Two string literals next to Example :
each other are >>> s = 'x' 'y' # without +
automatically >>> s
concatenated 'xy'
Strings 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>'
>>>
www.opengurukul.com 29
30. Python : Strings : Substring
Strings can be subscripted Example :
(indexed); like in C, the first >>> word='OPEN'
character of a string has
>>> word
subscript (index) 0.
'OPEN'
There is no separate character
type; a character is simply a >>> word[0]
string of size one. 'O'
Indices may be negative >>> word[3]
numbers, to start counting 'N'
from the right.
>>> word[-1]
'N'
>>>
www.opengurukul.com 30
31. Python : Strings : Slices
Substrings 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.
>>>
www.opengurukul.com 31
32. Python : Strings : Errors
Python 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
>>>
>>>
www.opengurukul.com 32
33. Python : Strings : Functions
The 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
>>>
www.opengurukul.com 33
35. Python : Lists : Features
The list is a compounded data Example :
type that is used to group
>>> a = ['o', 'g', 10, 20]
together values.
>>> a
The 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
>>>
www.opengurukul.com 35
36. Python : Lists : Mutable
Unlike 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.
>>> a
Assignment 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]
www.opengurukul.com 36
37. Python : Lists : Functions
The 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]
>>>
www.opengurukul.com 37
38. Python : Lists : Assignment by
Reference
Assignment 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.
>>> b
But be careful.
[15, 20, 25]
>>>
www.opengurukul.com 38
39. Python : Lists : Assignment by
Reference
a = [10, 20, 30] a 10 20 30
a
b=a 10 20 30
b
a
a.append(4) 10 20 30 40
b
www.opengurukul.com 39
41. Python : Control Flow
if condition: while condition:
statements statements
[elif condition:
statements] ... for var in sequence:
else: statements
statements
break, continue
else, pass
www.opengurukul.com 41
42. Python : Control Flow : if Statement
Program : Example :
$ cat if_stmt.py $ python if_stmt.py
print "Enter 0 or 1: " Enter 0 or 1:
x = input() 0
if x == 0: zero
print 'zero' $
NOTES : There can be zero or more
elif x == 1: elif parts, and the else part is
print 'one' optional. The keyword ‘elif‘ is short
for ‘else if’, and is useful to avoid
else: excessive indentation. An if ...
elif ... elif ... sequence is a
print 'unknown'
substitute for the switch or case
$ statements found in other
languages.
www.opengurukul.com 42
43. Python : Control Flow : for
Statement
Program : Example :
$ cat for_loop_range.py $ python for_loop_range.py
for i in range(2) : 0
print i 1
$ $
Program 2 : Example 2 :
$ cat for_loop_list.py $ python for_loop_list.py
a = [10, 20]; 10
for x in a: 20
print x $
$ www.opengurukul.com 43
44. Python : Control Flow : range()
function
The 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)
www.opengurukul.com 44
45. Python : Control Flow : break,
continue, else statement
The 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:
www.opengurukul.com 45
46. Python : Control Flow : break,
continue : example
Example : Output :
$ cat break_continue.py $ python break_continue.py
for 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') www.opengurukul.com 46
47. Python : Control Flow : loop else :
Example
Example : Output :
$ cat loop_else.py
$ python loop_else.py
for 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+1
else: # while loop
$
print('while completed : ', i)
www.opengurukul.com 47
$
48. Python : Control Flow : pass
Statement
The pass statement does Program :
nothing.
$ cat pass.py
It 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 ./pass.py
^CTraceback (most recent call last):
File "./pass.py", line 1, in <module>
while True:
KeyboardInterrupt
www.opengurukul.com 48
$
50. Python : Functions : Format
def name(arg1, arg2, ...):
"""documentation""" # optional doc string
statements
return # from procedure
return expression # from function
www.opengurukul.com 50
51. Python : Functions : Define Function
The keyword def introduces a Program :
function definition. $ cat func.py
It 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 total
The statements that form the $
body of the function start at
the next line, and must be Output :
indented. $ python func.py
30
$
www.opengurukul.com 51
52. Python : Functions : Default
Arguments
It is also possible to define Program :
functions with a variable $ cat func_default_arg.py
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 total
This creates a function that $
can be called with fewer
Output :
arguments than it is
defined to allow. $ python func_default_arg.py
30
$
www.opengurukul.com 52
53. Python : Functions : Keyword
Arguments
Functions can also be called using Program :
keyword arguments of the form
argumentname=value $ cat func_arg_keyword.py
In a function call, keyword arguments def sum(p1, p2):
must follow positional arguments. return p1 + p2
All the keyword arguments passed total = sum(10, p2=20)
must match one of the arguments
accepted by the function. print total
The order of keyword arguments is $
not important.
Output :
No argument may receive a value
$ python func_arg_keyword.py
more than once.
30
$
www.opengurukul.com 53
54. Python : Functions : Lambda Forms
With the lambda keyword, small Program :
anonymous functions can be
created. $ cat func_lambda.py
Lambda 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 func_lambda.py
popular demand.
30
$
www.opengurukul.com 54
55. Python : Functions : Documentation
String
The first statement of the function Program :
body can optionally be a string
$ cat func_doc.py
literal; this string literal is the
function’s documentation string, or def sum(p1, p2):
docstring.
"""sum adds two numbers
The docstring for a function can be and returns total"""
accessed by using
functionname.__doc__ return p1 + p2
print(sum.__doc__)
$ python func_doc.py
sum adds two numbers and returns total
$
www.opengurukul.com 55
57. Python : Data Structures : List
Methods
list.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).
www.opengurukul.com 57
58. Python : Data Structures : List
Methods
list.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
www.opengurukul.com
is an error if there is no such item.
59. Python : Data Structures : List
Methods
list.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.
www.opengurukul.com 59
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 list
2 >>> a
>>> [15, 20, 25]
>>>
www.opengurukul.com 60
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 index
20
>>>
www.opengurukul.com 61
62. Python : Data Structures : List as
Stack
The 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”).
>>> stack
To add an item to the top of the
stack, use append(). [10, 20, 30, 40]
>>> stack.pop() # pop
To retrieve an item from the top
of the stack, use pop() 40
without an explicit index. >>> stack.pop() # pop
30
>>> stack
[10, 20]
www.opengurukul.com 62
>>>
63. Python : Data Structures : Queues
It 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.
>>> queue
While 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
www.opengurukul.com 63
>>>
64. Python : Data Structures : del
There 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 element
This 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. >>> a
The 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
www.opengurukul.com 64
>>>
65. Python : Data Structures : Tuples
The lists and strings have many Example :
common properties, such as
indexing and slicing operations. >>> t = 1, 'two' # parenthesis optional
They 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. www.opengurukul.com 65
66. Python : Data Structures : Tuples :
Singleton & Empty
A special problem is the construction Example :
of tuples containing 0 or 1 items.
>>> t = () # empty tuple
The syntax has some extra quirks to
accommodate these. >>> t
Empty tuples are constructed by an ()
empty pair of parentheses >>> t = ('h',) # trailing comma
A 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'
>>>
www.opengurukul.com 66
67. Python : Data Structures : Tuples :
Sequence Unpacking
To 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. >>> t
Sequence 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'
>>>
www.opengurukul.com 67
68. Python : Data Structures : Sets
A 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. >>> vowels
Set 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 vowels
Note: To create an empty set you have to
use set(), not {}; the latter creates an True
empty dictionary.
>>> 'b' in vowels
False
>>>
www.opengurukul.com 68
69. Python : Data Structures : Sets
Operations
Example : >>> 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 both
set([])
set(['3', '2'])
>>>
>>> n1 | n2 # in either
set(['1', '3', '2', '4'])
>>>
www.opengurukul.com 69
70. Python : Data Structures :
Dictionaries
Another useful data type built into Python Example :
is the dictionary.
>>> d = {'one' : 1, 'two' : 2} # dictionary
Dictionaries 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 pair
It 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.
www.opengurukul.com 70
71. Python : Data Structures :
Dictionaries : Example
It 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 value
It is an error to extract a value >>> d
using a non-existent key.
{'three': '|||', 'two': 2}
>>>
>>> d['four'] # invalid key : error
KeyError: 'four'
>>>
www.opengurukul.com 71
72. Python : Data Structures :
Dictionaries : Function
The 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 key
The values() method can be
used to extract just values. True
>>> d.items() # in list & tuple format
The items() method can be
used to extract the data in a [('one', 1), ('three', 3), ('two', 2)]
list of tuples format. >>>
www.opengurukul.com 72
73. Python : Data Structures : Looping
Functions 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
>>>
www.opengurukul.com 73
74. Python : Data Structures : Looping
Functions 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
>>>
www.opengurukul.com 74
75. Python : Data Structures : Looping
Functions 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']
>>>
www.opengurukul.com 75
76. Python : Data Structures : Looping
Functions for Sequence : enumerate
When 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
>>>
www.opengurukul.com 76
77. Python : Data Structures : Looping
Functions for Sequence : zip
To 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.
>>>
www.opengurukul.com 77
79. Python : Modules : Script
If 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.
www.opengurukul.com 79
80. Python : Modules : Definition
Python 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__.
www.opengurukul.com 80
81. Python : Modules : Definition
Program : Output :
$ python
$ cat modcalc1.py
>>> 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'
$
>>>
$
www.opengurukul.com 81
82. Python : Modules : Module Search
Path
When 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 ABC.py 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.
www.opengurukul.com 82
84. Python : Modules : Compiled Python
Files
As 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 spam.py is found, this is assumed
to contain an already-“byte-compiled” version of the module
spam.
The modification time of the version of spam.py 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 spam.py 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. www.opengurukul.com 84
85. Python : Modules : Compiled Python
Files : Example
Program : Output :
$ python # without -O
$ cat modcalc1.py
>>> import modcalc1 # generates .pyc
# calculator module
$ ls modcalc1.py*
def add(a, b): modcalc1.py
return a + b modcalc1.pyc (generated)
def sub(a, b): $
return a - b
$
www.opengurukul.com 85
86. Python : Modules : Optimized
Python Files
When 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.
www.opengurukul.com 86
87. Python : Modules : Optimized
Python Files : Example
Program : Output :
$ python -O # with -O
$ cat modcalc1.py
>>> import modcalc1 # generates .pyo
# calculator module
$ ls modcalc1.py*
def add(a, b): modcalc1.py
return a + b modcalc1.pyc
def sub(a, b): modcalc1.pyo (generated)
$
return a - b
$
www.opengurukul.com 87
88. Python : Modules : .pyc and .pyo
A 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 spam.py 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.
www.opengurukul.com 88
89. Python : Modules : dir() function
The 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']
>>>
www.opengurukul.com 89
90. Python : Modules : Packages
Packages 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.
www.opengurukul.com 90
91. Python : Modules : Packages :
Design
Suppose you want to design a collection of modules (a
“package”).
A possible Hierarchical File system:
A # top level package directory
__init__.py # initialize the package
A1 # submodule directory
__init__.py # initialize the submodule
abc.py # some python file
A2 # submodule directory
__init__.py # initialize the submodule
xyz.py # some python file
www.opengurukul.com 91
92. Python : Modules : Packages :
__init__.py
When importing the package, Python searches through the
directories on sys.path looking for the package subdirectory.
The __init__.py 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, __init__.py can just be an empty file, but it
can also execute initialization code for the package.
www.opengurukul.com 92
93. Python : Modules : Packages :
import methods
Users 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)
www.opengurukul.com 93
94. Python : Modules : Packages :
import methods 2
Yet 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)
www.opengurukul.com 94
95. Python : Modules : Packages :
import * from package
from sound.effects import *
The import statement uses the following convention: if a
package’s __init__.py 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 __init__.py) and then imports
whatever names are defined in the package.
It is considered bad practice in production code.
www.opengurukul.com 95
96. Python : Modules : Packages : Intra-
package References
Supports explicit relative imports with the from module import
name
The 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
www.opengurukul.com 96
98. Python : IO : str & repr
Convert any value to string : Example :
str() and repr() >>> s = 'hello, worldn'
The str() function is meant to >>> str(s)
return representations of 'hello, worldn'
values which are fairly
human-readable. >>> repr(s) # add quote and
backslash
The 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'
>>>
www.opengurukul.com 98
99. Python : IO : methods
The 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 str.center().
These 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'
>>>
www.opengurukul.com 99
100. Python : IO : rjust : Example
Example : 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 1
10 100 1000 4 16 64
>>> 7 49 343
Note: It is left justified by default. 10 100 1000
>>>
NOTE:
www.opengurukul.com The trailing comma on lines. 100
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
>>>
www.opengurukul.com 101
102. Python : IO : str.format() : colon
An optional ':' and format specifier can Example :
follow the field name.
>>> import math
Passing an integer after the ':' will
cause that field to be a minimum >>> print ' PI : {}' .format(math.pi)
number of characters wide. PI : 3.14159265359
The ':' 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)
>>>
www.opengurukul.com 102
103. Python : IO : str.format() : old c style
formatting
The % operator can also be used for Example :
string formatting.
>>> import math
It 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.
www.opengurukul.com 103
104. Python : IO : Files : open
The 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.
www.opengurukul.com 104
105. Python : IO : Files : open : Example
Example :
>>> f = open('/tmp/workfile', 'w')
>>> print f
<open file '/tmp/workfile', mode 'w' at 0xb7700c28>
>>>
www.opengurukul.com 105
106. Python : IO : Files : Methods : read
To read a file’s contents, call
f.read(size), which reads some
Example :
quantity of data and returns it as a
string. >>> f.read()
The size is an optional numeric
argument.
'This is the entire file.n'
When size is omitted or negative, the >>> f.read()
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, f.read() will return an
empty string ("").
www.opengurukul.com 106
107. Python : IO : Files : Methods :
readline
f.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.n'
The 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 filen'
This 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.
www.opengurukul.com 107
108. Python : IO : Files : Methods :
readlines
The 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
>>>
www.opengurukul.com 108
109. Python : IO : Files : Methods : write
The 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 first
To 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)
www.opengurukul.com 109
110. Python : IO : Files : Methods : seek
and tell
The 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
f.seek(offset, from_what). >>> f.seek(5) # 6th byte in a file
The position is computed from adding >>> f.read(1)
offset to a reference point; the
reference point is selected by the '5'
from_what argument.
>>> f.seek(-3, 2) # 3rd byte before
0 : from the beginning of the file end
1 : uses the current file position >>> f.read(1)
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. www.opengurukul.com 110
111. Python : IO : Files : Methods : close
When you’re done with a Example :
file, call f.close() to close >>> f.close()
it and free up any system >>> f.read()
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.
www.opengurukul.com 111
112. Python : IO : Files : with keyword
It is good practice to use Example :
the with keyword when >>> with open('/tmp/workfile', 'r') as f:
dealing with file objects. ... read_data = f.read()
This 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:
www.opengurukul.com 112
113. Python : IO : Files : pickle
Strings 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.
www.opengurukul.com 113
114. Python : IO : Files : pickle : Example
If 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)
www.opengurukul.com 114
115. Python : IO : Keyboard : input and
raw_input
raw_input() : Example :
Python 3 : doesn't exist >>> x = input()
Python 2 : returns a string. 2*6
input():
>>> 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*6
Since getting a string was almost
always what we wanted, Python >>> x
3 does that with input(). '2 * 6'
If you ever want the old behavior,
>>>
eval(input()) works.
www.opengurukul.com 115
117. Python : Errors : Syntax Errors
Syntax 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 1
The 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 syntax
The 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. >>>
www.opengurukul.com 117
118. Python : Exceptions : Exceptions
Even 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 zero
The rest of line provides detail based on
the type of exception and what caused >>>
www.opengurukul.com 118
it.
119. Python : Exceptions : Exceptions :
Example
Example :
>>> 4 + spam*3
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects
>>>
www.opengurukul.com 119
120. Python : Exceptions : Handling
Exceptions
It 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 statements
The 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.
www.opengurukul.com 120
121. Python : Exceptions : Handling
Exceptions : Example
$ cat try.py Example :
while True:
$ python try.py
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
$ $
www.opengurukul.com 121
122. Python : Exception : Handling
Exceptions : Multiple Exceptions
A try statement may have more than one Example :
except clause, to specify handlers for
$ cat try_multi_except.py
different exceptions.
import sys
At 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
$
www.opengurukul.com 122
123. Python : Exception : Handling
Exceptions : else clause
The try ... except statement has Syntax :
an optional else clause,
try :
which, when present, must
follow all except clauses. try block
It is useful for code that must except : # one or more blocks
be executed if the try clause
does not raise an exception. except block
The 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 www.opengurukul.com 123
statement.
124. Python : Exception : Handling
Exceptions : else clause : Example
Example : Output :
$ cat try_else.py $ python try_else.py /etc/passwd
/etc/passwd1
import sys
/etc/passwd has 57 lines
for 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()
$ www.opengurukul.com 124
125. Python : Exception : Handling
Exceptions : Exception Arguments
When 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.
www.opengurukul.com 125
126. Python : Exception : Handling Exceptions :
Exception Arguments : Example
Example : Output :
$ cat try_raise.py
$ python try_raise.py
try:
<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
$ www.opengurukul.com 126
127. Python : Exception : Handling
Exceptions : Indirectly
Exception handlers don’t just Example : # example : try_func.py
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 try_func.py
Handling run-time error: integer
division or modulo by zero
www.opengurukul.com 127
128. Python : Exceptions : Raising
Exceptions
The raise statement allows the Example : # try_re_raise.py
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.py
If 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 "try_re_raise.py", line 2, in <module>
statement allows you to re-raise
raise NameError('HiThere')
the exception:
NameError: HiThere
www.opengurukul.com 128
$
129. Python : Exception : User-Defined
Exceptions
Programs may name their own Example : try_user.py
exceptions by creating a new class MyError(Exception): # subclass
exception class. def __init__(self, value): # constructor
Exceptions 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 try_user.py
exception. My exception occurred, value: 4
www.opengurukul.com 129
$
130. Python : Exception : Define Clean-
up Actions : finally
The 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.
www.opengurukul.com 130
131. Python : Exception : Define Clean-
up Actions : finally
Program : Output :
$ cat try_finally.py >>> 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"
$ www.opengurukul.com 131
132. Python :Exception : Pre-Defined
Clean-up Actions
Some 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 while
Other objects which provide processing the lines.
predefined clean-up actions will
indicate it in documentation.
www.opengurukul.com 132
134. Python : Classes : Scopes &
Namespaces : Example
Example : 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
www.opengurukul.com scope: global spam 134
print("After local assignment:", spam)
135. Python : Classes
class name:
"documentation"
statements
-or-
class name(base1, base2, ...):
...
Most, statements are method definitions:
def name(self, arg1, arg2, ...):
...
May also be class variable assignments
www.opengurukul.com 135
136. Python : Classes : Example
class 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
www.opengurukul.com 136
137. Python : Classes : Using Classes
To 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() # -> 1
x.push(1) # [1]
x.empty() # -> 0
x.push("hello") # [1, "hello"]
x.pop() # -> "hello" # [1]
To inspect instance variables, use dot notation:
x.items # -> [1]
www.opengurukul.com 137
138. Python : Classes : Subclassing
class 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]
www.opengurukul.com 138
139. Python : Classes : Subclassing 2
class 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
www.opengurukul.com 139
140. Python : Classes : Class/Instance
Variable : Example
class Connection:
verbose = 0 # class variable
def __init__(self, host):
self.host = host # instance variable
def debug(self, v):
self.verbose = v # make instance variable!
def connect(self):
if self.verbose: # instance variable
print "connecting to", self.host
www.opengurukul.com 140
142. Python : Classes : Instance Variable
Rules
On use via instance (self.x), search order:
(1) instance, (2) class, (3) base classes
this also works for method lookup
On assignment via instance (self.x = ...):
always makes an instance variable
Class variables "default" for instance variables
mutable class variable: one copy shared by all
mutable instance variable: each instance its own
www.opengurukul.com 142