Python - Basic and Intermediate Level Modules
Python - Basic and Intermediate Level Modules
2.0 Python Basics
IDLE will be installed, along with basic python in Windows. In Linux and Unix, it can be installed manually. IDLE
is a python IDE, from Python. Python commands can be executed using, either:
1. Interactive Mode, or
2. Script Mode
Individual commands can be executed in executed in interactive mode. Script mode is preferred for write a
program.
In script mode, >>> indicates the prompt of the python interpreter.
Programming in Python:
1. Interactive Mode Programming
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v.1500
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
2. Script Mode Programming
$ python [Link]
#!/usr/bin/python
print "Hello, World!"
$ chmod +x [Link]
$ ./[Link]
2.1 Basic Syntax and Indenting
In [1]: a = 12
In [2]: print a
12
In [3]: b = 34
In [4]: print "b = ", b
b = 34
[Link] 1/338
13/11/2016 python Basic and Intermediate Level Modules
>>> a=12
File "<stdin>", line 1
a=12
^
IndentationError: unexpected indent
In [6]: for i in [1,2,335]:
print i
File "<ipython‐input‐6‐4fc7dc342d36>", line 2
print i
^
IndentationError: expected an indented block
In [5]: for i in [1,2,335]:
print i
1
2
335
In [8]: if True:
print "Something"
else:
print "Nothing"
File "<ipython‐input‐8‐e309279ca877>", line 4
print "Nothing"
^
IndentationError: expected an indented block
In [7]: if True:
print "Something"
else:
print "Nothing"
Something
So, ensure that indentation is provided whenever it is needed, and avoid undesired indendations. Python
Program works based on indentation.
PEP 8 is a python group for coding style. It recommends 4 spaces as indentation. Also, they recommend to
prefer spaces, to tabs. If any one is interested in using tabs, ensure that the tab space is configured to 4
spaces, in settings of your editor or IDE.
Also, there should be consistency of intendation, throughtout the program.
[Link] 2/338
13/11/2016 python Basic and Intermediate Level Modules
2.2 Identifier Naming Conventions
Identifier can represent an object, including variables, classes, functions, execption, ...
For Identifiers, first character must be an alphabet (A to Z, a to z) or underscore
(_)
From second character onwards, it can be alpha‐numeric (A to Z, a to z, 0 to 9) and
underscore (_) character.
Ex: animal, _animal, animal123, ani123mal, ani_mal123, ani12ma_l3 are possible
Ex: 123animal, animal&, $animal, ani$mal, 0animal are not possible. (All these resu
lt in SyntaxError)
And, comma(,), dot(.), % operators are defined in python
Naming Conventions
‐ Class names start with an uppercase letter. All other identifiers start w
ith a lowercase letter.
‐ PRIVATE identifiers start with single underscore
ex: _identierName
‐ STRONGLY PRIVATE identifiers start with two leading underscores.
ex: __identifierName
‐ Language defined Special Names ‐ identifier with starts and ends with two
underscores
ex: __init__, __main__, __file__
Python is casesensitive language. This casesensitivity can be removed using advanced settings, but it is
strongly not recommended.
In [9]: animal = "Cat"
Animal = "Cow"
In [10]: print animal
Cat
In [11]: print Animal
Cow
[Link] 3/338
13/11/2016 python Basic and Intermediate Level Modules
Identifier casing is of twotypes:
1. snake casing
ex: cost_of_mangos
2. Camel casing
ex: costOfMangos
PEP 8 recommends to follow any one of them, but, only one type of them in a project.
NOTE: In all the following exercises and examples, Camel casing will be followed.
Comment operator
# comment Operator
Interpretor ignore the line, right to this operator
The is only line comment, in python.
Docstrings
''' '''
""" """
In [13]: print '''
These are not multi‐line comments, but
are called docstrings.
docstrinsg will be processed by the interpreter.
triple double quotes will also work as docstrings.
'''
These are not multi‐line comments, but
are called docstrings.
docstrinsg will be processed by the interpreter.
triple double quotes will also work as docstrings.
In [14]: '''
These are not multi‐line comments, but
are called docstrings.
docstrinsg will be processed by the interpreter.
triple double quotes will also work as docstrings.
'''
Out[14]: '\n These are not multi‐line comments, but\n are called docstrings.\n
docstrinsg will be processed by the interpreter.\n triple double quotes
will also work as docstrings.\n'
[Link] 4/338
13/11/2016 python Basic and Intermediate Level Modules
Quotes
‐ single ('apple' , "mango"), and triple quotes ('''apple''', """mango""")
‐ Triple quotes are generally used for docstrings
‐ Double quotes are NOT allowed. Don't be confused.
‐ quotes are used in defining strings
‐ words, sentences, paragraphs
MultiLine Statements
‐ \ Line continuation operator. (Also, used as reverse division operator)
In [15]: SomeOperation = 12+34‐ 1342342 + 23454545 + 3123 + \
3455 ‐ 3454 ‐ 3454 ‐ \
234
In [16]: print "SomeOperation = ", SomeOperation
sum = 22111685
Statements used within [], {}, or () doesn't need Line continuation operator
In [17]: months = ('Jan', 'Feb', 'Mar',
'Apr', 'May', 'Jun',
'jul', 'Aug')
In [18]: print months
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'jul', 'Aug')
Mutiple Statements in a line
‐ ; operator is used to separate statements
In [1]: a = 12
b = 34
c = a + b
print "c = ", c
c = 46
[Link] 5/338
13/11/2016 python Basic and Intermediate Level Modules
In [2]: a = 12; b = 34; c = a + b; print "c = ", c # there are 4 statements in th
is line
c = 46
2.3 Reserved Keywords in Python:
Reserved Keywords (27 in python 2.x)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
and assert break class continue def
del
elif else except exec finally for
from
global if import in is lambda
not
or pass print raise return try
while
yield
Reserved Keywords (33 in python 3.x)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
These reserved keywords should not be used for the names of userdefined identifiers.
[Link] 6/338
13/11/2016 python Basic and Intermediate Level Modules
Builtin Functions(64)
abs() divmod() input() open() staticmethod()
all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reversed() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set()
delattr() help() next() setattr()
dict() hex() object() slice()
dir() id() oct() sorted()
In [21]: a = 12
print type(a) # type() returns the type of the object.
<type 'int'>
In [22]: print type(type)
<type 'type'>
In [23]: print id(a) # returns the address where object 'a' is stored
40662740
In [24]: print(a) # print() function is different from print statement
12
In [25]: print(dir(a)) # returns the attributes and methods associated with the object
'a'
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__de
lattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '_
_format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__i
ndex__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mo
d__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '_
_pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__red
uce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod_
_', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub_
_', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__su
b__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_lengt
h', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
[Link] 7/338
13/11/2016 python Basic and Intermediate Level Modules
In [26]: help(a) # returns information and usage about the specified object, or funct
ion, ..
[Link] 8/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 9/338
13/11/2016 python Basic and Intermediate Level Modules
Help on int object:
class int(object)
| int(x=0) ‐> int or long
| int(x, base=10) ‐> int or long
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is floating point, the conversion truncates towards zer
o.
| If x is outside the integer range, the function returns a long instead.
|
| If x is not a number or if base is given, then x must be a string or
| Unicode object representing an integer literal in the given base. The
| literal can be preceded by '+' or '‐' and be surrounded by whitespace.
| The base defaults to 10. Valid bases are 0 and 2‐36. Base 0 means to
| interpret the base from the string as an integer literal.
| >>> int('0b100', base=0)
| 4
|
| Methods defined here:
|
| __abs__(...)
| x.__abs__() <==> abs(x)
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __and__(...)
| x.__and__(y) <==> x&y
|
| __cmp__(...)
| x.__cmp__(y) <==> cmp(x,y)
|
| __coerce__(...)
| x.__coerce__(y) <==> coerce(x, y)
|
| __div__(...)
| x.__div__(y) <==> x/y
|
| __divmod__(...)
| x.__divmod__(y) <==> divmod(x, y)
|
| __float__(...)
| x.__float__() <==> float(x)
|
| __floordiv__(...)
| x.__floordiv__(y) <==> x//y
|
| __format__(...)
|
| __getattribute__(...)
| x.__getattribute__('name') <==> [Link]
|
| __getnewargs__(...)
|
| __hash__(...)
| x.__hash__() <==> hash(x)
[Link] 10/338
13/11/2016 python Basic and Intermediate Level Modules
|
| __hex__(...)
| x.__hex__() <==> hex(x)
|
| __index__(...)
| x[y:z] <==> x[y.__index__():z.__index__()]
|
| __int__(...)
| x.__int__() <==> int(x)
|
| __invert__(...)
| x.__invert__() <==> ~x
|
| __long__(...)
| x.__long__() <==> long(x)
|
| __lshift__(...)
| x.__lshift__(y) <==> x<<y
|
| __mod__(...)
| x.__mod__(y) <==> x%y
|
| __mul__(...)
| x.__mul__(y) <==> x*y
|
| __neg__(...)
| x.__neg__() <==> ‐x
|
| __nonzero__(...)
| x.__nonzero__() <==> x != 0
|
| __oct__(...)
| x.__oct__() <==> oct(x)
|
| __or__(...)
| x.__or__(y) <==> x|y
|
| __pos__(...)
| x.__pos__() <==> +x
|
| __pow__(...)
| x.__pow__(y[, z]) <==> pow(x, y[, z])
|
| __radd__(...)
| x.__radd__(y) <==> y+x
|
| __rand__(...)
| x.__rand__(y) <==> y&x
|
| __rdiv__(...)
| x.__rdiv__(y) <==> y/x
|
| __rdivmod__(...)
| x.__rdivmod__(y) <==> divmod(y, x)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
[Link] 11/338
13/11/2016 python Basic and Intermediate Level Modules
|
| __rfloordiv__(...)
| x.__rfloordiv__(y) <==> y//x
|
| __rlshift__(...)
| x.__rlshift__(y) <==> y<<x
|
| __rmod__(...)
| x.__rmod__(y) <==> y%x
|
| __rmul__(...)
| x.__rmul__(y) <==> y*x
|
| __ror__(...)
| x.__ror__(y) <==> y|x
|
| __rpow__(...)
| y.__rpow__(x[, z]) <==> pow(x, y[, z])
|
| __rrshift__(...)
| x.__rrshift__(y) <==> y>>x
|
| __rshift__(...)
| x.__rshift__(y) <==> x>>y
|
| __rsub__(...)
| x.__rsub__(y) <==> y‐x
|
| __rtruediv__(...)
| x.__rtruediv__(y) <==> y/x
|
| __rxor__(...)
| x.__rxor__(y) <==> y^x
|
| __str__(...)
| x.__str__() <==> str(x)
|
| __sub__(...)
| x.__sub__(y) <==> x‐y
|
| __truediv__(...)
| x.__truediv__(y) <==> x/y
|
| __trunc__(...)
| Truncating an Integral returns itself.
|
| __xor__(...)
| x.__xor__(y) <==> x^y
|
| bit_length(...)
| int.bit_length() ‐> int
|
| Number of bits necessary to represent self in binary.
| >>> bin(37)
| '0b100101'
| >>> (37).bit_length()
| 6
[Link] 12/338
13/11/2016 python Basic and Intermediate Level Modules
|
| conjugate(...)
| Returns self, the complex conjugate of any int.
|
| ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
| Data descriptors defined here:
|
| denominator
| the denominator of a rational number in lowest terms
|
| imag
| the imaginary part of a complex number
|
| numerator
| the numerator of a rational number in lowest terms
|
| real
| the real part of a complex number
|
| ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
| Data and other attributes defined here:
|
| __new__ = <built‐in method __new__ of type object>
| T.__new__(S, ...) ‐> a new object with type S, a subtype of T
2.4 Arithmetic Operations
Arithmetic Operators:
+ ‐ * / \ % ** // =
PEP 8 recommends to place one space around the operator
In [27]: var1 = 123
var2 = 2345
Addition
In [28]: print var1+var2
2468
In [29]: var3 = 23.45
In [30]: print type(var1), type(var2), type(var3)
<type 'int'> <type 'int'> <type 'float'>
[Link] 13/338
13/11/2016 python Basic and Intermediate Level Modules
In [31]: print var1+var3 # type‐casting takes place # int + float = float
146.45
In [35]: var4 = 453453454534545435345435345345345454357090970970707
In [36]: print var4
453453454534545435345435345345345454357090970970707
In [37]: var4 # observe the 'L' in the end
Out[37]: 453453454534545435345435345345345454357090970970707L
In [34]: print type(var4)
<type 'long'>
Assignment 1: what is the largest number that can be computed in python?
In [39]: print var2+var4 # int + long int
453453454534545435345435345345345454357090970973052
In [40]: print var3 + var4 # float + long int
4.53453454535e+50
In [41]: print var2
2345
In [42]: var2 = 234.456 # overwrite the existing object; dynamic typing
print var2
234.456
In [43]: print type(var2)
<type 'float'>
subtraction
In [44]: print var1 ‐ var2
‐111.456
[Link] 14/338
13/11/2016 python Basic and Intermediate Level Modules
In [45]: print var2 ‐ var4
‐4.53453454535e+50
Assignment 2: what is the smallest number that can be computed in python?
Multiplication
In [46]: print var1*var2 # int * int
28838.088
In [47]: print var1*var2 # int * float
28838.088
In [49]: print var1, var2
123 234.456
In [50]: print var3, var4
23.45 453453454534545435345435345345345454357090970970707
In [51]: var5 = 23
In [52]: print var1*var5 # int * int
2829
In [53]: print var1 * var4 # int * long int
55774774907749088547488547477477490885922189429396961
Division Operation
Division is different in python 2.x and python 3.x
/ division operator
// floor division operator
\ reverse division (deprecated). It is no more used.
In [54]: 10/5
Out[54]: 2
[Link] 15/338
13/11/2016 python Basic and Intermediate Level Modules
In [55]: 10/2
Out[55]: 5
In [56]: 10/3
Out[56]: 3
In [57]: 10//3
Out[57]: 3
In [58]: 10/3.0 # true division in python 2
Out[58]: 3.3333333333333335
In python3, 10/3 will give true division
In [60]: 2/10
Out[60]: 0
\ reverse division operator got deprecated
In [61]: 2\10
File "<ipython‐input‐61‐1bdd425914b1>", line 1
2\10
^
SyntaxError: unexpected character after line continuation character
In [62]: 10/3 # int/int = int
Out[62]: 3
In [63]: 10/3.0 # int/float = float
Out[63]: 3.3333333333333335
In [64]: 10.0/3 # float/int = float
Out[64]: 3.3333333333333335
In [65]: 10.0/3.0 # float/float = float
Out[65]: 3.3333333333333335
In [66]: float(3) # float() is a built‐in function, used to convert to floating‐point
value
Out[66]: 3.0
[Link] 16/338
13/11/2016 python Basic and Intermediate Level Modules
In [67]: 10/float(3)
Out[67]: 3.3333333333333335
In [68]: 2/10
Out[68]: 0
In [69]: 2.0/10
Out[69]: 0.2
In [70]: 2.0//10
Out[70]: 0.0
In [71]: 5/2.0
Out[71]: 2.5
In [72]: 5//2.0
Out[72]: 2.0
In [74]: 5//2 #float division will convert to floor(), after division
Out[74]: 2
power operation
** power operator
pow() builtin function
In [75]: 2 ** 3
Out[75]: 8
In [76]: 3**100
Out[76]: 515377520732011331036461129765621272702107522001L
In [77]: pow(2,3)
Out[77]: 8
In [78]: pow(4,0.5) # square root
Out[78]: 2.0
[Link] 17/338
13/11/2016 python Basic and Intermediate Level Modules
In [82]: pow(4,0.652)
Out[82]: 2.469125213787077
In [83]: 4**0.652
Out[83]: 2.469125213787077
In [79]: print var1, var2
123 234.456
In [80]: var1**var2
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
OverflowError Traceback (most recent call last)
<ipython‐input‐80‐e9b276a0d761> in <module>()
‐‐‐‐> 1 var1**var2
OverflowError: (34, 'Result too large')
In [81]: pow(var1, var2)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
OverflowError Traceback (most recent call last)
<ipython‐input‐81‐4eb667228a30> in <module>()
‐‐‐‐> 1 pow(var1, var2)
OverflowError: (34, 'Result too large')
exponent operation
In [87]: 1e10
Out[87]: 10000000000.0
In [88]: 1e1 # equal to 1.0 * 10 **1
Out[88]: 10.0
In [89]: 1 * 10 **1
Out[89]: 10
In [90]: 1.0 * 10 **1
Out[90]: 10.0
Modulo Operations
[Link] 18/338
13/11/2016 python Basic and Intermediate Level Modules
In [ ]: 0%3, 1%3, 2%3, 3%3, 4%3, 5%3, 6%3, 7%3, 8%3, 9%3, 10%3
Observe that there are 3 elements repeating (0, N1) where N is modulo divisor.
You can take the analogy of an Analog clock. After it completes 12 hours, it starts again from 0
In [94]: 0%12, 1%12, 11%12
Out[94]: (0, 1, 11)
In [95]: 12%12
Out[95]: 0
In [96]: 14%12 # 2 past noon
Out[96]: 2
In [97]: 16.45%12 # Observe the output precision
Out[97]: 4.449999999999999
In [105]: ‐18%12 # Observe the +ve sign in the result
Out[105]: 6
In [106]: ‐18%‐12
Out[106]: ‐6
In [107]: 18%‐12 # conclusion: sign of modulo number is reflected
Out[107]: ‐6
Complex Numbers
Complex Number = Real Number +/ Imaginary Number
In python, 'j' is used to represent the imaginary number.
In [108]: print 2+3j
(2+3j)
In [109]: n1 = 2+3j
In [110]: print type(n1)
<type 'complex'>
[Link] 19/338
13/11/2016 python Basic and Intermediate Level Modules
In [111]: n2 = 3‐2j
print "n2 = ", n2, " type(n2) = ", type(n2)
n2 = (3‐2j) type(n2) = <type 'complex'>
In [112]: print n1, n1 + n2, n1‐n2
(2+3j) (5+1j) (‐1+5j)
In [113]: print n1*n1, pow(n1,2), n1/n1
(‐5+12j) (‐5+12j) (1+0j)
In [114]: print n1, [Link]() # Observe the signs of imaginary numbers
(2+3j) (2‐3j)
In [115]: print n1, [Link], [Link]
(2+3j) 2.0 3.0
In [116]: n2 = 2.0‐3.45j
In [117]: print n2, [Link], [Link]
(2‐3.45j) 2.0 ‐3.45
In [118]: 4j
Out[118]: 4j
NOTE: 4j, j4, j4 are not possible. In these cases, interpreter treats 'j' as a variable.
In [119]: print n1+n2, n1‐n2, ‐n1+n2, ‐n1‐n2
(4‐0.45j) 6.45j ‐6.45j (‐4+0.45j)
In [120]: print n1*[Link], (n1*n2).real
(4+6j) 14.35
In [121]: print n1, n2, [Link]+[Link] # [Link] is resulting in a real value
(2+3j) (2‐3.45j) ‐1.45
In [122]: complex(2,‐3.456) # complex() ‐ Builtin function
Out[122]: (2‐3.456j)
[Link] 20/338
13/11/2016 python Basic and Intermediate Level Modules
.* (of Matlab) operator is not valid in python. Elementwise multiplication is possible with numpy module. floor
division is also not valid on complex type data.
In [123]: (3+4j) == (4j+3) # == checks value equivalence
Out[123]: True
In [124]: (3+4j) is (4j+3) # is ‐ checks object level (both value and address)
Out[124]: False
In [126]: print n1, abs(n1) # abs ‐ absolute function, Builtin function.
(2+3j) 3.60555127546
abs()
Builtin function, to return the absolute value
If a is positive real integer,
abs(a) = a
abs(‐a) = a
abs((a+bj)) is equal to [Link](pow(a,2), pow(b,2))
In [128]: print abs(3)
3
In [129]: print abs(‐3)
3
In [130]: print abs(3+4j)
5.0
In [131]: import math
print [Link](3**2+4**2)
5.0
In [132]: divmod(12+2j, 2‐3j) # divmod(x,y) returns x//y, x%y
c:\python27\lib\site‐packages\ipykernel\__main__.py:1: DeprecationWarning: co
mplex divmod(), // and % are deprecated
if __name__ == '__main__':
Out[132]: ((1+0j), (10+5j))
[Link] 21/338
13/11/2016 python Basic and Intermediate Level Modules
Compound(mixed) Operations
+=, =, *=, **=
In [133]: a = 12
print a, type(a)
12 <type 'int'>
In [134]: a = a + 1 ; print "a = ", a # ; is used to write multiple statement in same
line
a = 13
In [135]: a += 1 ; print "a = ", a
a = 14
In [136]: a ‐= 1 ; print "a = ", a # a = a ‐1
a = 13
In [137]: a *= 2 ; print "a = ", a # a = a*2
a = 26
In [138]: a /= 2 ; print "a = ", a # a = a / 2
a = 13
In [139]: a **= 2 ; print "a = ", a # a = a ** 2
a = 169
In [140]: a %= 100 ; print "a = ", a
a = 69
In [141]: a = 23; a//=2; print "a = ", a
a = 11
In [142]: a<<=1; print "a = ", a #left‐shift
a = 22
at binary level 128 64 32 16 8 4 2 1
1 0 1 1
<<1 0 0 0 1 0 1 1 0
[Link] 22/338
13/11/2016 python Basic and Intermediate Level Modules
In [144]: a>>=1; print "a = ", a #right‐shift
a = 11
In [145]: a^=2; print "a = ", a # bitwise XOR operation
a = 9
In [146]: a|=2; print "a = ", a # bitwise OR operation
a = 11
NOTE: Pre and Post increment/ decrements (++a, a++, a, a) are not valid in Python
Working in Script Mode
In [91]: #!/usr/bin/python
# This is called shebong line
# [Link]
print "Hello World!"
Hello World!
[Link] 23/338
13/11/2016 python Basic and Intermediate Level Modules
In [92]:
[Link] 24/338
13/11/2016 python Basic and Intermediate Level Modules
#!/usr/bin/python
# [Link]
# This hash/pound is the comment operator, used for
# both single line and multi‐line comments.
# comment line will be ignored by interpreter
'''
These are not multi‐line comments, but
are called docstrings.
docstrinsg will be processed by the interpreter.
triple double quotes will also work as docstrings.
'''
#either single, single or double quotes, can be used for strings
costOfMango = 12
print "cost Of Each Mango is ", costOfMango
costOfApple = 40
print "cost Of Each Apple is ", costOfApple
# what is the cost of dozen apples and two dozens of mangos
TotalCost = 12* costOfApple + 2*12* costOfMango
print "Total cost is ", TotalCost
# print is a statement in python 2, and is a function call in python 3
# now, python 2 is supporting both
print "Hello World!"
print("Hello World!")
# by default, print will lead to display in next line
print "This is", # , after print will suppress the next line
# but, a space will result
print "python class"
# PEP 8 recommends to use only print statement or function call throughtout th
e project
# ; semicolon operator
# It is used as a statement separator.
name = 'yash'
print 'My name is ', name
name = 'yash'; print 'My name is ', name
[Link] 25/338
13/11/2016 python Basic and Intermediate Level Modules
print "who's name is ", name, '?'
print "'"
print '"'
print '\''
print "''' '''"
print '""'
print "''"
print ''' """ """ '''
print """ ''' ''' """
[Link] 26/338
13/11/2016 python Basic and Intermediate Level Modules
cost Of Each Mango is 12
cost Of Each Apple is 40
Total cost is 768
Hello World!
Hello World!
This is python class
My name is yash
My name is yash
who's name is yash ?
'
"
'
''' '''
""
''
""" """
''' '''
In [93]: #!/usr/bin/python
# [Link]
# Operator precedence in python
# It follows PEMDAS rule, and left to right, and top to bottom
# P ‐ Paranthesis
# E ‐ Exponent
# M ‐ Multiplication
# D ‐ Division
# A ‐ Addition
# S ‐ Subtraction
#Every type of braces has importance in python
# {} ‐ used for dictionaries and sets
# [] ‐ used for lists
# () ‐ used of tuples; also used in arithmetic operations
result = (22+ 2/2*4//4‐89)
print result
‐66
Assignment 3: examine the operator precedence will other examples
[Link] 27/338
13/11/2016 python Basic and Intermediate Level Modules
IO Operations
In python 2.x, raw_input() and input() are two builtin functions used for getting runtime input.
raw_input() ‐ takes any type of runtime input as a string.
input() ‐ takes any type of runtime input originally without any type conversi
on.
NOTE: working with raw_input() requires us to use type converters to convert the da
ta into the required data type.
In Python 3.x, there is only input() function; but not raw_input(). The Job of raw_input() in python 2.x is done
by input() in python 3.x
In [1]: #!/usr/bin/python
# class3_io.py
'''
Purpose : demonstration of input() and raw_input()
'''
dataRI = raw_input('Enter Something: ')
dataI = input('Enter something: ')
print "dataRI = ", dataRI, " type(dataRI) = ", type(dataRI)
print "dataI = ", dataI, " type(dataI) = ", type(dataI)
Enter Something: 324
Enter something: 324
dataRI = 324 type(dataRI) = <type 'str'>
dataI = 324 type(dataI) = <type 'int'>
[Link] 28/338
13/11/2016 python Basic and Intermediate Level Modules
Analyzed outputs for various demonstrated cases:
>>>
==================== RESTART: C:/pyExercises/class3_io.py ====================
Enter Something: 123
Enter something: 123
123 <type 'str'>
123 <type 'int'>
>>>
==================== RESTART: C:/pyExercises/class3_io.py ====================
Enter Something: 'Yash'
Enter something: 'Yash'
'Yash' <type 'str'>
Yash <type 'str'>
>>>
==================== RESTART: C:/pyExercises/class3_io.py ====================
Enter Something: True
Enter something: True
True <type 'str'>
True <type 'bool'>
>>>
==================== RESTART: C:/pyExercises/class3_io.py ====================
Enter Something: Yash
Enter something: Yash
Traceback (most recent call last):
File "C:/pyExercises/class3_io.py", line 12, in <module>
dataI = input('Enter something: ')
File "<string>", line 1, in <module>
NameError: name 'Yash' is not defined
>>> dataRI
'Yash'
input() takes only qualified data as runtime input. Whereas raw_input() will qualify any data as a 'str' type
[Link] 29/338
13/11/2016 python Basic and Intermediate Level Modules
In [3]: #!/usr/bin/python
# class3_io1.py
'''
Purpose : demonstration of input() and raw_input()
'''
dataRI = int(raw_input('Enter a number: '))
dataI = input('Enter a number: ')
print "dataRI = ", dataRI, " type(dataRI) = ", type(dataRI)
print "dataI = ", dataI, " type(dataI) = ", type(dataI)
print "Sum of numbers is ", dataRI+dataI
Enter a number: 324
Enter a number: 324
dataRI = 324 type(dataRI) = <type 'int'>
dataI = 324 type(dataI) = <type 'int'>
Sum of numbers is 648
[Link] 30/338
13/11/2016 python Basic and Intermediate Level Modules
Analyzed outputs for various demonstrated cases:
=================== RESTART: C:/pyExercises/class3_io1.py ===================
Enter a number: 123
Enter a number: 123
123 <type 'str'>
123 <type 'int'>
>>>
=================== RESTART: C:/pyExercises/class3_io1.py ===================
Enter a number: 123
Enter a number: 123
123 <type 'str'>
123 <type 'int'>
Sum of numbers is
Traceback (most recent call last):
File "C:/pyExercises/class3_io1.py", line 19, in <module>
print "Sum of numbers is ", dateRI+dataI
NameError: name 'dateRI' is not defined
>>>
=================== RESTART: C:/pyExercises/class3_io1.py ===================
Enter a number: 123
Enter a number: 123
123 <type 'str'>
123 <type 'int'>
Sum of numbers is
Traceback (most recent call last):
File "C:/pyExercises/class3_io1.py", line 19, in <module>
print "Sum of numbers is ", dataRI+dataI
TypeError: cannot concatenate 'str' and 'int' objects
>>>
=================== RESTART: C:/pyExercises/class3_io1.py ===================
Enter a number: 123
Enter a number: 123
123 <type 'int'>
123 <type 'int'>
Sum of numbers is 246
>>>
String Operations
string data type can be representing using either single or double quotes
[Link] 31/338
13/11/2016 python Basic and Intermediate Level Modules
Creating a string
In [5]: s1 = 'Python Programming'
In [6]: s1
Out[6]: 'Python Programming'
In [7]: print s1
Python Programming
In [8]: print type(s1)
<type 'str'>
In [9]: s2 = "Django"
In [10]: print s2, type(s2)
Django <type 'str'>
In [11]: s3 = ''' python programming with Django '''
In [12]: print s3
python programming with Django
In [13]: print type(s3)
<type 'str'>
In [14]: s4 = """ python programming with Django """
In [15]: print s4
python programming with Django
In [16]: type(s4)
Out[16]: str
In [19]: print django1
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐19‐5701398af8ee> in <module>()
‐‐‐‐> 1 print django1
NameError: name 'django1' is not defined
[Link] 32/338
13/11/2016 python Basic and Intermediate Level Modules
In [20]: print 'django1'
django1
In [21]: s5 = '~!@#$%^&*()1232425'
In [22]: print s5, type(s5)
~!@#$%^&*()1232425 <type 'str'>
In [23]: s6 = str(123.34) # str() is a builtin function to convert to string
In [24]: print s6, type(s6)
123.34 <type 'str'>
In [25]: s7 = str(True)
In [26]: print s7, type(s7)
True <type 'str'>
Indexing
In [27]: print s1
Python Programming
In [28]: print len(s1) # len() is a bultin function to return the length of object
18
P y t h o n P r o g r a m m i n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ‐> forward indexing
‐5 ‐4 ‐3 ‐2 ‐1 ‐> Reverse indexing
In [29]: s1[0]
Out[29]: 'P'
In [30]: s1[6] # white‐space character
Out[30]: ' '
In [31]: s1[17]
Out[31]: 'g'
[Link] 33/338
13/11/2016 python Basic and Intermediate Level Modules
In [32]: s1[18]
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
IndexError Traceback (most recent call last)
<ipython‐input‐32‐8486803f392a> in <module>()
‐‐‐‐> 1 s1[18]
IndexError: string index out of range
NOTE:Indexing can be done from 0 through len(string)1
In [33]: s1[‐1]
Out[33]: 'g'
In [34]: s1[‐5]
Out[34]: 'm'
In [35]: s1[‐16]
Out[35]: 't'
In [36]: s1[‐18] == s1[0]
Out[36]: True
Interview Question 1: what is string[0]
In [37]: s1[‐0] # is equal to s1[0]
Out[37]: 'P'
String Slicing
In [39]: print s1
Python Programming
In [40]: s1[2:6]
Out[40]: 'thon'
In [41]: s1[2:8]
Out[41]: 'thon P'
In [42]: s1[:]
Out[42]: 'Python Programming'
[Link] 34/338
13/11/2016 python Basic and Intermediate Level Modules
In [43]: s1[:‐1]
Out[43]: 'Python Programmin'
In [44]: s1[‐5:‐1]
Out[44]: 'mmin'
In [45]: s1[‐5:17] # complex indexing
Out[45]: 'mmin'
In [46]: s1[::‐1]
Out[46]: 'gnimmargorP nohtyP'
In [47]: s1[::1]
Out[47]: 'Python Programming'
In [48]: s1[::2]
Out[48]: 'Pto rgamn'
In [49]: s1[::3]
Out[49]: 'Ph oai'
In [50]: s1[::4]
Out[50]: 'Poran'
In [51]: s1[4:9]
Out[51]: 'on Pr'
In [52]: s1[Link] # string[initialBound, finalBound, increment/decrement]
Out[52]: 'on Pr'
In [53]: s1[Link]‐1] # 4‐1 = 3 index 3 is not represented in this object
Out[53]: ''
NOTE: After all these alterations, the original string object will not change, until it is overwrited.
Mutability of Strings
String objects are immutable. They, can't be edited. Only way is to overwrite it
[Link] 35/338
13/11/2016 python Basic and Intermediate Level Modules
In [54]: print s1
Python Programming
In [55]: s1[3]
Out[55]: 'h'
In [56]: s1[3] = 'H'
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐56‐a32a26674f0e> in <module>()
‐‐‐‐> 1 s1[3] = 'H'
TypeError: 'str' object does not support item assignment
In [59]: s1 = "PyTHON PROGRAMMING" # object overwriting taken place
print s1
PyTHON PROGRAMMING
String attributes
In [61]: print dir(s1)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt
__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex_
_', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str_
_', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser',
'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtab
s', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'i
sspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partitio
n', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translat
e', 'upper', 'zfill']
In [62]: print s1
PyTHON PROGRAMMING
In [63]: print [Link]()
Python programming
In [64]: [Link]('m')
Out[64]: 0
[Link] 36/338
13/11/2016 python Basic and Intermediate Level Modules
In [65]: [Link]('M') # python is case‐ sensitive, and capitalize() created new obje
ct, without disturbing the original object
Out[65]: 2
In [66]: [Link]('ing') # endswith() returns the boolen result
Out[66]: False
In [67]: [Link]('ING')
Out[67]: True
In [68]: [Link]('Py')
Out[68]: True
In [69]: [Link]('P')
Out[69]: 0
In [70]: [Link]('THON')
Out[70]: 2
In [71]: [Link]('MM')
Out[71]: 13
In [72]: [Link]('M')
Out[72]: 13
In [73]: [Link]('THON')
Out[73]: 2
Interview Question: Difference between [Link]() and s1. index()? Also, 'rfind', 'rindex' attributes of string
objects
In [74]: s1
Out[74]: 'PyTHON PROGRAMMING'
In [75]: [Link]()
Out[75]: 'Python programming'
In [76]: [Link]()
Out[76]: 'python programming'
[Link] 37/338
13/11/2016 python Basic and Intermediate Level Modules
In [77]: [Link]()
Out[77]: 'PYTHON PROGRAMMING'
In [78]: [Link]()
Out[78]: 'Python Programming'
In [79]: [Link]()
Out[79]: 'pYthon programming'
Interview Question : what is the data type of result of [Link]()
In [80]: [Link](' ')
Out[80]: ['PyTHON', 'PROGRAMMING']
In [81]: [Link]('O')
Out[81]: ['PyTH', 'N PR', 'GRAMMING']
In [82]: s1
Out[82]: 'PyTHON PROGRAMMING'
In [83]: [Link]('N') # string to list conversion
Out[83]: ['PyTHO', ' PROGRAMMI', 'G']
In [84]: [Link]('r') # no splitting as there is no 'r' character, but 'R' characte
r in string s1
Out[84]: ['PyTHON PROGRAMMING']
In [85]: [Link]('y')
Out[85]: ['P', 'THON PROGRAMMING']
In [86]: len([Link]('y'))
Out[86]: 2
In [87]: ''.join([Link]('y')) # list to strinsg conversion
Out[87]: 'PTHON PROGRAMMING'
In [88]: '@'.join([Link]('y')) # delimiter can be placed
Out[88]: 'P@THON PROGRAMMING'
[Link] 38/338
13/11/2016 python Basic and Intermediate Level Modules
In [89]: [Link]('O')
Out[89]: ['PyTH', 'N PR', 'GRAMMING']
In [90]: '@'.join([Link]('O')) # Observe that 'O' is replaced by '@'. This is one
example of duck‐typing
Out[90]: 'PyTH@N PR@GRAMMING'
In [91]: s9 = '''
This is a good day!
Fall 7 times, raise 8!
This is a famous japanese quote.
'''
In [92]: print len(s9), s9
114
This is a good day!
Fall 7 times, raise 8!
This is a famous japanese quote.
In [93]: print 'IS'.join([Link]('is'))
ThIS IS a good day!
Fall 7 times, raISe 8!
ThIS IS a famous japanese quote.
In [94]: s1
Out[94]: 'PyTHON PROGRAMMING'
In [95]: [Link]()
Out[95]: False
In [96]: 'python'.isalpha()
Out[96]: True
In [97]: 'python programming'.isalpha() # As there is a space character also
Out[97]: False
In [98]: 'python'.isalnum()
Out[98]: True
In [99]: 'python123'.isalnum()
Out[99]: True
[Link] 39/338
13/11/2016 python Basic and Intermediate Level Modules
In [100]: 'python123 '.isalnum() # There is a white space character also
Out[100]: False
In [101]: 'python123'.isdigit()
Out[101]: False
In [102]: '123'.isdigit()
Out[102]: True
In [103]: 'python'.islower()
Out[103]: True
In [104]: 'python123$'.islower() # It ensures that there is no capital letter, only
Out[104]: True
In [105]: 'python123$ '.isspace()
Out[105]: False
In [106]: ' '.isspace()
Out[106]: True
In [107]: ''.isspace()
Out[107]: False
In [108]: 'python programming'.isupper()
Out[108]: False
In [109]: 'PYTHON'.isupper()
Out[109]: True
In [110]: 'PyTHoN'.isupper()
Out[110]: False
In [111]: s1
Out[111]: 'PyTHON PROGRAMMING'
In [112]: [Link]()
Out[112]: False
[Link] 40/338
13/11/2016 python Basic and Intermediate Level Modules
In [113]: ([Link]()).istitle()
Out[113]: True
In [114]: ' Python Programming '.rstrip()
Out[114]: ' Python Programming'
In [115]: ' Python Programming '.lstrip()
Out[115]: 'Python Programming '
In [116]: ' Python Programming '.strip() # removes whitespaces
Out[116]: 'Python Programming'
In [117]: ' Python Programming '.strip('ing')
Out[117]: ' Python Programming '
In [118]: ' Python Programming '.strip().strip('ing')
Out[118]: 'Python Programm'
In [119]: 'ad123da'.strip('a')
Out[119]: 'd123d'
In [120]: 'ad1a2a3ada'.strip('a') # middle characters will retain
Out[120]: 'd1a2a3ad'
In [121]: '01\t012\t0123\t01234'.expandtabs() # recognizes \t espace character
Out[121]: '01 012 0123 01234'
In [122]: '01012012301234'.expandtabs()
Out[122]: '01012012301234'
In [124]: #To get the website name, excluding the domain
'[Link]'.lstrip('www.').rstrip('.org')
Out[124]: 'python'
In [125]: '[Link]'.split('.')[1] # alternatively
Out[125]: 'python'
In [126]: word = 'Python'
In [127]: len(word)
Out[127]: 6
[Link] 41/338
13/11/2016 python Basic and Intermediate Level Modules
In [128]: [Link](6) # numbers less than len(word) doesn't show any affect
Out[128]: 'Python'
In [129]: [Link](7) # zeros will be prepended correspondingly
Out[129]: '0Python'
In [130]: [Link](len(word)+4)
Out[130]: '0000Python'
In [131]: '@'.join(([Link](len(word)+4).split('0'))) # filling with character '@'
Out[131]: '@@@@Python'
In [132]: 'Python\tProgramming\t'.expandtabs() # recognizes '\t', '\r' and '\r\t'; and
expands correspondingly whenever it finds '\t'
Out[132]: 'Python Programming '
In [133]: r'Python\tProgramming'.expandtabs()
Out[133]: 'Python\\tProgramming'
In [134]: 'Python\r\tProgramming'.expandtabs()
Out[134]: 'Python\r Programming'
In [135]: print 'Python\r\tProgramming'.expandtabs()
Programming
In [136]: 'Python\t\rProgramming'.expandtabs()
Out[136]: 'Python \rProgramming'
In [137]: print 'Python\t\rProgramming'.expandtabs()
Programming
In [138]: "python programming".partition(' ')
Out[138]: ('python', ' ', 'programming')
In [139]: "python programming".partition('o')
Out[139]: ('pyth', 'o', 'n programming')
In [140]: "python programming".partition('0') # 0 is not present
Out[140]: ('python programming', '', '')
[Link] 42/338
13/11/2016 python Basic and Intermediate Level Modules
In [141]: "python programming".partition('n')
Out[141]: ('pytho', 'n', ' programming')
In [142]: "python programming".rpartition('n')
Out[142]: ('python programmi', 'n', 'g')
In [143]: "python programming".partition('g')
Out[143]: ('python pro', 'g', 'ramming')
In [144]: "python programming".rpartition('n')
Out[144]: ('python programmi', 'n', 'g')
In [145]: "python programming language".partition('a') # observe 3 'a' characters
Out[145]: ('python progr', 'a', 'mming language')
In [146]: "python programming language".rpartition('a') # middle 'a' isn't preferred
Out[146]: ('python programming langu', 'a', 'ge')
Assignment : Try to practice the remaining attributes, in this order.
split splitlines rsplit
find rfind
index rindex
center ljust rjust
encode decode translate
2.7 String Operations
2.7.6 String Attributes
In [1]: myString = 'fall 7 times stand up 8!'
In [2]: len(myString)
Out[2]: 24
In [3]: [Link](len(myString)+1)
Out[3]: ' fall 7 times stand up 8!'
[Link] 43/338
13/11/2016 python Basic and Intermediate Level Modules
In [4]: [Link](len(myString)+2)
Out[4]: ' fall 7 times stand up 8! '
In [5]: [Link](len(myString)+5)
Out[5]: ' fall 7 times stand up 8! '
In [6]: [Link](len(myString)+5)
Out[6]: 'fall 7 times stand up 8! '
In [7]: [Link](len(myString)+5)
Out[7]: ' fall 7 times stand up 8!'
In [8]: [Link](len(myString)+5).rjust(len(myString)+5)
Out[8]: 'fall 7 times stand up 8! '
In [9]: myParagraph = "fall 7 times stand up 8!\nfall 7 times stand up 8!\nfall 7 time
s stand up 8!"
In [10]: print myParagraph
fall 7 times stand up 8!
fall 7 times stand up 8!
fall 7 times stand up 8!
In [11]: [Link]()
Out[11]: ['fall 7 times stand up 8!',
'fall 7 times stand up 8!',
'fall 7 times stand up 8!']
In [12]: [Link](2) # results in two \n 's
Out[12]: ['fall 7 times stand up 8!\n',
'fall 7 times stand up 8!\n',
'fall 7 times stand up 8!']
In [13]: [Link]('base64')
Out[13]: 'ZmFsbCA3IHRpbWVzIHN0YW5kIHVwIDgh\n'
In [14]: 'ZmFsbCA3IHRpbWVzIHN0YW5kIHVwIDgh\n'.decode('base64')
Out[14]: 'fall 7 times stand up 8!'
In [15]: [Link]('base64', 'strict') # 'strict' option results in raising Uni
codeError, for encoding errors
Out[15]: 'ZmFsbCA3IHRpbWVzIHN0YW5kIHVwIDgh\n'
[Link] 44/338
13/11/2016 python Basic and Intermediate Level Modules
In [16]: [Link]('utf_8', 'strict')
Out[16]: 'fall 7 times stand up 8!'
In [17]: unicode('fall 7 times stand up 8!')
Out[17]: u'fall 7 times stand up 8!'
In [18]: type('fall 7 times stand up 8!'), type(unicode('fall 7 times stand up 8!'))
Out[18]: (str, unicode)
Interview Question : what is the difference betweem [Link]() and in operator
In [19]: "c" in "abc" # in ‐ Membership check operator; returns the boolean(True/Fals
e)
Out[19]: True
In [20]: "abc".find("c")
Out[20]: 2
In [21]: "abc".find("d")
Out[21]: ‐1
In [22]: "d" in "abc"
Out[22]: False
In [23]: "d" not in "abc"
Out[23]: True
2.7.8 String Concatenation
In [24]: myString
Out[24]: 'fall 7 times stand up 8!'
In [25]: myNewString = "It's time to wake up!!"
In [26]: myString + myNewString
Out[26]: "fall 7 times stand up 8!It's time to wake up!!"
In [27]: ''.join([myString, myNewString])
Out[27]: "fall 7 times stand up 8!It's time to wake up!!"
[Link] 45/338
13/11/2016 python Basic and Intermediate Level Modules
In [28]: myString + "@@@" +myNewString
Out[28]: "fall 7 times stand up 8!@@@It's time to wake up!!"
In [29]: '@@@'.join([myString, myNewString])
Out[29]: "fall 7 times stand up 8!@@@It's time to wake up!!"
In [30]: myString + 1947
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐30‐5a193782e9b7> in <module>()
‐‐‐‐> 1 myString + 1947
TypeError: cannot concatenate 'str' and 'int' objects
In [31]: myString + str(1947)
Out[31]: 'fall 7 times stand up 8!1947'
In [32]: str(True) + myString
Out[32]: 'Truefall 7 times stand up 8!'
In [33]: result = myString + str(True)+ str(' ')+ str(123.45)+ str(' ~!@#$%^&*')
print type(result), result
<type 'str'> fall 7 times stand up 8!True 123.45 ~!@#$%^&*
2.7.9 String Formating
Accessing Arguments by position
In [34]: '{}, {} and {}'.format('a', 'b', 'c')
Out[34]: 'a, b and c'
In [35]: '{}, {} and {}'.format('b', 'b', 'c')
Out[35]: 'b, b and c'
In [36]: '{0}, {1} and {2}'.format('a', 'b', 'c')
Out[36]: 'a, b and c'
In [37]: '{0}, {2} and {2}'.format('a', 'b', 'c')
Out[37]: 'a, c and c'
[Link] 46/338
13/11/2016 python Basic and Intermediate Level Modules
In [38]: str1 = 'Naseer'
str2 = 'Shoban'
str3 = 'Shankar'
print "The class is organized by {0}, and attended by {1} and
{2}".format(str1, str2, str3)
The class is organized by Naseer, and attended by Shoban and Shankar
In [39]: print "The class is organized by {2}, and attended by {0} and
{1}".format(str1, str2, str3)
The class is organized by Shankar, and attended by Naseer and Shoban
In [40]: '{2}, {1}, {0}'.format(*'abc') # Tuple unpacking
Out[40]: 'c, b, a'
In [41]: '{0}, {1}, {2}'.format(*'abc')
Out[41]: 'a, b, c'
accessing arguments by name
In [42]: 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='‐1
15.81W')
Out[42]: 'Coordinates: 37.24N, ‐115.81W'
In [43]: print "The class is organized by {organizer}, and attended by {std1} and {std
2}".format(organizer = str3, std2 = str2, std1 = str1)
The class is organized by Shankar, and attended by Naseer and Shoban
2.7.10 str() vs repr() | %s vs %r
Interview Question : what is the difference between str() and repr() ?
In [44]: str9 = "C:\newFolder"
In [45]: print str9
C:
ewFolder
In [46]: str10 = "C:\\newFolder" # to escape the \ character
In [47]: print str10
C:\newFolder
[Link] 47/338
13/11/2016 python Basic and Intermediate Level Modules
In [48]: str11 = r"C:\newFolder" #raw string representation
In [49]: print str11
C:\newFolder
In [50]: type(str9), type(str10), type(str11)
Out[50]: (str, str, str)
repr() is representational, but results in string data type.
Interview Question : In which cases, repr() is preferred to str()?
str() is comfortable for Humans; whereas repr() is for the machine
In [51]: "repr() shows quotes: {!r}; str() doesn't : {!s}".format('Shoban', 'Shoban')
Out[51]: "repr() shows quotes: 'Shoban'; str() doesn't : Shoban"
In [52]: str("udhay")
Out[52]: 'udhay'
In [53]: repr("udhay")
Out[53]: "'udhay'"
In [54]: str('udhay')
Out[54]: 'udhay'
In [55]: repr('udhay')
Out[55]: "'udhay'"
In [56]: print str("udhay"), repr("udhay"), str('udhay'), repr('udhay')
udhay 'udhay' udhay 'udhay'
In [57]: name = repr("Udhay")
print "My name is ", name
My name is 'Udhay'
aligning the text and specifying a width
In [58]: '{:<30}'.format('Python Programming') # ljust
Out[58]: 'Python Programming '
[Link] 48/338
13/11/2016 python Basic and Intermediate Level Modules
In [59]: print '{:<30}'.format('Python Programming') # ljust
Python Programming
In [60]: '{:>30}'.format('Python Programming') # rjust
Out[60]: ' Python Programming'
In [61]: '{:^30}'.format('Python Programming') # center
Out[61]: ' Python Programming '
Assignment: Create a raw template for the supermarket bill, with corresponding data types. Use string
formatting to display the final bill. Hint: extend previous supermarket logic.
2.8 Logical Operations
In [63]: a = 12;
In [64]: a > 9
Out[64]: True
In [65]: a < 14
Out[65]: True
In [66]: a == 34
Out[66]: False
In [67]: a != 23
Out[67]: True
In [68]: a <> 23 # <> is also used as != ; used only in python 2.x
Out[68]: True
In [69]: (a > 9) and (a < 34)
Out[69]: True
In [70]: (a>9) and (a>34)
Out[70]: False
In [71]: (a > 9) or (a > 34)
Out[71]: True
[Link] 49/338
13/11/2016 python Basic and Intermediate Level Modules
In [72]: (a < 9) or (a > 34)
Out[72]: False
In [73]: not ((a<9) or (a>34))
Out[73]: True
In [74]: (a<9), not (a<9)
Out[74]: (False, True)
2.9 Bitwise operations
In [75]: 4 << 1 # bitwise left‐shift ‐ shifts their corresponding binary digits l
eft side by one position. # binary notation 8421
Out[75]: 8
In [76]: 4>>1 # bitwise right‐shift
Out[76]: 2
In [77]: 4 & 8 # bitwise AND # 4 ‐ 0100 8 ‐ 1000
Out[77]: 0
In [78]: 4 & 12
Out[78]: 4
In [79]: 4 | 8 # bitwise OR
Out[79]: 12
In [80]: 4, ~4 # bitwise not ‐ Gives the 1's complement value
Out[80]: (4, ‐5)
2.10 Identity Operations
is, is not
In [81]: 4 is 4 # is ‐ does object level (value, type, address)identity check
Out[81]: True
In [82]: a = 12
b = 234
[Link] 50/338
13/11/2016 python Basic and Intermediate Level Modules
In [83]: a is b
Out[83]: False
In [84]: b = 12
In [85]: a is b
Out[85]: True
In [87]: a not is b
File "<ipython‐input‐87‐d94cceae31d8>", line 1
a not is b
^
SyntaxError: invalid syntax
In [86]: a is not b
Out[86]: False
Interview Question : why 'is' operator results in False for two objects with same value above 257, and True
for values less than or equal to 256?
In [88]: a = 257
b = 257
In [89]: a is b
Out[89]: False
In [90]: id(a), id(b) # Observe the addressed of these objects
Out[90]: (42037752, 42037728)
In [91]: a = 256
b = 256
In [92]: a is b
Out[92]: True
In [93]: id(a), id(b) # 1 byte ‐ 256
Out[93]: (38896092, 38896092)
Assignment : Try this with float values, and try to find the boundary?
[Link] 51/338
13/11/2016 python Basic and Intermediate Level Modules
2.10 Boolean Operations
True, False
In [94]: True
Out[94]: True
In [95]: False
Out[95]: False
In [96]: true
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐96‐74d9a83219ca> in <module>()
‐‐‐‐> 1 true
NameError: name 'true' is not defined
In [97]: not True
Out[97]: False
In [98]: True is True
Out[98]: True
In [99]: print True is False
False
In [100]: False is True
Out[100]: False
In [101]: False is False
Out[101]: True
In [102]: False is not True
Out[102]: True
In [103]: True is not False
Out[103]: True
In [104]: True *3 # Value for 'True' boolean object is 1
Out[104]: 3
[Link] 52/338
13/11/2016 python Basic and Intermediate Level Modules
In [105]: False * 9 # Value for 'False' boolean object is 0
Out[105]: 0
In [106]: True == 1
Out[106]: True
In [107]: True is 1
Out[107]: False
In [108]: id(True), id(1)
Out[108]: (498410564, 38893200)
In [109]: True is not 1
Out[109]: True
In [110]: 0 == False
Out[110]: True
In [111]: 0 is False
Out[111]: False
In [112]: bool(23 > 45) # bool() ‐ builtin function; results in boolean value
Out[112]: False
In [113]: bool('') # empty or null elements result in False
Out[113]: False
In [114]: bool('python')
Out[114]: True
In [115]: bool(0), bool(1), bool(23420), bool(‐3424)
Out[115]: (False, True, True, True)
In [116]: bool([]), bool({}), bool(())
Out[116]: (False, False, False)
In [117]: bool([12]), bool({12}), bool((12))
Out[117]: (True, True, True)
[Link] 53/338
13/11/2016 python Basic and Intermediate Level Modules
In [118]: bool([2,3]), bool({23, 34}), bool((122, 345))
Out[118]: (True, True, True)
In [119]: bool(True), bool(False), bool(not False)
Out[119]: (True, False, True)
2.10 Order of evaluation
operators descriptions
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
(), [], {}, ‘’ tuple, list, dictionnary, string
[Link], x[], x[i:j], f() attribute, index, slide, function call
+x, ‐x, ~x unary negation, bitwise invert
** exponent
*, /, % multiplication, division, modulo
+, ‐ addition, substraction
<<, >> bitwise shifts
& bitwise and
^ bitwise xor
| bitwise or
<, <=, >=, >,==, != comparison operators
is, is not, in,not in comparison operators (continue)
not boolean NOT
and boolean AND
or boolean OR
lambda lambda expression
Assignment 3 : Try few arithmetic expressions and Observe that the Order of precedence is followed.
2.11 Conditional Operations
PEP 8 recommends 4 spaces for indentation
In [120]: if True:
print "By Default, It will be True"
By Default, It will be True
[Link] 54/338
13/11/2016 python Basic and Intermediate Level Modules
In [121]: if True:
print "By Default, It will be True"
else:
print "Unless given as False, It isn't choosen"
By Default, It will be True
In [122]: if 34 < 56:
print '34<56'
34<56
In [123]: if 1:
print 'It is true'
else:
print 'It is false'
It is true
In [124]: if ‐67:
print 'It is true'
else:
print 'It is false'
It is true
In [125]: a = False
if a == True:
print 'It is true'
else:
print 'It is false'
It is false
In [126]: if a: # equivalent to a == True
print 'It is true'
else:
print 'It is false'
It is false
In [127]: if not a:
print 'It is true'
else:
print 'It is false'
It is true
[Link] 55/338
13/11/2016 python Basic and Intermediate Level Modules
In [128]: a = ‐23
if a:
print 'It is not zero'
else:
print 'It is zero'
It is not zero
2.11 range() and xrange()
In [129]: range(9)
Out[129]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [130]: var = range(9)
print type(var), var
<type 'list'> [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [131]: range(0,9)
Out[131]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [132]: range(0,9,1) # range(initialValue, FinalBound, Step)
Out[132]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
range(initialValue, FinalBound, Step)
defaults:
initialValue = 0th value
FinalBound = last value in list/string
step = +1
In [133]: range(‐1)
Out[133]: []
In [134]: range(0,9,1)
Out[134]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [135]: range(0,9,2)
Out[135]: [0, 2, 4, 6, 8]
In [136]: range(0,9,‐2) # 0‐2 = ‐2 , which is out of bounds of [0‐9]
Out[136]: []
[Link] 56/338
13/11/2016 python Basic and Intermediate Level Modules
In [137]: range(9,0,‐2)
Out[137]: [9, 7, 5, 3, 1]
In [138]: range(9.5,0.5,‐0.5)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐138‐08513d47a46c> in <module>()
‐‐‐‐> 1 range(9.5,0.5,‐0.5)
TypeError: range() integer end argument expected, got float.
In [139]: print range(‐44, ‐4, 4)
[‐44, ‐40, ‐36, ‐32, ‐28, ‐24, ‐20, ‐16, ‐12, ‐8]
In [140]: print range(‐4, ‐44, ‐4)
[‐4, ‐8, ‐12, ‐16, ‐20, ‐24, ‐28, ‐32, ‐36, ‐40]
In [141]: range(9,‐1) # Here, finalBoundValue = ‐1; step = +1
Out[141]: []
In [142]: range(9,1) # Here, finalBoundValue = 1; step = +1
Out[142]: []
Interview Question : What is the difference between range() and xrange()?
range() will result in a list data type. It will store the resulting data in the buffer. For larger values, it will cost
more buffer (heap) memory to store all those values. It is memory inefficient.
In [143]: print range(34)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 2
1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
In [144]: print xrange(34)
xrange(34)
In [145]: for i in range(34):
print i, # , is a newline suppressor
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33
[Link] 57/338
13/11/2016 python Basic and Intermediate Level Modules
In [146]: for i in xrange(34):
print i,
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33
NOTE: When iterating, both xrange() and range() will result the same end output.
In [148]: #!/usr/bin/python
# [Link]
ticketNumber = raw_input("Enter the ticket Number")
lotteryNumber = "12AE8Jy4"
if ticketNumber == lotteryNumber:
print "YOU ARE THE WINNER"
else:
print "Sorry! But, Try again. You may win very next moment"
Enter the ticket NumberY023H56HAT
Sorry! But, Try again. You may win very next moment
Assignment : create a lottery ticket game, which takes only numbers; then prompt whether the given ticket
number is greater, lesser or equal to the lotter ticket number.
3.1 for Loop
General sematic for for loop in any language
for (initialization, condition, increment/decrement)
logic
syntax in Python:
for indexingVaraible1, indexingVariable2,.. in (iterator1, iterator2,..):
logic
In [149]: for i in [1,2,3,5]: # here, initialization is 1,
print i
1
2
3
5
[Link] 58/338
13/11/2016 python Basic and Intermediate Level Modules
In [150]: for i in range(2, 10, 2): # initialization is 2, increment = 2, finalValue
= 10
print i, # , operator works as newline suppresser, here
2 4 6 8
In [151]: for i in range(6): # initialization = 0, increment = 1; finalValue = 6
print i,
0 1 2 3 4 5
In [152]: for i in 'Python':
print i,
P y t h o n
In [153]: words = ['Python', 'Programming', 'Django', 'NEtworking']
for i in words:
print i,
Python Programming Django NEtworking
In Python, Pass, Continue and break are used to loops.
Though continue and break are similar to that of other traditional programming languages, pass is a unique
feature available in python.
break Terminates the loop, after its invocation.
continue Skips the current loop, and continues perform the next consecutive loops.
pass Does nothing. No logic to work. No break nor skipping a loop. pass is placed when there is no logic to
be written for that condition, within the loop. It is advantageous for the developers for writing the logic in future.
In [154]: for i in words:
if i == 'Python':
continue
print i
Programming
Django
NEtworking
[Link] 59/338
13/11/2016 python Basic and Intermediate Level Modules
In [155]: #!/usr/bin/python
#class5_passContinueBreakSysExit.py
print "\nExample to illustrate CONTINUE "
for j in range(10):
if j==5:
continue
print j,
print "\nExample to illustrate BREAK "
for p in range(10):
if p==5:
break
print p,
print "\nExample to illustrate PASS "
for i in range(10):
if i==5:
pass # its like TODO
print i,
Example to illustrate CONTINUE
0 1 2 3 4 6 7 8 9
Example to illustrate BREAK
0 1 2 3 4
Example to illustrate PASS
0 1 2 3 4 5 6 7 8 9
pass It acts like a TODO during development. Generally, pass is used when logic will be written in future.
3.2 while loop
Syntax in python
initialization
while condition
logic
increment/decrement
Prefer while loop, only when the exit condition known; else it might lead to infinite loop.
In [156]: a = 0 # initialization
while a<20: # condition
print a, # logic statement
a+=1 # increment
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[Link] 60/338
13/11/2016 python Basic and Intermediate Level Modules
In [157]: b = 34
while b>0:
print b,
b‐=1
34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
8 7 6 5 4 3 2 1
In [158]: # Fibonacci Series: 0,1,1,2,3,5,8,13,...
a,b = 0,1 # Tuple unpacking
while b < 100: # to print fibonacci numbers less than 100
print b,
a,b = b, a+b
1 1 2 3 5 8 13 21 34 55 89
In [159]: a,b = 0,1 # Tuple unpacking
count = 0 # initializing new variable
while b < 100: # to print fibonacci numbers less than 100
print b,
a,b = b, a+b
count+=1 # newVariables can't be used in loops, as new variable gets cre
ated everytime
1 1 2 3 5 8 13 21 34 55 89
In [160]: count
Out[160]: 11
In [161]: a,b = 0,1 # Tuple unpacking
count = 0 # initializing new variable
while b < 100: # to print fibonacci numbers less than 100
print b,
a,b = b, a+b
count+=1 # newVariables can't be used in loops, as new variable gets cre
ated everytime
print "\nIn total, there are %d fibonacci numbers"%(count)
1 1 2 3 5 8 13 21 34 55 89
In total, there are 11 fibonacci numbers
[Link] 61/338
13/11/2016 python Basic and Intermediate Level Modules
In [162]:
[Link] 62/338
13/11/2016 python Basic and Intermediate Level Modules
#!/usr/bin/python
#class5_whileFor.py
print "\nExample to illustrate CONTINUE "
print "with while loop"
j = 0
while j<10:
if j == 5:
j+=1 # comment this line and observe the difference
continue
print j,
j+=1
print "\nwith for loop"
for j in range(10):
if j==5:
continue
print j,
print "\nExample to illustrate BREAK "
print "with while loop"
p = 0
while p<10:
if p == 5:
break
print p,
p+=1
print "\nwith for loop"
for p in range(10):
if p==5:
break
print p,
print "\nExample to illustrate PASS "
print "with while loop"
i = 0
while i<10:
if i == 5:
pass
print i,
i+=1
print "\nwith for loop"
for i in range(10):
if i==5:
pass # its like TODO
print i,
print "\nExample to illustrate [Link] "
import sys
print "with while loop"
i = 0
[Link] 63/338
13/11/2016 python Basic and Intermediate Level Modules
while i<10:
if i == 5:
[Link](0) # exit code 0 ‐ everything is correct. 1‐ some error
print i,
i+=1
print "\nwith for loop"
for i in range(10):
if i==5:
[Link](1)
print i,
[Link] 64/338
13/11/2016 python Basic and Intermediate Level Modules
Example to illustrate CONTINUE
with while loop
0 1 2 3 4 6 7 8 9
with for loop
0 1 2 3 4 6 7 8 9
Example to illustrate BREAK
with while loop
0 1 2 3 4
with for loop
0 1 2 3 4
Example to illustrate PASS
with while loop
0 1 2 3 4 5 6 7 8 9
with for loop
0 1 2 3 4 5 6 7 8 9
Example to illustrate [Link]
with while loop
0 1 2 3 4
An exception has occurred, use %tb to see the full traceback.
SystemExit: 0
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
c:\python27\lib\site‐packages\IPython\core\[Link] in run_code(s
elf, code_obj, result)
2875 result.error_in_exec = e
2876 [Link](exception_only=True)
‐> 2877 warn("To exit: use 'exit', 'quit', or Ctrl‐D.", level=1)
2878 except self.custom_exceptions:
2879 etype, value, tb = sys.exc_info()
TypeError: 'level' is an invalid keyword argument for this function
NOTE: Observe that [Link]() will exit the program execution. Also, observe that last for loop wasn't executed.
In [163]: # Generating the POWER Series:
# e**x =1+x+x**2/2! +x**3/3! +....+ x**n/n! where 0 < x < 1
# class5_powerSeries.py
#!/usr/bin/python
x = float(input("Enter the value of x: "))
n = term = num = 1 # multiple Assigment
sum = 1.0
while n <= 100:
term *= x / n
sum += term
n += 1
if term < 0.0001:
break
print("No of Times= %d and Sum= %f" % (n, sum)) # print function
Enter the value of x: 3
No of Times= 15 and Sum= 20.085523
[Link] 65/338
13/11/2016 python Basic and Intermediate Level Modules
In [164]: #!/usr/bin/python
# class5_multiplicationTable.py
# Write a Script to print the multiplication table up to 10
maxLimit = 14
i = 1
print "‐"*20
while i<maxLimit:
n =1
while n<maxLimit:
print '%2d * %2d = %3d'%(i,n,i*n),
#print '%3d'%(i*n),
n+=1
print '|'
i+=1
print "‐"*20
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
1 * 1 = 1 |
2 * 2 = 4 |
3 * 3 = 9 |
4 * 4 = 16 |
5 * 5 = 25 |
6 * 6 = 36 |
7 * 7 = 49 |
8 * 8 = 64 |
9 * 9 = 81 |
10 * 10 = 100 |
11 * 11 = 121 |
12 * 12 = 144 |
13 * 13 = 169 |
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
[Link] 66/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Run the below class5_multiplicationTables2.py, with the various commented options, in the
script. Observe the differences.
#!/usr/bin/python
# ‐*‐ coding: utf‐8 ‐*‐
# class5_multiplicationTables2.py
'''
Purpose:
To print the multiplications tables upto 10
'''
maxLimit = 10
#maxLimit = int(raw_input('Enter the maximum table to display :'))
print '‐'*50
i=1
while i<maxLimit+1:
j =1
while j< maxLimit+1:
#print "i = ", i,"j = " , j, "i*j = ", i*j
#print i, '*', j, '=', i*j
#print "%d * %d = %d "%(i,j,i*j)
print '%2d * %2d = %3d'%(i,j, i*j)#,
j+=1
#print i
print '‐'*25
i+=1
print '‐'*50
Assignment : Based on class5_multiplicationTables2.py, write a program to display the multiplication tables
from 1 through 10, horizontally
ex:
1 * 1 = 1 | 2 * 1 = 2 | 3 * 1 = 3 | ....
1 * 2 = 2 | 2 * 2 = 4 | 3 * 2 = 6 | ....
...
[Link] 67/338
13/11/2016 python Basic and Intermediate Level Modules
In [165]: #!/usr/bin/python
# class5_halfDiamond.py
# To display the astrickes in a half‐diamond pattern
size = 10
j=0
#print 'j=',j
while j<size:
print '*'*j
j+=1
#print 'j=',j
while j>0:
print '*'*j
j‐=1
#print 'j=',j
# implementation with for loop
for j in range(size):
print '*'*j
for j in range(size,0, ‐1):
print '*'*j
[Link] 68/338
13/11/2016 python Basic and Intermediate Level Modules
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
[Link] 69/338
13/11/2016 python Basic and Intermediate Level Modules
In [166]: #!/usr/bin/python
# class5_quaterdiamond.py
# Printing a quarter diamond
row = int(input("Enter the number of rows: "))
n = row
while n >= 0:
x = "*" * n
y = " " * (row ‐ n)
print(y + x)
n ‐= 1
print 'row = %d'%(row)
print 'n = %d'%(n)
n = row
while n >= 0:
x = "*" * n
y = " " * (row ‐ n)
print(x+y)
n ‐= 1
Enter the number of rows: 5
*****
****
***
**
*
row = 5
n = ‐1
*****
****
***
**
*
Assignment : Write a program to display a full diamond shape, separetely using for loop, and using while
loop?
4 Collections
Lists
Tuples
named Tuples
Dictionaries
sets
Comprehensions
[Link] 70/338
13/11/2016 python Basic and Intermediate Level Modules
4.1 Lists
List can be classified as singledimensional and multidimensional.
List is representing using []
List is a mutable object, which means elements in list can be changed.
It can store asymmetric data types.
In [167]: list1 = [12, 23, 34, 56, 67, 89] # Homogenous list
In [168]: type(list1)
Out[168]: list
In [169]: l2 = [12, 23.45, 231242314125, 'Python', True, str(0), complex(2,3), 2+3j, int(23.
45)]
In [170]: print l2 # non‐homogenous list
[12, 23.45, 231242314125L, 'Python', True, '0', (2+3j), (2+3j), 23]
In [171]: print type(l2)
<type 'list'>
In [172]: print l2[3], type(l2[3]) # indexing
Python <type 'str'>
In [173]: l2[6], type(6) # elements of a list, retain their data type.
Out[173]: ((2+3j), int)
In [174]: print dir(l2) # results in the attributes and methods associated with
'list' type
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__del
slice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '_
_getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '_
_init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__
new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul_
_', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '_
_subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'rem
ove', 'reverse', 'sort']
In [175]: l1 = [] # empty list
In [177]: print l1, type(l1)
[] <type 'list'>
[Link] 71/338
13/11/2016 python Basic and Intermediate Level Modules
In [178]: l1 = [12, 34] # Re‐initialization. Previous object gets overwritten
In [179]: print [Link](23) # Observe that append() operation doesn't return anything
None
In [180]: print l1
[12, 34, 23]
In [181]: [Link](['Python', 3456])
In [182]: print l1
[12, 34, 23, ['Python', 3456]]
In [183]: [Link]([56]) # It is different from [Link](56)
In [184]: print "l1 = ", l1
l1 = [12, 34, 23, ['Python', 3456], [56]]
In [185]: [Link](78)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐185‐7defae611412> in <module>()
‐‐‐‐> 1 [Link](78)
TypeError: 'int' object is not iterable
In [186]: [Link]([78])
In [187]: print l1
[12, 34, 23, ['Python', 3456], [56], 78]
In [188]: [Link](['Django', 45567])
In [189]: print l1
[12, 34, 23, ['Python', 3456], [56], 78, 'Django', 45567]
Interview Question : In which cases, [Link] is more suitable than [Link]?
extend add the new list to the original list, in the same dimension
append adds the new list to the original list, in separate dimension
[Link] 72/338
13/11/2016 python Basic and Intermediate Level Modules
In [190]: print list1+l1
[12, 23, 34, 56, 67, 89, 12, 34, 23, ['Python', 3456], [56], 78, 'Django', 45
567]
In [191]: l2 = [12, [23, 45], [56]]
In [192]: l3 = [12, [23, 45], [56]]
In [193]: l2+l3
Out[193]: [12, [23, 45], [56], 12, [23, 45], [56]]
In [194]: [Link](l3) # adding in new dimension
In [195]: print l2
[12, [23, 45], [56], [12, [23, 45], [56]]]
In [196]: l2 = [12, [23, 45], [56]] # reassigning
In [197]: [Link](l3)
In [198]: print l2 # It is same as l2+l3
[12, [23, 45], [56], 12, [23, 45], [56]]
In [199]: [Link](0, 'Yash')
In [200]: print l2
['Yash', 12, [23, 45], [56], 12, [23, 45], [56]]
In [201]: [Link](5, 999) # specifying position is mandatory
In [202]: l2
Out[202]: ['Yash', 12, [23, 45], [56], 12, 999, [23, 45], [56]]
NOTE: [Link]() is different from overwritting
In [204]: l2[5] = 'Nine Nine'
In [205]: print l2
['Yash', 12, [23, 45], [56], 12, 'Nine Nine', [23, 45], [56]]
In [206]: [Link](3,[23, ''])
[Link] 73/338
13/11/2016 python Basic and Intermediate Level Modules
In [207]: print l2
['Yash', 12, [23, 45], [23, ''], [56], 12, 'Nine Nine', [23, 45], [56]]
In [208]: l2 = [12, [23, 45], [56]] # reassigning
In [209]: print len(l2)
3
In [210]: [Link](len(l2), l3)
In [211]: l2 # It is same as [Link](l3)
Out[211]: [12, [23, 45], [56], [12, [23, 45], [56]]]
In [212]: [Link](45, 34) # there isn't 45th position. so, added in the last
In [213]: print l2
[12, [23, 45], [56], [12, [23, 45], [56]], 34]
In [214]: print [Link]() # outputs last element, and removes from list
34
In [215]: print l2
[12, [23, 45], [56], [12, [23, 45], [56]]]
In [216]: l2 = [12, [23, 45], [56]] # reassigning
In [217]: [Link](12) # nothing will be outputted
In [218]: print l2
[[23, 45], [56]]
In [219]: [Link](56)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ValueError Traceback (most recent call last)
<ipython‐input‐219‐33f84afb8304> in <module>()
‐‐‐‐> 1 [Link](56)
ValueError: [Link](x): x not in list
In [220]: [Link]([56])
In [221]: print l2
[[23, 45]]
[Link] 74/338
13/11/2016 python Basic and Intermediate Level Modules
In [222]: [Link]([23])
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ValueError Traceback (most recent call last)
<ipython‐input‐222‐5a17760cd7ae> in <module>()
‐‐‐‐> 1 [Link]([23])
ValueError: [Link](x): x not in list
In [223]: l2[0], type(l2)
Out[223]: ([23, 45], list)
In [224]: l2[0].remove(23)
In [225]: print l2
[[45]]
In [226]: l2 = [12, [23, 45], [56]] # reassigning
In [227]: del l2[1] # deleting an element in list
In [228]: l2
Out[228]: [12, [56]]
In [229]: del l2 # deleting a list object
In [230]: print l2
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐230‐9ecc7d9d142b> in <module>()
‐‐‐‐> 1 print l2
NameError: name 'l2' is not defined
In [231]: l3 = ['zero', 0, '0', 'Apple', 1, '1']
In [232]: sorted(l3) # builtin function ; doesn't affect the original object
Out[232]: [0, 1, '0', '1', 'Apple', 'zero']
In [233]: print l3
['zero', 0, '0', 'Apple', 1, '1']
In [234]: [Link]() # modifies the original object
[Link] 75/338
13/11/2016 python Basic and Intermediate Level Modules
In [235]: print l3
[0, 1, '0', '1', 'Apple', 'zero']
In [236]: reversed(l3)
Out[236]: <listreverseiterator at 0x3c68e10>
In [237]: for i in reversed(l3):
print i,
zero Apple 1 0 1 0
In [238]: list(reversed(l3))
Out[238]: ['zero', 'Apple', '1', '0', 1, 0]
In [239]: l4 = []
for i in reversed(l3):
[Link](i)
print l4 # This is list type
['zero', 'Apple', '1', '0', 1, 0]
In [240]: l3 # original object didn't change
Out[240]: [0, 1, '0', '1', 'Apple', 'zero']
In [241]: [Link]()
In [242]: l3
Out[242]: ['zero', 'Apple', '1', '0', 1, 0]
In [243]: [Link](1)
Out[243]: 1
In [244]: [Link]('1')
Out[244]: 1
In [245]: [Link]('1')
In [246]: l3
Out[246]: ['zero', 'Apple', '1', '0', 1, 0, '1']
In [247]: [Link]('1')
Out[247]: 2
[Link] 76/338
13/11/2016 python Basic and Intermediate Level Modules
In [248]: [Link](['1'])
In [249]: l3
Out[249]: ['zero', 'Apple', '1', '0', 1, 0, '1', ['1']]
In [250]: [Link]('1') # dimension account here
Out[250]: 2
In [251]: [Link](['1'])
Out[251]: 1
4.1a List Comprehensions
syntax:
[logic for i in (initialValue, finalValue, increment/decrement) if condition]
In [252]: week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'S
unday']
In [253]: type(week), type(week[2])
Out[253]: (list, str)
In [254]: print [i for i in week]
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunda
y']
In [255]: for i in week:
print i,
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
In [257]: [print i for i in week]
File "<ipython‐input‐257‐be84efc03df9>", line 1
[print i for i in week]
^
SyntaxError: invalid syntax
NOTE: stdout not possible within comprehension
In [258]: print [[Link]() for i in week]
['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDA
Y']
[Link] 77/338
13/11/2016 python Basic and Intermediate Level Modules
In [259]: print [[Link]() for i in week]
['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunda
y']
In [260]: print [[Link]() for i in week]
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunda
y']
In [261]: print [i[0:3] for i in week]
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
In [262]: print [i[:3] for i in week]
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
In [263]: print [i[0:2] for i in week]
['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']
In [264]: print [i[3] for i in week]
['d', 's', 'n', 'r', 'd', 'u', 'd']
In [265]: print [i for i in week if [Link]('T')]
['Tuesday', 'Thursday']
NOTE: else and elif are not possible in list comprehensions
In [267]: print [i for i in week if [Link]('W')]
['Wednesday']
In [268]: print [i for i in week if len(i) == 8]
['Thursday', 'Saturday']
In [269]: ord('a') # returns the ACSCII value of the character
Out[269]: 97
In [270]: print [ord(i) for i in 'Python Programming']
[80, 121, 116, 104, 111, 110, 32, 80, 114, 111, 103, 114, 97, 109, 109, 105,
110, 103]
In [271]: chr(80) # returns the correpsonding ACSII character for the number
Out[271]: 'P'
[Link] 78/338
13/11/2016 python Basic and Intermediate Level Modules
In [272]: print [chr(i) for i in range(12)]
['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08',
'\t', '\n', '\x0b']
In [273]: print [chr(i) for i in [80, 121, 116, 104, 111, 110, 32, 80, 114, 111, 103, 11
4, 97, 109, 109, 105, 110, 103]]
['P', 'y', 't', 'h', 'o', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm',
'i', 'n', 'g']
In [274]: ''.join([chr(i) for i in [80, 121, 116, 104, 111, 110, 32, 80, 114, 111, 103, 114
97, 109, 109, 105, 110, 103]])
Out[274]: 'Python Programming'
In [275]: print [float(i) for i in range(9)]
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
Assignment : Generate a Caesar Cipher for a given string using list comprehensions
In [276]: for i in range(4):
print i, i*i
0 0
1 1
2 4
3 9
In [277]: print [i, i*i for i in range(9)]
File "<ipython‐input‐277‐3c71da0ffd9d>", line 1
print [i, i*i for i in range(9)]
^
SyntaxError: invalid syntax
It will not work, as objects resulting from logic are not qualified
In [279]: print [[i, i*i] for i in range(9)]
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64]]
In [280]: print [(i, i*i) for i in range(9)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64)]
In [281]: print [[i, i*i, i*i*i] for i in range(9)]
[[0, 0, 0], [1, 1, 1], [2, 4, 8], [3, 9, 27], [4, 16, 64], [5, 25, 125], [6,
36, 216], [7, 49, 343], [8, 64, 512]]
[Link] 79/338
13/11/2016 python Basic and Intermediate Level Modules
In [282]: [[i, i*i, i*i*i] for i in range(9)]
Out[282]: [[0, 0, 0],
[1, 1, 1],
[2, 4, 8],
[3, 9, 27],
[4, 16, 64],
[5, 25, 125],
[6, 36, 216],
[7, 49, 343],
[8, 64, 512]]
4.1b Nested List comprehensions
In [283]: [(i,i*i, i*i*i, i**4) for i in range(9)]
Out[283]: [(0, 0, 0, 0),
(1, 1, 1, 1),
(2, 4, 8, 16),
(3, 9, 27, 81),
(4, 16, 64, 256),
(5, 25, 125, 625),
(6, 36, 216, 1296),
(7, 49, 343, 2401),
(8, 64, 512, 4096)]
Flattening a multidimensional list
In [285]: matrix = [[11, 22, 33],
[22, 33, 11],
[33, 11, 22]]
print len(matrix)
print matrix
3
[[11, 22, 33], [22, 33, 11], [33, 11, 22]]
In [287]: flattened = []
for row in matrix:
for n in row:
[Link](n)
print flattened
[11, 22, 33, 22, 33, 11, 33, 11, 22]
In [289]: flattenedLc = [n for row in matrix for n in row]
print flattenedLc
[11, 22, 33, 22, 33, 11, 33, 11, 22]
[Link] 80/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Pythogorean triples, between 1 and 55
a**2 + b**2 = c**2
ex: [3,4,5] WAP to result these triples using for loop, while loop and list comprehension
4.1c Composite List Comprehensions
In [290]: ['Good' if i else "Bad" for i in range(3)] # Duck‐tying
Out[290]: ['Bad', 'Good', 'Good']
In [291]: # It the same as the below
for i in range(3):
if i:
print "Good"
else:
print "Bad"
Bad
Good
Good
In [293]: ["Good" if not i else "Bad" for i in range(3)]
Out[293]: ['Good', 'Bad', 'Bad']
Assignment : Implement Queue mechanism using lists (FIFO Queue)
Assignment : If the temperature on 22 July, 2015 was recorded as 32°C, 44°C, 45.5°C and 22°C in Paris,
Hyderabad, Visakhapatnam and Ontario respectively, what are their temperatures in °F.
Hint: T(°F) = T(°C) × 9/5 + 32
Assignment : If the temperatures on same day this year are forecasted as same numerics in °F, what will be
their corresponding temperature in °C.
Interview Question : What is the sorting algorithm used in python?
Ans: timesort
Interview Question : What is the difference between sort() and sorted()?
Ans: [Link]() is a nethod of list data structure;whereas sorted(list) is a buildin function. [Link]() will modify
the existing list object; sorted(list) will create a new object, without modifying the existing list object. Limitation
of list comprehensions is that pass, break, continue won't work in them.
[Link] 81/338
13/11/2016 python Basic and Intermediate Level Modules
Interview Question 5: Implement the stack mechanism using 'List'
Creating a Stack with Lists
stack works based on LastIn FirstOut (LIFO) mechanism.
The push and pop operations of stack can be mimicked using append() and pop() methods of list,
respectively.
In [1]: stack = [12, 34, 45, 5677]
In [2]: print type(stack)
<type 'list'>
In [3]: [Link](25) # push operation
In [4]: print stack
[12, 34, 45, 5677, 25]
In [5]: [Link]() # pop operation
Out[5]: 25
In [6]: [Link]() # LIFO
Out[6]: 5677
In [7]: [Link](8880)
In [8]: print stack
[12, 34, 45, 8880]
Creataing a Queue with Lists
Interview Question : Implement a queue mechanism, using collections module
In [9]: from collections import deque
queue = deque(['Python', 'Programming', 'Pearl'])
print queue
deque(['Python', 'Programming', 'Pearl'])
[Link] 82/338
13/11/2016 python Basic and Intermediate Level Modules
In [10]: type(queue) # returns a named tuple # It is of '[Link]' type.
Different from basic col types
Out[10]: [Link]
In [11]: print dir(queue)
['__class__', '__copy__', '__delattr__', '__delitem__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash
__', '__iadd__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne
__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '_
_setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'app
end', 'appendleft', 'clear', 'count', 'extend', 'extendleft', 'maxlen', 'po
p', 'popleft', 'remove', 'reverse', 'rotate']
In [12]: [Link]('George')
In [13]: [Link]('Bush')
In [14]: queue
Out[14]: deque(['Bush', 'George', 'Python', 'Programming', 'Pearl'])
In [15]: [Link]()
Out[15]: 'Pearl'
In [16]: [Link]()
Out[16]: 'Programming'
In [17]: queue
Out[17]: deque(['Bush', 'George', 'Python'])
In [18]: [Link]() # clears the queue; but retains the queue object
In [19]: queue
Out[19]: deque([])
Interview Question : what is the difference between = and ==
= assignment operation ; Also called as Hard COPY
== equivalence checking operation
In [20]: a = 23 # assigning 23 to 'a'
[Link] 83/338
13/11/2016 python Basic and Intermediate Level Modules
In [21]: print a
23
In [22]: a == 23
Out[22]: True
4.1.2 Hard COPY vs Shallow COPY vs Deep COPY
([Link]
v=yjYIyydmrc0&index=2&list=PLTEjme3I6BCg6PdKkMaysdtuEG1cAv_0)
Hard COPY is the assignment Operation
In [23]: parList = [1,2,3,4,54,5,56,6]
In [24]: childList = parList # Assignment Operation (or) Hard COPY
Assignment operation wont create a new object; rather the new identifier (variable) refers to the same Object
In [26]: print parList, type(parList)
[1, 2, 3, 4, 54, 5, 56, 6] <type 'list'>
In [27]: print childList, type(childList)
[1, 2, 3, 4, 54, 5, 56, 6] <type 'list'>
In [28]: parList == childList
Out[28]: True
In [29]: parList is childList # becoz both are refering to the same object.
Out[29]: True
In [30]: print id(parList), id(childList)
67383048 67383048
In [31]: parList[4]
Out[31]: 54
In [32]: parList[4] = 'Five Four'
In [33]: parList
Out[33]: [1, 2, 3, 4, 'Five Four', 5, 56, 6]
[Link] 84/338
13/11/2016 python Basic and Intermediate Level Modules
In [34]: childList # modifications are reflected in childList
Out[34]: [1, 2, 3, 4, 'Five Four', 5, 56, 6]
In [35]: childList[5] = 'Five'
In [36]: childList
Out[36]: [1, 2, 3, 4, 'Five Four', 'Five', 56, 6]
In [37]: parList # modifications are reflected in parList
Out[37]: [1, 2, 3, 4, 'Five Four', 'Five', 56, 6]
import copy
[Link]() ‐> Shallow COPY
[Link]() ‐> Deep COPY
In [38]: parList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Programming']]
# re‐assigning
In [39]: hardCopyList = parList # Hard COPY or assignment operation
In [40]: import copy
shallowCopyList = [Link](parList) # shallow COPY
deepCopyList = [Link](parList) # Deep COPY
In [41]: print 'parList = %r \nhardCopyList = %r \nshallowCopyList = %r \ndeepCopyList
= %r'%(\
parList, hardCopyList, shallowCopyList, deepCopyList)
parList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Programming']]
hardCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Programmin
g']]
shallowCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
deepCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Programmin
g']]
In [63]: print 'id(parList) = %16r \nid(hardCopyList) = %11r \nid(shallowCopyList) = %r
\nid(deepCopyList) = %11r'%(\
id(parList), id(hardCopyList), id(shallowCopyList), id(deepCopyLis
t))
id(parList) = 67482848
id(hardCopyList) = 67482848
id(shallowCopyList) = 67483248
id(deepCopyList) = 67482288
[Link] 85/338
13/11/2016 python Basic and Intermediate Level Modules
With this, we can draw inference that shallowCopyList and deepCopyList are creating a new objects
In [43]: parList == hardCopyList == shallowCopyList == deepCopyList
Out[43]: True
In [44]: parList is hardCopyList is shallowCopyList is deepCopyList
Out[44]: False
In [45]: parList is hardCopyList
Out[45]: True
In [46]: parList is shallowCopyList
Out[46]: False
In [47]: parList is deepCopyList
Out[47]: False
In [48]: shallowCopyList is deepCopyList
Out[48]: False
In [49]: parList
Out[49]: [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Programming']]
In [50]: parList[4] = False
In [60]: print 'parList = %75r \nhardCopyList = %70r \nshallowCopyList = %65r \ndeepCop
yList = %69r'%(\
parList, hardCopyList, shallowCopyList, deepCopyList)
parList = [12, 23.34, '1223', 'Python', False, [12, 23, '34', 'Progra
mming']]
hardCopyList = [12, 23.34, '1223', 'Python', False, [12, 23, '34', 'Progra
mming']]
shallowCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
deepCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
In [64]: hardCopyList[2] = 'NEW STRING'
[Link] 86/338
13/11/2016 python Basic and Intermediate Level Modules
In [70]: print 'parList = %81r \nhardCopyList = %76r \nshallowCopyList = %65r \ndeepCop
yList = %69r'%(\
parList, hardCopyList, shallowCopyList, deepCopyList)
parList = [12, 23.34, 'NEW STRING', 'Python', False, [12, 23, '34',
'Programming']]
hardCopyList = [12, 23.34, 'NEW STRING', 'Python', False, [12, 23, '34',
'Programming']]
shallowCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
deepCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
In [71]: parList[5][1]
Out[71]: 23
In [72]: parList[5][1] = 88 # changing in second dimension
In [73]: print 'parList = %81r \nhardCopyList = %76r \nshallowCopyList = %65r \ndeepCop
yList = %69r'%(\
parList, hardCopyList, shallowCopyList, deepCopyList)
parList = [12, 23.34, 'NEW STRING', 'Python', False, [12, 88, '34',
'Programming']]
hardCopyList = [12, 23.34, 'NEW STRING', 'Python', False, [12, 88, '34',
'Programming']]
shallowCopyList = [12, 23.34, '1223', 'Python', True, [12, 88, '34', 'Program
ming']]
deepCopyList = [12, 23.34, '1223', 'Python', True, [12, 23, '34', 'Program
ming']]
Now, let us try in 3rd dimension
In [74]: parList = [12, '1223', [23, '34', 'Programming', [56, 45.56, '98.45',
'Flask']]]
In [75]: deepCopyList = [Link](parList)
In [76]: parList[2][3][3]
Out[76]: 'Flask'
In [77]: parList[2][3][3] = 'Django'
In [78]: print parList, '\n', deepCopyList
[12, '1223', [23, '34', 'Programming', [56, 45.56, '98.45', 'Django']]]
[12, '1223', [23, '34', 'Programming', [56, 45.56, '98.45', 'Flask']]]
[Link] 87/338
13/11/2016 python Basic and Intermediate Level Modules
Conclusions:
In single dimension lists, if you do not want the copied list to get affected to the changes in source
list, go for shallow COPY.
In multidimensional lists, if you do not want the copied list to get affected to changes in source list,
go for deep COPY.
Other Builtin functions
all() Verifies whether all the elements in the collection(list,tuple,set,dictionary) are True or False
any() Verifies whether any of the elements in the collection(list,tuple,set,dictionary) are True or False
In [79]: l = [1, 2, ‐4, [34, 556, [56, 67, 0]]]
In [80]: any(l)
Out[80]: True
In [81]: all(l) # doesn't consider deep dimensions; only considers the first dimensi
on elements
Out[81]: True
In [82]: [Link]('')
In [83]: l
Out[83]: [1, 2, ‐4, [34, 556, [56, 67, 0]], '']
In [84]: all(l) # '' has False as boolean result
Out[84]: False
4.2 Tuples
Tuples are immutable (means can't be edited)
Tuples have all the capabilities of lists, expect modification.
Tuples can be indexed
In [85]: t = () # empty tuple
In [86]: print t, type(t)
() <type 'tuple'>
[Link] 88/338
13/11/2016 python Basic and Intermediate Level Modules
In [87]: print dir(t)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__l
en__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex_
_', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subcla
sshook__', 'count', 'index']
In [88]: t2 = ('Apple', 'Mango', 'Goa') # simple Homogeneous tuple
In [89]: t3 = ('Apple', 123, 34.56, True) # simple non‐homogenous tuple
Buitin functions can be applied on them
In [90]: any(t3)
Out[90]: True
In [91]: all(t3)
Out[91]: True
Indexing and slicing Tuples
In [92]: print t2
('Apple', 'Mango', 'Goa')
In [93]: print t2[1]
Mango
In [94]: t3[:2]
Out[94]: ('Apple', 123)
In [95]: t3[‐1]
Out[95]: True
In [96]: t3[1:]
Out[96]: (123, 34.56, True)
In [97]: [Link](True)
Out[97]: 1
[Link] 89/338
13/11/2016 python Basic and Intermediate Level Modules
In [98]: [Link](True)
Out[98]: 3
In [99]: t3
Out[99]: ('Apple', 123, 34.56, True)
In [100]: t3[:]
Out[100]: ('Apple', 123, 34.56, True)
In [101]: t3[::]
Out[101]: ('Apple', 123, 34.56, True)
In [102]: t3[::‐1]
Out[102]: (True, 34.56, 123, 'Apple')
In [103]: t3 is t3[:] is t3[::]
Out[103]: True
In [104]: t3 is t3[::‐1]
Out[104]: False
In [105]: numbers = range(9)
print numbers
print type(numbers)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
<type 'list'>
In [106]: # List to tuple conversion
tuple(numbers) # tuple() ‐ built‐in function for converting to tuple
Out[106]: (0, 1, 2, 3, 4, 5, 6, 7, 8)
In [107]: print type(tuple(numbers)), type(numbers)
<type 'tuple'> <type 'list'>
In [108]: t2 + t3 # concatenation
Out[108]: ('Apple', 'Mango', 'Goa', 'Apple', 123, 34.56, True)
In [109]: t2 + (3,4)
Out[109]: ('Apple', 'Mango', 'Goa', 3, 4)
[Link] 90/338
13/11/2016 python Basic and Intermediate Level Modules
In [110]: t2 + (3)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐110‐bf92910fe75d> in <module>()
‐‐‐‐> 1 t2 + (3)
TypeError: can only concatenate tuple (not "int") to tuple
In [111]: t2 + (3,)
Out[111]: ('Apple', 'Mango', 'Goa', 3)
In [112]: t2 + t3 != t3 + t2 # commutative Property not satisfied
Out[112]: True
In [113]: t2*2 # Repition
Out[113]: ('Apple', 'Mango', 'Goa', 'Apple', 'Mango', 'Goa')
In [114]: print len(t2), len(t2*2)
3 6
In [116]: t5 = t2[0:len(t2)‐1] # same as t2[0:2]
print t5
('Apple', 'Mango')
in
Membership verification operator. Used on lists, tuples, strings, dictionaries
In [117]: 'Apple' in t5
Out[117]: True
In [118]: 'Banana' in t5
Out[118]: False
In [119]: 'Apple ' in t5
Out[119]: False
In [120]: 'Apples' not in t5
Out[120]: True
[Link] 91/338
13/11/2016 python Basic and Intermediate Level Modules
is ‐‐ is not
in ‐‐ not in
Assigment 1: Try all the operations performed on Lists, to tuples, and observe the difference
Tuples are Immutable
In [121]: t3
Out[121]: ('Apple', 123, 34.56, True)
In [122]: t3[0] = 'Kiwi'
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐122‐69c0cea87f59> in <module>()
‐‐‐‐> 1 t3[0] = 'Kiwi'
TypeError: 'tuple' object does not support item assignment
In [123]: t2**2 # power operation
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐123‐7e012b19a351> in <module>()
‐‐‐‐> 1 t2**2 # power operation
TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'
NOTE: Tuple are Immutable; So, they can't be edited. But, can be overwritten
Buitin functions on Tuples
In [124]: t1 = tuple(range(9)); t2 = tuple(range(3,12))
print "t1 = ", t1,'\n', "t2 = ", t2
t1 = (0, 1, 2, 3, 4, 5, 6, 7, 8)
t2 = (3, 4, 5, 6, 7, 8, 9, 10, 11)
In [125]: t3 = tuple(xrange(9))
print "t3 = ", t3
t3 = (0, 1, 2, 3, 4, 5, 6, 7, 8)
[Link] 92/338
13/11/2016 python Basic and Intermediate Level Modules
In [126]: # cmp(obj1, obj2) ‐ builtin function. result in
# +1 if obj1 is greater,
# ‐1 if obj1 is smaller, and
# 0 if both obj1 and obj2 are equal
cmp(t1,t2)
Out[126]: ‐1
In [127]: cmp(t2,t1)
Out[127]: 1
In [128]: cmp(t1,t1)
Out[128]: 0
In [129]: min(t2)
Out[129]: 3
In [130]: min((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1))) # even multidimensional elements are c
onsidered
Out[130]: ‐1
In [131]: min((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1), (‐1, 9))) # even multidimensional eleme
nts are considered
Out[131]: ‐1
In [132]: min((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1, ‐9))) # even multidimensional elements a
re considered
Out[132]: 0
Assignment : Try the min() function for lists, and lists within tuples
In [133]: max(t2)
Out[133]: 11
In [134]: max((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1, 100))) # even multidimensional elements
are considered
Out[134]: (‐1, 100)
In [135]: max((0, 1, 2, 3, 4, 5, 6, 7, 8, (100), (78,)))
Out[135]: (78,)
Inference: (100) is an integer, whereas (78,) is a tuple object.
[Link] 93/338
13/11/2016 python Basic and Intermediate Level Modules
In [136]: max((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1, 100), (100), (‐1, ‐2, 100)))
Out[136]: (‐1, 100)
In [137]: max((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1, 100), [‐1, 100] ))
Out[137]: (‐1, 100)
In [138]: max((0, 1, 2, 3, 4, 5, 6, 7, 8, (‐1, 100), [‐1, 100], {‐1,100}, {‐1:100} ))
Out[138]: (‐1, 100)
In [144]: () > set() > [] > {} # order in collections
Out[144]: True
In [ ]: list(t2) # list() ‐ builtin function; used to convert to list type
In [145]: sorted(t2) # convert any collection type, to list; then sorts; Creates new o
bject
Out[145]: [3, 4, 5, 6, 7, 8, 9, 10, 11]
Tuple Unpacking
In [146]: a = 12
In [147]: print a
12
In [149]: a,b = 420, 950
print "a =", a
print "b =", b
print type(a)
a = 420
b = 950
<type 'int'>
In [150]: a = 420, 950
print "a =", a
print type(a)
a = (420, 950)
<type 'tuple'>
[Link] 94/338
13/11/2016 python Basic and Intermediate Level Modules
In [151]: a,b = 420
print "a =", a
print "b =", b
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐151‐4dee3c1eb5cb> in <module>()
‐‐‐‐> 1 a,b = 420
2
3 print "a =", a
4 print "b =", b
TypeError: 'int' object is not iterable
NOTE: For unpacking, ensure that the number of identifiers in left side of assignment operator, must be equal
to the number of objects(values) on the righthand side
In [153]: (a,b,c,d,e) = 12, 23, 34, 45, 45
print a, type(a)
12 <type 'int'>
In [154]: (a,b,c,d,e) = (12, 23, 34, 45, 45)
print a, type(a)
12 <type 'int'>
In [155]: (a,b,c,d,e) = [12, 23, 34, 45, 45]
print a, type(a)
12 <type 'int'>
In [156]: [a,b,c,d,e] = [12, 23, 34, 45, 45] # List unpacking
print a, type(a)
12 <type 'int'>
In [157]: print [a,b,c,d,e], type([a,b,c,d,e])
[12, 23, 34, 45, 45] <type 'list'>
Lists within Tuples, and tuples within Lists
In [158]: th = (12, 23, 34, [54, 54, 65,(23, 45), [34, 45]], ('python', 'programming'))
[Link] 95/338
13/11/2016 python Basic and Intermediate Level Modules
In [159]: len(th)
Out[159]: 5
In [160]: th[4]
Out[160]: ('python', 'programming')
In [161]: print type(th[3]), type(th[4])
<type 'list'> <type 'tuple'>
In [162]: th[3][0]
Out[162]: 54
Interview Question : Can the list elements present within the tuple, be changed?
In [163]: th[3][0] = 'Five Four'
In [164]: print th
(12, 23, 34, ['Five Four', 54, 65, (23, 45), [34, 45]], ('python', 'programmi
ng'))
In [165]: th[4][0]
Out[165]: 'python'
In [166]: th[4][0] = 'Django'
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐166‐1b45a8ec8717> in <module>()
‐‐‐‐> 1 th[4][0] = 'Django'
TypeError: 'tuple' object does not support item assignment
In [167]: th[3][4][0]
Out[167]: 34
In [168]: th[3][4][0] = 'Three Four'
In [169]: th[3][3][0]
Out[169]: 23
[Link] 96/338
13/11/2016 python Basic and Intermediate Level Modules
In [170]: th[3][3][0] = 'two three'
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐170‐f35586ec7407> in <module>()
‐‐‐‐> 1 th[3][3][0] = 'two three'
TypeError: 'tuple' object does not support item assignment
Inference: The mutability of an element depends on its primary collection type
Assignment : Write a Program to convert this heterogeneous tuple completely to flat tuple.
Input = (2,23, 34, [55, 'six six', (77, 88, ['nine nine', 0])])
Output = (2, 23, 34, 55, 'six six', 77, 88, 'nine nine', 0)
Tuple Comprehensions (or) Generator expressions
In [171]: tc = (i for i in range(9))
In [172]: print tc
<generator object <genexpr> at 0x041246C0>
In [173]: len(tc)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐173‐a4c5085a07fe> in <module>()
‐‐‐‐> 1 len(tc)
TypeError: object of type 'generator' has no len()
In [174]: print [Link]()
0
In [175]: print [Link]()
1
In [176]: print [Link]()
2
[Link] 97/338
13/11/2016 python Basic and Intermediate Level Modules
In [177]: print [Link](), [Link](), [Link](), [Link](), [Link](), [Link]()
3 4 5 6 7 8
In [178]: print [Link]()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐178‐b701980f9cc2> in <module>()
‐‐‐‐> 1 print [Link]()
StopIteration:
NOTE: Calling [Link]() when there is no value in that, results in StopIteration exception
In [179]: tc = (i for i in range(9))
In [180]: for ele in tc:
print ele,
0 1 2 3 4 5 6 7 8
In [181]: [r for r in tc] # Once it results all the data, there will not be any more ele
ment in that object
Out[181]: []
In [182]: tc = (i for i in range(9))
In [183]: [r for r in tc]
Out[183]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [184]: tc = (i for i in range(9))
tl = [i for i in tc]
print type(tc), type(tl)
<type 'generator'> <type 'list'>
In [185]: [[Link]() for i in tc] # no exception, as there is no element in 'tc'
Out[185]: []
Interview Question : what is the result of this operation:
tc = (i for i in range(9)); [[Link]() for i in tc]
[Link] 98/338
13/11/2016 python Basic and Intermediate Level Modules
In [186]: tc = (i for i in range(9))
[[Link]() for i in tc] # 'i' is an integer, but not an iterator; tc is i
terator;
# During iteration, elements become basic data ty
pes
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐186‐e300884eaf16> in <module>()
1 tc = (i for i in range(9))
‐‐‐‐> 2 [[Link]() for i in tc] # 'i' is an integer, but not an iterato
r; tc is iterator;
3 # During iteration, elements become basi
c data types
AttributeError: 'int' object has no attribute 'next'
In [187]: [type(i) for i in tc]
Out[187]: [int, int, int, int, int, int, int, int]
In [188]: tc = (i for i in [(12, 34), 12, 23, 'String', 'Python', True, 23.2])
In [189]: [i for i in tc]
Out[189]: [(12, 34), 12, 23, 'String', 'Python', True, 23.2]
In [190]: print [type(i) for i in tc]
[]
In [191]: tc = (i for i in [(12, 34), 12, 23, 'String', 'Python', True, 23.2])
print [type(i) for i in tc]
[<type 'tuple'>, <type 'int'>, <type 'int'>, <type 'str'>, <type 'str'>, <typ
e 'bool'>, <type 'float'>]
Assignment : practice all the exercises performed on list comprehensions, on tuple comprehensions
4.3 Sets
sets are unordered; Then can't be indexed
sets doesn't store duplicates; It will discard the two and other consequent occurrences of the same
element.
denoted with {}
In [192]: s1 = set() # empty set # set() is a builtin function
[Link] 99/338
13/11/2016 python Basic and Intermediate Level Modules
In [193]: print s1, type(s1)
set([]) <type 'set'>
In [194]: s2 = {9,1,2,2,3,4,5,3,5} # simple set
print s2, type(s2)
set([1, 2, 3, 4, 5, 9]) <type 'set'>
In [195]: s3 = {1,2,[1,2], (1,2), 1,2} # compound set with a list and a tuple as eleme
nts
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐195‐1fd6a6c319ec> in <module>()
‐‐‐‐> 1 s3 = {1,2,[1,2], (1,2), 1,2} # compound set with a list and a tuple
as elements
TypeError: unhashable type: 'list'
NOTE: Elements in a set must be immutable only.
mutable ‐ list
Immutable ‐ tuple, string, int, float, long int
In [196]: s3 = {1,2,tuple([1,2]), (1,2), 1,2}
print s3, type(s3)
set([(1, 2), 1, 2]) <type 'set'>
Interview Question : What is the simplest way to remove duplicates in a list?
Ans list(set(list1))
In [197]: myList = [45,1,2,3,2,3,3,4,5,6,4,4,4,45,45]
myList = list(set(myList)) # set() and list() are buit‐in functions
print type(myList), myList
<type 'list'> [1, 2, 3, 4, 5, 6, 45]
In [198]: s4 = {'Apple', 'Mango', 'Banana', 12, 'Bnana', 'Mango'}
print type(s4), s4
<type 'set'> set([12, 'Mango', 'Bnana', 'Banana', 'Apple'])
[Link] 100/338
13/11/2016 python Basic and Intermediate Level Modules
In [199]: print dir(s4) # set attributes
['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le
__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduc
e__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__seta
ttr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'a
dd', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersec
tion', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop',
'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'u
pdate']
NOTE: As sets can't be indexed, we need not bother about the way the sequence in which the elements are
taken into the set.
NOTE: Sets doesn't support arithmetic Operations.
In [200]: s5 = {'Mercedes', 'Toyota', 'Maruthi', 'Hyundai'}
In [201]: s4 + s5 # + is not even used as concatenation operator on sets
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐201‐b57dac89ce6c> in <module>()
‐‐‐‐> 1 s4 + s5 # + is not even used as concatenation operator on sets
TypeError: unsupported operand type(s) for +: 'set' and 'set'
In [202]: s4 ‐ s5 # It means to get elements of s4, which are not present in s5
Out[202]: {12, 'Apple', 'Banana', 'Bnana', 'Mango'}
In [203]: s5 = {'Mercedes', 'Toyota', 'Maruthi', 'Hyundai', 'Mango'}
In [204]: s4 ‐ s5
Out[204]: {12, 'Apple', 'Banana', 'Bnana'}
In [206]: # list to tuple conversion
countries = set(['India', 'Afganistan', 'Sri Lanka', 'Nepal'])
print type(countries), countries
<type 'set'> set(['Afganistan', 'Sri Lanka', 'Nepal', 'India'])
[Link] 101/338
13/11/2016 python Basic and Intermediate Level Modules
In [207]: # tuple to list conversion
brics = set(('Brazil', 'Russia', 'India', 'China', 'South Africa'))
print type(brics), brics
<type 'set'> set(['Brazil', 'China', 'India', 'Russia', 'South Africa'])
Interview Question : what is the result of set('Python Programming')
In [208]: # string to set of characters
strSet = set('Python Programming')
print type(strSet), strSet
<type 'set'> set(['a', ' ', 'g', 'i', 'h', 'm', 'o', 'n', 'P', 'r', 't',
'y'])
In [209]: # List to set conversion
strSet1 = set(['Python Programming'])
print type(strSet1), strSet1
<type 'set'> set(['Python Programming'])
In [210]: # tuple to set conversion
strSet2 = set(('Python Programming'))
print type(strSet2), strSet2
<type 'set'> set(['a', ' ', 'g', 'i', 'h', 'm', 'o', 'n', 'P', 'r', 't',
'y'])
Interview Question : what is the result of set(1221)?
In [211]: set(1221)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐211‐a4823664953d> in <module>()
‐‐‐‐> 1 set(1221)
TypeError: 'int' object is not iterable
In [212]: set('1221')
Out[212]: {'1', '2'}
[Link] 102/338
13/11/2016 python Basic and Intermediate Level Modules
In [213]: set([1221])
Out[213]: {1221}
In [214]: set([1221, 12221, 1221])
Out[214]: {1221, 12221}
In [215]: import sets
c:\python27\lib\site‐packages\ipykernel\__main__.py:1: DeprecationWarning: th
e sets module is deprecated
if __name__ == '__main__':
In [216]: # list to sets
asean = [Link](['Myanmar', 'Indonesia', 'Malaysia', 'Philiphines', 'Thailan
d'])
print type(asean), asean # Observe the type of the object
<class '[Link]'> Set(['Malaysia', 'Philiphines', 'Indonesia', 'Myanmar', 'T
hailand'])
In [217]: # list contains a list and tuple in it
africa = [Link](['south Africa', 'Mozambique', ['Moracco', 'tunisia'], ('ken
ya', 'sudan')])
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐217‐b1538a717281> in <module>()
‐‐‐‐> 1 africa = [Link](['south Africa', 'Mozambique', ['Moracco', 'tunisi
a'], ('kenya', 'sudan')])
c:\python27\lib\[Link] in __init__(self, iterable)
412 self._data = {}
413 if iterable is not None:
‐‐> 414 self._update(iterable)
415
416 def __getstate__(self):
c:\python27\lib\[Link] in _update(self, iterable)
357 try:
358 for element in it:
‐‐> 359 data[element] = value
360 return
361 except TypeError:
TypeError: unhashable type: 'list'
In [218]: africa = [Link](['south Africa', 'Mozambique', ('Moracco', 'tunisia'), ('ken
ya', 'sudan')])
[Link] 103/338
13/11/2016 python Basic and Intermediate Level Modules
In [219]: print africa
Set([('Moracco', 'tunisia'), ('kenya', 'sudan'), 'Mozambique', 'south Afric
a'])
In [220]: engineers = set(['John', 'Jane', 'Jack', 'Janice'])
programmers = [Link]({'Jack', 'Sam', 'Susan', 'Janice'})
managers = {'Jane', 'Jack', 'Susan', 'Zack'}
print type(engineers), type(programmers), type(managers)
<type 'set'> <class '[Link]'> <type 'set'>
In [221]: programmers = set(programmers)
print type(programmers)
<type 'set'>
4.3. 2 Set Operations
| ‐ union operator
& ‐ Intersection operator
‐ ‐ difference operator
In [222]: employees = engineers | programmers | managers
print employees
set(['Jack', 'Sam', 'Susan', 'Jane', 'Janice', 'John', 'Zack'])
In [223]: engg_managers = engineers & managers
print engg_managers
set(['Jane', 'Jack'])
In [224]: onlyManagers = managers ‐ engineers ‐ programmers # same as (managers ‐ engine
ers) ‐ programmers
print onlyManagers
set(['Zack'])
In [225]: onlyEngineers = engineers ‐ managers ‐ programmers
print onlyEngineers
set(['John'])
[Link] 104/338
13/11/2016 python Basic and Intermediate Level Modules
4.3.3 Mutability of sets
In [226]: [Link]('Naseer') # add ‐ to add an element to the set
In [227]: print engineers
set(['Jane', 'Naseer', 'Janice', 'John', 'Jack'])
4.3.4 SuperSet and Subset
In [228]: [Link](engineers)
Out[228]: False
In [229]: [Link](employees)
Out[229]: False
In [230]: print engineers, '\n', employees
set(['Jane', 'Naseer', 'Janice', 'John', 'Jack'])
set(['Jack', 'Sam', 'Susan', 'Jane', 'Janice', 'John', 'Zack'])
In [231]: [Link]('Naseer')
In [232]: [Link](engineers)
# It means that every element of 'engineers' set is present in 'employees' set
Out[232]: True
In [233]: [Link]('Susan')
In [234]: print employees
set(['Naseer', 'Jack', 'Sam', 'Jane', 'Janice', 'John', 'Zack'])
Interview Question : what is the error that occurs for [Link](element), when the element is not present
in the set
In [235]: [Link]('Shoban')
Observation: didn't result any execption, even though 'Shoban' is not present in set
[Link] 105/338
13/11/2016 python Basic and Intermediate Level Modules
4.3.4 frozenset
set is a mutable object; elements in a set can be modified
frozenset is an immutable object; elements in a frozenset can't be modified
In [236]: vetoCountries = set(['US', 'UK', 'Russia', 'China', 'France'])
print vetoCountries
set(['France', 'China', 'UK', 'US', 'Russia'])
In [237]: print dir(vetoCountries)
['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le
__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduc
e__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__seta
ttr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'a
dd', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersec
tion', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop',
'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'u
pdate']
In [238]: vetoCountries = frozenset(['US', 'UK', 'Russia', 'China', 'France'])
print vetoCountries
frozenset(['France', 'China', 'UK', 'US', 'Russia'])
In [239]: print dir(vetoCountries)
['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__',
'__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__',
'__subclasshook__', '__xor__', 'copy', 'difference', 'intersection', 'isdisj
oint', 'issubset', 'issuperset', 'symmetric_difference', 'union']
In [240]: testSet = {12, 12.34, True, (45.67, (23, 45)), 'Omen', 2+3j, set(['France','R
ussia'])}
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐240‐0dec7e97501d> in <module>()
‐‐‐‐> 1 testSet = {12, 12.34, True, (45.67, (23, 45)), 'Omen', 2+3j, set(['F
rance','Russia'])}
TypeError: unhashable type: 'set'
[Link] 106/338
13/11/2016 python Basic and Intermediate Level Modules
In [241]: testSet = {12, 12.34, True, (45.67, (23, 45)), 'Omen', 2+3j, frozenset(['Fran
ce','Russia'])}
print testSet, type(testSet)
set([True, (2+3j), 'Omen', 12.34, 12, frozenset(['Russia', 'France']), (45.6
7, (23, 45))]) <type 'set'>
NOTE: Sets are mutable (can be changed); frozensets are immutable (can't be changed). Both sets and
frozensets can store immutable objects only.
As set is mutable, it can't be placed in a set; whereas as frozenset is immutable, it can be placed within a set
or frozenset.
In [242]: fruits = {'Mango', 'Apple', 'Papaya', 'apple'}
vegetables = {'Beetroot', 'cabbage', 'Carrot', 'Carrot'}
In [243]: fruitsAndVegetables = [Link](vegetables)
print fruitsAndVegetables
set(['Beetroot', 'Mango', 'Papaya', 'apple', 'Carrot', 'cabbage', 'Apple'])
In [244]: [Link]('tomato')
In [245]: print fruits
set(['a', 'Papaya', 'apple', 'm', 'o', 'Mango', 't', 'Apple'])
In [246]: [Link](['tomato'])
[Link](('banana')) # string
[Link]({'Banana'})
print fruits
set(['a', 'tomato', 'b', 'Papaya', 'apple', 'm', 'o', 'n', 'Mango', 't', 'Ban
ana', 'Apple'])
In [247]: [Link](('banana',)) # tuple element
print fruits
set(['a', 'tomato', 'b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango',
't', 'Banana', 'Apple'])
In [248]: [Link]('a')
print fruits
set(['tomato', 'b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 't',
'Banana', 'Apple'])
[Link] 107/338
13/11/2016 python Basic and Intermediate Level Modules
In [249]: [Link]('t')
print fruits
set(['tomato', 'b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Ban
ana', 'Apple'])
In [250]: [Link]('m','o', 't')
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐250‐f92a0d176540> in <module>()
‐‐‐‐> 1 [Link]('m','o', 't')
TypeError: discard() takes exactly one argument (3 given)
Inference: [Link]() will discards only one element once
In [251]: [Link](('m','o', 't'))
print fruits
set(['tomato', 'b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Ban
ana', 'Apple'])
Inference: [Link]() tries to locate and remove the tuple ('m','o', 't')
In [252]: [Link](vegetables)
Out[252]: set()
In [253]: [Link](['tomato', 'watermelon'])
print vegetables
set(['Beetroot', 'tomato', 'cabbage', 'Carrot', 'watermelon'])
In [254]: [Link](vegetables)
Out[254]: {'tomato'}
In [255]: print fruits ‐ vegetables
set(['b', 'Papaya', 'apple', 'm', 'o', 'n', 'Mango', 'Banana', 'banana', 'App
le'])
Observation: Observe that [Link](vegetables) elements are not present
In [256]: print vegetables ‐ fruits
set(['Beetroot', 'cabbage', 'Carrot', 'watermelon'])
[Link] 108/338
13/11/2016 python Basic and Intermediate Level Modules
In [257]: [Link](vegetables) == [Link](fruits)
Out[257]: True
In [260]: fruits ‐ vegetables != vegetables ‐ fruits
Out[260]: True
NOTE: set difference is not Commutative; whereas intersection attribute of set is Commutative. Intersection
results in the common elements among the sets given
In [261]: [Link](vegetables) # no, there is a common element
Out[261]: False
In [262]: [Link](vetoCountries) #yes, there is no common element
Out[262]: True
In [263]: print fruits
print [Link]() # It will remove some element in random
print fruits
set(['tomato', 'b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Ban
ana', 'Apple'])
tomato
set(['b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Banana', 'App
le'])
In [265]: print fruits
print [Link]('b') # To remove a particular element; [Link]() will
not return anything
print fruits
set(['b', 'Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Banana', 'App
le'])
None
set(['Papaya', 'apple', 'banana', 'm', 'o', 'n', 'Mango', 'Banana', 'Apple'])
Assignment : Explore the differences between [Link]() vs [Link]() vs [Link]()
Assignment : Explore the differences between [Link]() and [Link]()
Interview Question : what is the difference between discard, pop and remove methods of set.
In [266]: print asean
Set(['Malaysia', 'Philiphines', 'Indonesia', 'Myanmar', 'Thailand'])
[Link] 109/338
13/11/2016 python Basic and Intermediate Level Modules
In [267]: print [Link]() # deletes an elemnt, in random; So, not preferred
Malaysia
In [268]: [Link]('Myanmar') # delete the given element
NOTE: remove() can't delete multiple elements
In [269]: print asean
Set(['Philiphines', 'Indonesia', 'Thailand'])
In [270]: [Link]('Myanmar') # delete the given element
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
KeyError Traceback (most recent call last)
<ipython‐input‐270‐fe684ca56941> in <module>()
‐‐‐‐> 1 [Link]('Myanmar') # delete the given element
c:\python27\lib\[Link] in remove(self, element)
516 """
517 try:
‐‐> 518 del self._data[element]
519 except TypeError:
520 transform = getattr(element, "__as_temporarily_immutable_
_", None)
KeyError: 'Myanmar'
In [271]: [Link]('Myanmar')
Inference: [Link]() will not throw any exception, even if the quieried element is not present
Assignment : try the [Link](), [Link]() and [Link]() with multiple elements, and with string in
different collections
[Link] 110/338
13/11/2016 python Basic and Intermediate Level Modules
Conclusion:
[Link]() vs [Link]() vs [Link]()
[Link]() ‐ Used to remove an element. Throws KeyError, if the specifed elemen
t is not present
[Link]() ‐ Used to remove an element. Doesn't raise any error, if specified e
lement is not present.
[Link]() ‐ Used to remove and RETURN a random element from the set. Raises ke
yError, if no element is present.
for sets A and B,
symmetric difference is (A‐B) | (B‐A)
It is same as union intersection
In [273]: fruits = {'Mango', 'Apple', 'Papaya', 'tomato'}
vegetables = {'Beetroot', 'cabbage', 'tomato', 'Carrot', 'Carrot'}
In [274]: fruits.symmetric_difference(vegetables)
Out[274]: {'Apple', 'Beetroot', 'Carrot', 'Mango', 'Papaya', 'cabbage'}
In [275]: # set.symmetric_difference() is cumulative
fruits.symmetric_difference(vegetables) == vegetables.symmetric_difference(fru
its)
Out[275]: True
Interview Question : what is the result of set1 = {1, 'Python', True}
In [276]: set1 = {1, 'Python', True}
print type(set1), set1
<type 'set'> set(['Python', True])
In [277]: id(1), id(True)
Out[277]: (43612384, 1593123908)
[Link] 111/338
13/11/2016 python Basic and Intermediate Level Modules
NOTE: True is preferred as it is builtin object
In [278]: print all(set1)
True
In [279]: print any(set1)
True
In [280]: set2 = {10, 10.9, 0.01, 0, 'Prog', None, ''}
In [281]: print all(set2)
False
In [282]: print any(set2)
True
In [283]: for i in set2:
print i,
0 10 None 10.9 Prog 0.01
In [284]: lc = [i for i in set2] # list comprehension
print type(lc), lc
<type 'list'> ['', 0, 10, None, 10.9, 'Prog', 0.01]
In [285]: sc = {i for i in set2} # set comprehension
print type(sc), sc
<type 'set'> set(['', 0, 10, None, 10.9, 'Prog', 0.01])
enumerate() builtin function; enumerate() stores the index of the elements iterated.
In [286]: for i,j in enumerate(set2):
print i,j
0
1 0
2 10
3 None
4 10.9
5 Prog
6 0.01
In [287]: max(set2), min(set2)
Out[287]: ('Prog', None)
[Link] 112/338
13/11/2016 python Basic and Intermediate Level Modules
In [288]: a = None; print type(a)
<type 'NoneType'>
In [289]: sorted(set2)
Out[289]: [None, 0, 0.01, 10, 10.9, '', 'Prog']
In [290]: len(set2)
Out[290]: 7
4.3.6 Orderedset
Used to store elements in an ascending order
This is a module, to be imported
This module doesn;t come with standard library
It must be installed using the command
pip install orderedset
NOTE: If you are working in windows, and the error is as below,
error: Microsoft Visual C++ 9.0 is required (Unable to find [Link]). Get it
from [Link]
then, download VC++ compiler from [Link]
In [291]: import orderedset
In [292]: oset = [Link]([11, 2, 3.33, '1', 1, 'python', frozenset('tomat
o')])
print type(oset)
print oset # [Link]() ensures that the assigned order of th
e set is retained.
<class 'orderedset._orderedset.OrderedSet'>
OrderedSet([11, 2, 3.33, '1', 1, 'python', frozenset(['a', 'm', 't', 'o'])])
[Link] 113/338
13/11/2016 python Basic and Intermediate Level Modules
In [293]: print dir(oset)
['__abstractmethods__', '__and__', '__class__', '__contains__', '__delattr_
_', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute_
_', '__getitem__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__',
'__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__metacl
ass__', '__module__', '__ne__', '__new__', '__or__', '__pyx_vtable__', '__qua
lname__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed_
_', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str_
_', '__sub__', '__subclasshook__', '__weakref__', '__xor__', '_abc_cache', '_
abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_from_i
terable', '_hash', 'add', 'clear', 'copy', 'difference', 'difference_update',
'discard', 'index', 'intersection', 'intersection_update', 'isdisjoint', 'is
orderedsubset', 'isorderedsuperset', 'issubset', 'issuperset', 'pop', 'remov
e', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
In [294]: [Link]('AI')
In [295]: print oset # Observe that updated elements are placed in the end (right‐mos
t)
OrderedSet([11, 2, 3.33, '1', 1, 'python', frozenset(['a', 'm', 't', 'o']),
'A', 'I'])
In [296]: [Link](['AI'])
print oset
OrderedSet([11, 2, 3.33, '1', 1, 'python', frozenset(['a', 'm', 't', 'o']),
'A', 'I', 'AI'])
In [297]: print [Link]()
AI
In [298]: print [Link]()
I
In [299]: for _ in range(4): print [Link]()
A
frozenset(['a', 'm', 't', 'o'])
python
1
In [300]: print oset # Observe that four set elements were removed from the last(right‐
most)
OrderedSet([11, 2, 3.33, '1'])
Assignment : Try to get a sorted set from the given set, using orderedset module
[Link] 114/338
13/11/2016 python Basic and Intermediate Level Modules
4.4 Dictionaries
It is a key/value structure
It contain series of key:value pair, separated by comma(,) operator and enclosed in {} .
eg: dict1 = {key1:value1, key2: value2}
It doesn't store duplicate keys
The keys in dictionaries should be immutable (string, tuple, int, float, frozenset). whereas list, set is
not possible.
Indexing is done based on keys, and not position.
In [301]: d = {} # Empty dictionary
print type(d), d
<type 'dict'> {}
In [302]: d1 = {12}
print type(d1)
<type 'set'>
Interview Question: what is the type of 'd2' in the below statement:
d2 = {12,}
In [303]: d2 = {12,}
print type(d2)
<type 'set'>
In [ ]: d3 = {12:23}
print type(d3)
In [304]: d4 = {'a': 'apple', 'b': 'banana', 'c': 'cat'} # method 1 of dictionary crea
tion
print d4 # Observe the order of the el
ements
{'a': 'apple', 'c': 'cat', 'b': 'banana'}
[Link] 115/338
13/11/2016 python Basic and Intermediate Level Modules
In [305]: dict1 = {} # method 2 of dictionaru creat
ion
dict1['A'] = 'Apple'
dict1['B'] = 'Banana' # The dictionary keys must be
unique
dict1['B'] = 'Ball' # For duplicate assignments for a key, the key will retain
the last assignment
dict1['c'] = 'Cat'
print dict1
{'A': 'Apple', 'c': 'Cat', 'B': 'Ball'}
Indexing the dictionaries
In [306]: print "There is one ", dict1['c'], " in the hall"
There is one Cat in the hall
In [308]: print dict1['C'] # case sensitivity
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
KeyError Traceback (most recent call last)
<ipython‐input‐308‐70c4ffc7a92c> in <module>()
‐‐‐‐> 1 print dict1['C']
KeyError: 'C'
In [309]: 'C' in dict1 # membership check
Out[309]: False
In [310]: 'c' in dict1 # membership check
Out[310]: True
In [311]: [Link]()
Out[311]: ['Apple', 'Cat', 'Ball']
In [312]: [Link]()
Out[312]: ['A', 'c', 'B']
Interview Question : what is the type of the result of [Link]()?
In [313]: [Link]()
Out[313]: [('A', 'Apple'), ('c', 'Cat'), ('B', 'Ball')]
[Link] 116/338
13/11/2016 python Basic and Intermediate Level Modules
In [314]: dict1
Out[314]: {'A': 'Apple', 'B': 'Ball', 'c': 'Cat'}
Editing an existing dictionary
In [315]: dict1['ac'] = 'Air Conditioner'
In [316]: dict1['z'] = 'Zombie'
In [317]: print dict1
{'A': 'Apple', 'c': 'Cat', 'B': 'Ball', 'ac': 'Air Conditioner', 'z': 'Zombi
e'}
In [318]: if 'ac' in dict1:
print 'There is ', dict1['ac']
There is Air Conditioner
In [319]: [Link]('B')
Out[319]: 'Ball'
In [320]: [Link]('b') # Doesn't raise any error, in the absence of specified key
In [321]: [Link]('b', 'xxx') # if not present, returns the specified value
Out[321]: 'xxx'
iterations on dictionaries
In [322]: [i for i in [Link]()]
Out[322]: [('A', 'Apple'),
('c', 'Cat'),
('B', 'Ball'),
('ac', 'Air Conditioner'),
('z', 'Zombie')]
In [323]: [i for i in [Link]()]
Out[323]: ['A', 'c', 'B', 'ac', 'z']
In [324]: [i for i in [Link]()]
Out[324]: ['Apple', 'Cat', 'Ball', 'Air Conditioner', 'Zombie']
[Link] 117/338
13/11/2016 python Basic and Intermediate Level Modules
In [325]: [i for i in dict1] # In loops, dict1 means [Link]() by default
Out[325]: ['A', 'c', 'B', 'ac', 'z']
NOTE: By default, iterating over a dictionary takes place on keys() only
String Formatting with dictionaries
In [327]: cricket ={'players': 'Batsmen and Bowlers', 'count': 11}
print cricket
{'count': 11, 'players': 'Batsmen and Bowlers'}
In [328]: cricket['players']
Out[328]: 'Batsmen and Bowlers'
In [329]: sentence = "The %(players)s in cricket team are %(count)d in number!"%cricket
print sentence
The Batsmen and Bowlers in cricket team are 11 in number!
In [330]: sentence = "The %(players)r in cricket team are %(count)r in number!"%cricket
print sentence
The 'Batsmen and Bowlers' in cricket team are 11 in number!
In [336]: sentence = "The %(0)r in cricket team are %(1)r in number!"%[Link]()
print sentence
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐336‐766646641dec> in <module>()
‐‐‐‐> 1 sentence = "The %(0)r in cricket team are %(1)r in number!"%cricket.i
tems()
2
3 print sentence
TypeError: list indices must be integers, not str
NOTE: Dictionaries can't be indexed using positions
In [337]: [Link]()
Out[337]: [('count', 11), ('players', 'Batsmen and Bowlers')]
[Link] 118/338
13/11/2016 python Basic and Intermediate Level Modules
In [339]: sentence = "The %s in cricket team are %d in number!"%[Link]()[0]
print sentence
The count in cricket team are 11 in number!
In [340]: sentence = "The %r in cricket team are %r in number!"%[Link]()[0]
print sentence
The 'count' in cricket team are 11 in number!
In [347]: for i in range(len([Link]())):
tmp = [Link]()[i]
print "The %r in cricket team are %r in number!"%tmp
The 'count' in cricket team are 11 in number!
The 'players' in cricket team are 'Batsmen and Bowlers' in number!
But, there is logical error
In [348]: (cricket['players'], cricket['count'])
Out[348]: ('Batsmen and Bowlers', 11)
In [349]: print "The %r in cricket team are %r in number!"%(cricket['players'],
cricket['count'])
The 'Batsmen and Bowlers' in cricket team are 11 in number!
In [351]: [Link]() # results in empty dictionary object
In [352]: dict1
Out[352]: {}
In [353]: del dict1 # deletes the object
In [354]: dict1
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐354‐8239e7494a4a> in <module>()
‐‐‐‐> 1 dict1
NameError: name 'dict1' is not defined
dictonary keys should be immutable
[Link] 119/338
13/11/2016 python Basic and Intermediate Level Modules
Interview Question : what is the result of the below statement:
dict1 = {[1,2,3]: 'numbers'}
In [355]: dict1 = {[1,2,3]: 'numbers'}
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐355‐b12b39d823ae> in <module>()
‐‐‐‐> 1 dict1 = {[1,2,3]: 'numbers'}
TypeError: unhashable type: 'list'
In [356]: dict1 = {(1,2,3): 'numbers'} #possible, as tuple is immutable
In [357]: dict1 = {"1,2,3": 'numbers'} # possible, as string is immutable
In [358]: dict1 = {123: 'numbers'} # possible, as int is immutable
In [359]: dict1
Out[359]: {123: 'numbers'}
In [360]: dict1 = {{1,2,3}: 'numbers'} # Not possible, as set is mutable
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐360‐7755ecdb2543> in <module>()
‐‐‐‐> 1 dict1 = {{1,2,3}: 'numbers'} # Not possible, as set is mutable
TypeError: unhashable type: 'set'
In [361]: dict1 = {frozenset({1,2,3}): 'numbers'} # possible, as frozen set is immutab
le
In [362]: dict1
Out[362]: {frozenset({1, 2, 3}): 'numbers'}
In [363]: dict1 = {3: 'numbers'} # on integers
In [364]: dict1 = {3.333: 'numbers'} # on floats
In [365]: dict1 = {True: 'numbers'} # on booleans
Interview Question : what is the result of dict1 = {True: 'numbers', 1: 'one', 2.0: 'two', 3.333: 'three'}
In [366]: dict1 = {True: 'numbers', 1: 'one', 2.0: 'two', 2.0: '2.0', 3.333: 'three'}
[Link] 120/338
13/11/2016 python Basic and Intermediate Level Modules
In [367]: dict1
Out[367]: {True: 'one', 2.0: '2.0', 3.333: 'three'}
NOTE: As 'True' is a python object, it is preferred.
COPY in dictionaries
In [368]: dictHardCopy = dict1 # Hard COPY (assignment operation)
In [369]: dictCopy = [Link]() # soft COPY
In [370]: print dict1, '\n', dictHardCopy, '\n', dictCopy
{True: 'one', 2.0: '2.0', 3.333: 'three'}
{True: 'one', 2.0: '2.0', 3.333: 'three'}
{True: 'one', 2.0: '2.0', 3.333: 'three'}
In [371]: dict1 == dictHardCopy == dictCopy
Out[371]: True
In [372]: dict1 is dictHardCopy is dictCopy
Out[372]: False
In [373]: dictHardCopy[3.333] = '3333333' # updating the key, not position
In [374]: dictCopy[2.0] = '2222222' # indexing is done with key, and not index here
In [375]: print dict1, '\n', dictHardCopy, '\n', dictCopy
{True: 'one', 2.0: '2.0', 3.333: '3333333'}
{True: 'one', 2.0: '2.0', 3.333: '3333333'}
{True: 'one', 2.0: '2222222', 3.333: 'three'}
In [382]: mDict = {True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333:
'three'} } }
In [383]: mdictCopy = [Link]() # soft COPY
In [384]: mDict[3.333][3.333][3.333]
Out[384]: 'three'
In [385]: mDict[3.333][3.333][3.333] = 9999
[Link] 121/338
13/11/2016 python Basic and Intermediate Level Modules
In [386]: print mDict, '\n', mdictCopy
{True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333: 9999}}}
{True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333: 9999}}}
In [387]: import copy
mDictDeepCopy = [Link](mDict)
In [388]: print mDict, '\n', mDictDeepCopy
{True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333: 9999}}}
{True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333: 9999}}}
In [391]: mDict[3.333][3.333][True]
Out[391]: 'one'
In [392]: mDict[3.333][3.333][1] #Observe that both are resulting the same.
Out[392]: 'one'
In [393]: mDict[3.333][3.333][True] = 1111
In [394]: print mDict, '\n', mDictDeepCopy
{True: 'one', 3.333: {True: 'one', 3.333: {True: 1111, 3.333: 9999}}}
{True: 'one', 3.333: {True: 'one', 3.333: {True: 'one', 3.333: 9999}}}
In [395]: sorted(mDict)
Out[395]: [True, 3.333]
In [396]: sorted([Link]())
Out[396]: [True, 3.333]
In [397]: sorted([Link]())
Out[397]: [{True: 'one', 3.333: {True: 1111, 3.333: 9999}}, 'one']
In [398]: sorted([Link]())
Out[398]: [(True, 'one'), (3.333, {True: 'one', 3.333: {True: 1111, 3.333: 9999}})]
In [400]: fruits = {'a': 'apple', 'b': 'banana', 'd': 'donut'} # observe the common keys
in fruits and countries
countries = {'a': 'america', 'b': 'bahamas', 'c':'canada'}
In [401]: print fruits
{'a': 'apple', 'b': 'banana', 'd': 'donut'}
[Link] 122/338
13/11/2016 python Basic and Intermediate Level Modules
In [402]: [Link](countries)
In [403]: print fruits
{'a': 'america', 'c': 'canada', 'b': 'bahamas', 'd': 'donut'}
In [404]: fruits = {'a': 'apple', 'b': 'banana', 'd': 'donut'} # re‐initializing
In [405]: print countries
{'a': 'america', 'c': 'canada', 'b': 'bahamas'}
In [406]: [Link](fruits)
print countries # observe that as there is no key 'c' in fruits, i
t is not updated
{'a': 'apple', 'c': 'canada', 'b': 'banana', 'd': 'donut'}
creating dictionaries from lists
In [407]: countries = ['India', 'US', 'UK', 'Germany']
capitals = ['New Delhi', 'Washington', 'London', 'Berlin']
In [408]: countriesNcaptitals = zip(countries, capitals) # zip() builtin function; ret
urns list of tuples
print countriesNcaptitals
print type(countriesNcaptitals)
[('India', 'New Delhi'), ('US', 'Washington'), ('UK', 'London'), ('Germany',
'Berlin')]
<type 'list'>
Interview Question : How to create a list of tuples?
Ans: Using zip, map
In [409]: cncDictionary = dict(countriesNcaptitals) # dict() builtin function to create
dictionary
print type(cncDictionary)
print cncDictionary
<type 'dict'>
{'Germany': 'Berlin', 'India': 'New Delhi', 'UK': 'London', 'US': 'Washingto
n'}
[Link] 123/338
13/11/2016 python Basic and Intermediate Level Modules
In [410]: dict(zip(['India', 'US', 'UK', 'Germany'], ['New Delhi', 'Washington', 'Londo
n', 'Berlin']))
Out[410]: {'Germany': 'Berlin', 'India': 'New Delhi', 'UK': 'London', 'US': 'Washingto
n'}
In [411]: d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'a': 1, 'b': 2, 'c': 3}
cmp(d1,d2)
Out[411]: 0
In [412]: d2['d'] = 123;
print d2
{'a': 1, 'c': 3, 'b': 2, 'd': 123}
In [413]: cmp(d1, d2)
Out[413]: ‐1
In [414]: d1['d'] = '123'
d1['e'] = '345'
cmp(d1,d2)
Out[414]: 1
In [415]: len(d1), len(d2)
Out[415]: (5, 4)
In [416]: print d1
{'a': 1, 'c': 3, 'b': 2, 'e': '345', 'd': '123'}
In [417]: d4 ={}
[Link](d1) # to extract the keys of d1, and place them for d4
Out[417]: {'a': None, 'b': None, 'c': None, 'd': None, 'e': None}
In [418]: d5 ={}
[Link](d1, 'Python') # To place a default value, instead of None
Out[418]: {'a': 'Python', 'b': 'Python', 'c': 'Python', 'd': 'Python', 'e': 'Python'}
NOTE: dictionary Values can't be extracted in the same way
[Link] 124/338
13/11/2016 python Basic and Intermediate Level Modules
In [419]: print dir(d1)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc
__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__
gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__
ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalue
s', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems',
'viewkeys', 'viewvalues']
In [420]: print d1
{'a': 1, 'c': 3, 'b': 2, 'e': '345', 'd': '123'}
In [421]: [Link]('a')
Out[421]: 1
In [422]: [Link]('A')
In [423]: [Link]('A', 'Not present')
Out[423]: 'Not present'
In [424]: d1.has_key('a')
Out[424]: True
In [425]: d1.has_key('A')
Out[425]: False
Interview Question: what is the difference between dictionary attributes: pop() and popitem()
Assignment : Write a script to take the names of our five friends in a list, and their designations in a separate
list. ... Then, create a dictionary, containing their name, designation pairs
Assignment : Write a script to get the letter frequency from a given sentence. Display the top three most
occurred letters
In [426]: d1 = {'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '345'}
In [427]: [Link]('a', None) # works same as indexing a key, when key is present
Out[427]: 1
[Link] 125/338
13/11/2016 python Basic and Intermediate Level Modules
In [428]: d1
Out[428]: {'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '345'}
In [429]: [Link]('A', None) # works same as indexing a key, when key is present
In [430]: d1
Out[430]: {'A': None, 'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '345'}
In [431]: [Link]('A', 'not present') # As 'A' is present, None will be returned.
In [432]: d1
Out[432]: {'A': None, 'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '345'}
In [433]: [Link]('B', 'not present')
Out[433]: 'not present'
In [434]: d1
Out[434]: {'A': None, 'B': 'not present', 'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '34
5'}
In [435]: [Link]('B', 'someThing')
Out[435]: 'not present'
In [436]: d1
Out[436]: {'A': None, 'B': 'not present', 'a': 1, 'b': 2, 'c': 3, 'd': '123', 'e': '34
5'}
In [437]: [Link]() # results a list
Out[437]: [1, None, 3, 2, '345', '123', 'not present']
In [438]: print type([Link]())
<type 'list'>
In [439]: [Link]() # results a dict_item
Out[439]: dict_values([1, None, 3, 2, '345', '123', 'not present'])
In [440]: print type([Link]())
<type 'dict_values'>
[Link] 126/338
13/11/2016 python Basic and Intermediate Level Modules
In [441]: [Link]()
Out[441]: [('a', 1),
('A', None),
('c', 3),
('b', 2),
('e', '345'),
('d', '123'),
('B', 'not present')]
In [442]: [Link]()
Out[442]: dict_items([('a', 1), ('A', None), ('c', 3), ('b', 2), ('e', '345'), ('d', '1
23'), ('B', 'not present')])
In [443]: dictContent = [Link]() # returns items as a generator; need to iterate
with next() to get the items
print type(dictContent)
<type 'dictionary‐itemiterator'>
In [444]: print dictContent
<dictionary‐itemiterator object at 0x0414F8A0>
In [445]: print [Link]()
('a', 1)
[Link]() and [Link]() will work in the same way.
In [446]: d1= {True: 'one', 2: '2222', 3: 'three', 'b': 'ball'}
In [447]: print [Link](2) # 2 here is key, not position
2222
In [448]: print [Link]()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐448‐16d02b4d0b6a> in <module>()
‐‐‐‐> 1 print [Link]()
TypeError: pop expected at least 1 arguments, got 0
[Link] 127/338
13/11/2016 python Basic and Intermediate Level Modules
In [449]: print [Link]('abcd')
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
KeyError Traceback (most recent call last)
<ipython‐input‐449‐6973f3cda490> in <module>()
‐‐‐‐> 1 print [Link]('abcd')
KeyError: 'abcd'
In [450]: print [Link]('abcd', None) # returns None, if the key is not present
None
In [451]: print [Link]('abcd', "No Such Key") # To return default statement, in the abse
nce of key
No Such Key
In [49]: d1 = {True: 'one', 2: '2222', 3: 'three', 'b': 'ball'}
In [50]: key,value = [Link]() # deletes a random key‐pair and retuns them
print key, value
True one
In [51]: [Link]()
Out[51]: (2, '2222')
In [52]: key,value = [Link]()
print key,value
3 three
In [53]: key,value = [Link](); print key,value
b ball
In [54]: key,value = [Link](); print key,value
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
KeyError Traceback (most recent call last)
<ipython‐input‐54‐b91033ef2679> in <module>()
‐‐‐‐> 1 key,value = [Link](); print key,value
KeyError: 'popitem(): dictionary is empty'
In [55]: d = {'a':1, 'b':2}
[Link] 128/338
13/11/2016 python Basic and Intermediate Level Modules
In [56]: [Link]('a', 34) # As key 'a' is present, it is resulting in corresponding val
ue
Out[56]: 1
In [57]: [Link]('z', 34) # In the absence of key 'z', it results the second argument
Out[57]: 34
In [58]: print d # Observe that 'z' is not created
{'a': 1, 'b': 2}
In [59]: [Link]('z', 34)
Out[59]: 34
In [60]: print d # Observe that 'z' is created
{'a': 1, 'b': 2, 'z': 34}
[Link] 129/338
13/11/2016 python Basic and Intermediate Level Modules
In [5]: # [Link]
"""
Purpose: To count the number of times, each character occurred in the
sentence.
Output: Each character and its occurrence count, as a pair.
"""
#sentence = "It always seem impossible, until it is achieved!"
sentence = raw_input("Enter a Quote: ")
count = {} # empty dictionary
for character in sentence:
count[character] = [Link](character, 0) + 1
print "character: occurrenceFrequency \n"
#print count
#for key,value in [Link]():
# print key, value
for item in [Link]():
print item
#for index, item in enumerate([Link]()):
# print index, item
Enter a Quote: This is right time to re‐concentrate my energies, to achieve s
uccess
character: occurrenceFrequency
('a', 2)
(' ', 10)
('c', 5)
('e', 10)
('g', 2)
('i', 6)
('h', 3)
('‐', 1)
('m', 2)
(',', 1)
('o', 3)
('n', 3)
('s', 6)
('r', 4)
('u', 1)
('T', 1)
('v', 1)
('y', 1)
('t', 6)
Assignment : In this [Link] example, try to display first three most frequently
occurred characters
[Link] 130/338
13/11/2016 python Basic and Intermediate Level Modules
Memoization
To store the values which are already compiled, in cache, to optimize the time consumption
Interview Question : What is memoization. How to achieve it in dictionaries?
In [8]: alreadyknown = {0: 0, 1: 1} # Memoization
def fib(n):
if n not in alreadyknown:
new_value = fib(n‐1) + fib(n‐2)
alreadyknown[n] = new_value
return alreadyknown[n]
print "fib(20) = ", fib(20)
# fib(20)
#alreadyknown[20] = fib(19) + fib(18)
fib(20) = 6765
Ordered Dictionary
In [61]: d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} # regular unsorted dict
ionary
print d
{'orange': 2, 'pear': 1, 'banana': 3, 'apple': 4}
In [62]: import collections
In [63]: [Link]({'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2})
Out[63]: OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4)])
In [64]: [Link](sorted([Link](), key=lambda t: t[0])) # Sorted by key
[(k1,v1),(k2,v2)]
Out[64]: OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
In [11]: [Link](sorted([Link](), key=lambda t: t[1])) # Sorted by Val
ue
Out[11]: OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
Interview Question : How to sort a dictionary based on the length of the key?
[Link] 131/338
13/11/2016 python Basic and Intermediate Level Modules
In [12]: [Link](sorted([Link](), key=lambda t: len(t[0]))) # Sorted b
y length of key .
Out[12]: OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
In [13]: [Link](sorted([Link](), key=lambda t: len(str(t[1])))) # Sor
ted by length of Value; Observe no change
Out[13]: OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4)])
In [14]: [Link]([Link]())
Out[14]: OrderedDict([('orange', 2), ('pear', 1), ('banana', 3), ('apple', 4)])
Assignment : Use [Link] to get an ordered dictionary. Try to do some example
5.0 Functions
To reduce the code duplication for repetive logic
Modularizing the complex problem, into simpler pieces.
For better code reusage
Information Hiding
Functions in Python are called Firstclass citizens.
Function object have equal status as other objects
Functions too, can be assigned to variables, stored in collections (list, tuple, dictionary, set)
or can be passed as arguments.
Functions are of two types:
1. Builtfunctions
2. userdefined functions
Syntax:
def functionName(argument1, arguments2, ... ):
statement1
statement2
....
return expression/Value # return is optional, None object goes when return is
not present.
Functions without input arguments
In [15]: def helloWorld(): # function definition
print "Hello World!!!"
[Link] 132/338
13/11/2016 python Basic and Intermediate Level Modules
In [16]: type(helloWorld)
Out[16]: function
In [17]: helloWorld() # function call
Hello World!!!
In [18]: hw = helloWorld() # assigning the result of function call
print type(hw), hw
Hello World!!!
<type 'NoneType'> None
NOTE: Observe that the result(assignment) is NoneType beacuse the default return is NoneType.
Problem : write a function to get all the team members for a new project in runtime, and display them along
with their count.
In [19]: def teamMembers():
team = raw_input('Enter the names of team members: ')
print team, type(team)
teamMembers() # function call
Enter the names of team members: Shoban, Shankar, Naseer
Shoban, Shankar, Naseer <type 'str'>
In [20]: def teamMembers():
team = raw_input('Enter the names of team members: ')
print team, type(team)
print len(team)
print [Link](',')
print "There are ", len([Link](',')), " members in the team"
teamMembers() # function call
Enter the names of team members: Shoban, Shankar, Naseer Shiek
Shoban, Shankar, Naseer Shiek <type 'str'>
29
['Shoban', ' Shankar', ' Naseer Shiek']
There are 3 members in the team
Functions with inputs
Problem : Write a script to print the greater number, among two numbers
[Link] 133/338
13/11/2016 python Basic and Intermediate Level Modules
In [21]: def greaterNumber(a,b):
if a > b:
print "%d is greater than %d"%(a,b)
elif a < b:
print "%d is lesser than %d"%(a,b)
else:
print "%d is equal to %d"%(a,b)
greaterNumber(‐32, ‐12)
greaterNumber(32, ‐12)
greaterNumber(‐32, 12)
‐32 is lesser than ‐12
32 is greater than ‐12
‐32 is lesser than 12
Problem : Displaying a given name
In [22]: def displayName(name):
print "The name of the candidate is %r"%(name)
displayName('Naseer')
The name of the candidate is 'Naseer'
Scope: Local and Global Variables
In [23]: a = 10 # 'a' is an integer ‐ immutable
print "Initially, before going to function, a = %d"%(a)
def localEx(a):
print "In localEx() function, a = %d"%(a)
a= 5
print "In localEx() function, a = %d"%(a)
localEx(a)
print "Outside: a = %d"%(a)
Initially, before going to function, a = 10
In localEx() function, a = 10
In localEx() function, a = 5
Outside: a = 10
Inference: Changes made to the variable within function, does affect outside it
Interview Question : Explain the difference between global and local values, with an example?
[Link] 134/338
13/11/2016 python Basic and Intermediate Level Modules
global : It is a keyword to reflect the local changes (within the function) to the global level
In [24]: global a # global declaration
a = 10
def globalEx(): # Observe that 'a' is not passed as input
global a # global declaration
print "In globalEx() function, a = %d"%(a)
a= 5
print "In globalEx() function, a = %d"%(a)
globalEx()
print "Outside: a = %d"%(a)
In globalEx() function, a = 10
In globalEx() function, a = 5
Outside: a = 5
In [28]: myDict = {'a': 'Apple', 'b': 'Bat'} # 'myDict' is dictionary ‐ mutable
print "Initially, before going to function, myDict =", myDict
def localEx(a):
print "In localEx() function, myDict =", myDict
myDict['z'] = 'ZOO'
print "In localEx() function, myDict =", myDict
localEx(a)
print "Outside: myDict =", myDict
Initially, before going to function, myDict = {'a': 'Apple', 'b': 'Bat'}
In localEx() function, myDict = {'a': 'Apple', 'b': 'Bat'}
In localEx() function, myDict = {'a': 'Apple', 'b': 'Bat', 'z': 'ZOO'}
Outside: myDict = {'a': 'Apple', 'b': 'Bat', 'z': 'ZOO'}
Inference: Changes made to the object within function, gets effected outside the function, if the object is of
mutable type; else in immutable objects no change is reflected
Default and Keyword arguments
In [2]: def hello(name = "World!!!"): # name is having a default input argument
print "Hello ", name
In [3]: hello() # In the absence of input argument, It will display th
e default input argument
Hello World!!!
In [4]: hello('Shoban')
Hello Shoban
[Link] 135/338
13/11/2016 python Basic and Intermediate Level Modules
In [5]: hello(name = 'Shoban Babu!!!')
Hello Shoban Babu!!!
In [6]: def cricket(balls = 3, bats = 2):
print "There are %d balls and %d bats"%(balls, bats)
In [7]: cricket.func_defaults
Out[7]: (3, 2)
In [8]: cricket()
There are 3 balls and 2 bats
In [9]: cricket(4,5) # taken based on position
There are 4 balls and 5 bats
In [10]: cricket(balls = 5, bats= 4) # taken based on assignment
There are 5 balls and 4 bats
In [11]: cricket(bats = 5, balls= 40) # taken based on assignment
There are 40 balls and 5 bats
In [335]: def nTimesDisplay(message, noOfTimes = 3):
print message*noOfTimes
Here, as only one argument is default, the other should be given mandatorily.
NOTE: The default arguments must be placed in the last in the function definition.
In [14]: nTimesDisplay('Python ')
Python Python Python
In [15]: nTimesDisplay('Python ', 5)
Python Python Python Python Python
In [16]: nTimesDisplay(5,'Python ') # It identified based on the data type
Python Python Python Python Python
[Link] 136/338
13/11/2016 python Basic and Intermediate Level Modules
In [337]: nTimesDisplay(5.0,'Python ')
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐337‐9cea01eb6299> in <module>()
‐‐‐‐> 1 nTimesDisplay(5.0,'Python ')
<ipython‐input‐335‐dcd91946b6b5> in nTimesDisplay(message, noOfTimes)
1 def nTimesDisplay(message, noOfTimes = 3):
‐‐‐‐> 2 print message*noOfTimes
TypeError: can't multiply sequence by non‐int of type 'float'
In [17]: def funcExpressions(a,b = 12, c = 123):
print "The value of a is %d"%(a)
print "The value of b is %d"%(b)
print "The value of c is %d"%(c)
In [18]: funcExpressions(23)
The value of a is 23
The value of b is 12
The value of c is 123
In [19]: funcExpressions(23, c= 999)
The value of a is 23
The value of b is 12
The value of c is 999
In [20]: funcExpressions(c = 123, a = 234)
The value of a is 234
The value of b is 12
The value of c is 123
return statement
By default, userdefined functions will return 'None'
In [21]: def evenOddTest(a):
result = a % 2
if result == 0:
return '%d is an even number'%(a)
else:
return '%d is an odd number'%(a)
In [22]: evenOddTest(223)
Out[22]: '223 is an odd number'
[Link] 137/338
13/11/2016 python Basic and Intermediate Level Modules
In [23]: print evenOddTest(223)
223 is an odd number
In [24]: eot = evenOddTest(223)
print eot, type(eot)
223 is an odd number <type 'str'>
DocStrings
Essential for specifying something about the function
Enclosed within ''' ''' or """ """
Docstrings are different from comments
In [25]: def evenOddTest(a):
'''
Purpose: To validate the even‐ness or odd‐ness of a given integer.
Input: Variable a
Input Type: Integer
Output: result statement
Output Type: String
'''
result = a%2 # For even values, result is zero
# checking value equivalence with result
if result == 0:
return '%d is an even number'%(a)
else:
return '%d is an odd number'%(a)
In [26]: print type(evenOddTest)
<type 'function'>
In [27]: print evenOddTest.func_doc
Purpose: To validate the even‐ness or odd‐ness of a given integer.
Input: Variable a
Input Type: Integer
Output: result statement
Output Type: String
In [28]: print dir(evenOddTest)
['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delat
tr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '_
_globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__
reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str_
_', '__subclasshook__', 'func_closure', 'func_code', 'func_defaults', 'func_d
ict', 'func_doc', 'func_globals', 'func_name']
[Link] 138/338
13/11/2016 python Basic and Intermediate Level Modules
In [29]: evenOddTest.func_name
Out[29]: 'evenOddTest'
In [31]: print evenOddTest.func_defaults # prints None, as there are no default arg
uments defined
None
Variable Arguments (*args and **kwargs)
In [33]: #!/usr/bin/python
'''
Purpose: Demonstration of the usage of *args and **kwargs
*args stores variables in tuple
**kwargs stores variables in dictionaries
'''
def foo(firstArg, *args, **kwargs):
print 'Necessary argument is ', firstArg
print 'args = ', args
print 'type(args) is', type(args)
if args:
for arg in args:
print arg
print 'kwargs = ', kwargs
print 'type(kwargs) is', type(kwargs)
if kwargs:
print "The keyword arguments are\n",
# for kwarg in kwargs:
# print kwarg
for k,v in [Link]():
print '%15r ‐‐> %10r'%(k,v)
In [34]: foo(321)
Necessary argument is 321
args = ()
type(args) is <type 'tuple'>
kwargs = {}
type(kwargs) is <type 'dict'>
[Link] 139/338
13/11/2016 python Basic and Intermediate Level Modules
In [35]: foo(1,2,3,4)
Necessary argument is 1
args = (2, 3, 4)
type(args) is <type 'tuple'>
2
3
4
kwargs = {}
type(kwargs) is <type 'dict'>
In [36]: foo(99,22.0, True, [12,34, 56])
Necessary argument is 99
args = (22.0, True, [12, 34, 56])
type(args) is <type 'tuple'>
22.0
True
[12, 34, 56]
kwargs = {}
type(kwargs) is <type 'dict'>
In [37]: foo(32, 56, 32, (2, [34, (2.3, 99, 0)]))
Necessary argument is 32
args = (56, 32, (2, [34, (2.3, 99, 0)]))
type(args) is <type 'tuple'>
56
32
(2, [34, (2.3, 99, 0)])
kwargs = {}
type(kwargs) is <type 'dict'>
In [38]: foo(2.0, a = 1, b=2, c=3)
Necessary argument is 2.0
args = ()
type(args) is <type 'tuple'>
kwargs = {'a': 1, 'c': 3, 'b': 2}
type(kwargs) is <type 'dict'>
The keyword arguments are
'a' ‐‐> 1
'c' ‐‐> 3
'b' ‐‐> 2
[Link] 140/338
13/11/2016 python Basic and Intermediate Level Modules
In [39]: foo(2, 2.0, a = 1, b=2, c=3)
Necessary argument is 2
args = (2.0,)
type(args) is <type 'tuple'>
2.0
kwargs = {'a': 1, 'c': 3, 'b': 2}
type(kwargs) is <type 'dict'>
The keyword arguments are
'a' ‐‐> 1
'c' ‐‐> 3
'b' ‐‐> 2
In [40]: foo('a', 1, None, a = 1, b = '2', c = 3)
Necessary argument is a
args = (1, None)
type(args) is <type 'tuple'>
1
None
kwargs = {'a': 1, 'c': 3, 'b': '2'}
type(kwargs) is <type 'dict'>
The keyword arguments are
'a' ‐‐> 1
'c' ‐‐> 3
'b' ‐‐> '2'
In [41]: foo(123, courseName = 'Python', currentStatus = '40% completed', studentList =
['Michel', 'Naseer', 'Johson', 'Shoban'])
Necessary argument is 123
args = ()
type(args) is <type 'tuple'>
kwargs = {'courseName': 'Python', 'currentStatus': '40% completed', 'student
List': ['Michel', 'Naseer', 'Johson', 'Shoban']}
type(kwargs) is <type 'dict'>
The keyword arguments are
'courseName' ‐‐> 'Python'
'currentStatus' ‐‐> '40% completed'
'studentList' ‐‐> ['Michel', 'Naseer', 'Johson', 'Shoban']
Lambda
In [43]: def printSquares(x):
for i in range(x):
print i**2,
printSquares(7)
0 1 4 9 16 25 36
[Link] 141/338
13/11/2016 python Basic and Intermediate Level Modules
In [44]: [lambda x:x**2 for x in range(7)]
Out[44]: [<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>,
<function __main__.<lambda>>]
In [45]: map(lambda x:x**2, range(7)) # map() builtin function, that takes function a
nd collection as input
Out[45]: [0, 1, 4, 9, 16, 25, 36]
In [46]: filter(lambda x:x%2 == 0, range(12)) # Observe that filter() takes a conditi
on, unlike map()
Out[46]: [0, 2, 4, 6, 8, 10]
Assignment : Using the above written logic, write evenOddTest function within the map() and get the even
values between 23 and 97
map()
In [47]: def double(number):
return number*2 # experession isn't returned, but the result is
In [48]: map(double, range(45, 56))
Out[48]: [90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110]
filter()
In [49]: def OddNessTest(number):
return number%2 != 0
In [50]: listOfValues = [12, 34, 56, 78, 923, 23]
In [51]: filter(OddNessTest, listOfValues)
Out[51]: [923, 23]
In [52]: map(OddNessTest, listOfValues)
Out[52]: [False, False, False, False, True, True]
[Link] 142/338
13/11/2016 python Basic and Intermediate Level Modules
Interview Question : Write a function to highlist the vowels in the input string
5.5 filter
In [1]: def OddNessTest(number):
return number%2 != 0
In [2]: listOfValues = [12, 34, 56, 78, 923, 23]
Interview Question : What is the difference between map() and filter()?
In [3]: filter(OddNessTest, listOfValues)
Out[3]: [923, 23]
In [4]: map(OddNessTest, listOfValues)
Out[4]: [False, False, False, False, True, True]
Interview Question : Write a function to highlist the vowels in the input string
In [5]: def highlightVowels(string):
vowels = 'ae1ouAEIOU'
for i in string:
if i in vowels:
print [Link](),
else:
print [Link](),
highlightVowels('I will give my best to achieve my goal!')
I w i l l g i v E m y b E s t t O A c h i E v E m y g O A l !
In [6]: def highLightVowels(string):
newString = []
for ch in string:
if [Link]() in 'aeiou':
[Link]([Link]())
else:
[Link]([Link]())
return ''.join(newString)
print highLightVowels('python programming language')
print highLightVowels('python programming language'.upper())
pythOn prOgrAmmIng lAngUAgE
pythOn prOgrAmmIng lAngUAgE
[Link] 143/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Do modifications to this logic, to remove the spaces between letters; not words
zip()
To pair the list given
Results in list of tuples
In [7]: zip('Python', 'Python')
Out[7]: [('P', 'P'), ('y', 'y'), ('t', 't'), ('h', 'h'), ('o', 'o'), ('n', 'n')]
In [8]: zip('python', xrange(len('python')))
Out[8]: [('p', 0), ('y', 1), ('t', 2), ('h', 3), ('o', 4), ('n', 5)]
In [9]: zip(xrange(len('python')), 'Python')
Out[9]: [(0, 'P'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]
In [10]: zip('Python')
Out[10]: [('P',), ('y',), ('t',), ('h',), ('o',), ('n',)]
In [11]: zip('Python', 'Python', 'python')
Out[11]: [('P', 'P', 'p'),
('y', 'y', 'y'),
('t', 't', 't'),
('h', 'h', 'h'),
('o', 'o', 'o'),
('n', 'n', 'n')]
Interview Question : What is the difference between map() and zip()?
In [12]: print map(None, range(10), range(12)) # None type function taken to pair th
e values in list
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9,
9), (None, 10), (None, 11)]
In [13]: print zip(range(10), range(12))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9,
9)]
NOTE: Observe that map() can handle lists of assymmetric lengths, whereas zip() can't
[Link] 144/338
13/11/2016 python Basic and Intermediate Level Modules
reduce()
It accepts an iterator to process, but it's not an iterator itself. It returns a single result
In [14]: reduce( (lambda a, b: a * b), [1, 2, 3, 4] ) # results in 1*2*3*4
Out[14]: 24
Interview Question : what is the difference between map() and reduce()?
In [15]: map( (lambda a, b: a * b), [1, 2, 3, 4] )
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐15‐c4e63925bfeb> in <module>()
‐‐‐‐> 1 map( (lambda a, b: a * b), [1, 2, 3, 4] )
TypeError: <lambda>() takes exactly 2 arguments (1 given)
In [16]: map( (lambda a, b: a * b), [1, 2, 3, 4], [1, 2, 3, 4] )
Out[16]: [1, 4, 9, 16]
In [17]: reduce( (lambda a, b: a ** b), [1, 2, 3, 4] ) # results in 1**2**3**4; anyth
ing to the power of 1 results in 1
Out[17]: 1
In [18]: map( (lambda a, b: a ** b), [1, 2, 3, 4], [1, 2, 3, 4] )
Out[18]: [1, 4, 27, 256]
In [19]: reduce( (lambda a, b: a ** b), [2, 3, 4] ) # results in 2**3**4
Out[19]: 4096
Interview Question : Perform string concatenation using reduce()
In [20]: reduce((lambda a,b:a+b), ['python', 'programming'])
Out[20]: 'pythonprogramming'
In [21]: reduce((lambda a,b:a + ' ' + b), ['python', 'programming'])
Out[21]: 'python programming'
[Link] 145/338
13/11/2016 python Basic and Intermediate Level Modules
In [22]: string = 'I am confident about my Success'
list = [Link](' ')
print list
['I', 'am', 'confident', 'about', 'my', 'Success']
In [23]: reduce(lambda a,b: a+b, list)
Out[23]: 'IamconfidentaboutmySuccess'
In [24]: reduce(lambda a,b: a + ' ' + b, list) # with spaces between words
Out[24]: 'I am confident about my Success'
[Link] 146/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Write a script with two functions, celsiusToFahrenheit() and fahrenheitToCelsius(). In runtime,
the user should be given choice to enter either Fahrenheit or Celcius; then use the functions to convert
correspondingly. Then, display the result from main function, using string formatting?
Assignment : Write three functions absolute(). Prompt the user to feed an integer (). The result should
mimick the functionality of builtin function abs()
ex:
In [1]: abs(9)
Out[1]: 9
In [2]: abs(‐9)
Out[2]: 9
In [3]: abs(0)
Out[3]: 0
In [4]: abs(2+3j)
Out[4]: 3.6055512754639896
Assignment : Write a function that validates and prints whether the runtime input given is palindrome or not.
Ex: 'otto'
'Evil is a deed as I live' Hint: ignore white‐space here.
Assignment : write a function that generates palindromes, for the given runtime input. There should be two
outputs, both for even and odd case. Ref : [Link]
([Link]
Assignment : Write a function to implement the caesar cipher, for the given input.
ex1: input ‐‐> 'Python'
output ‐‐> 'Qzuipo'
ex2: input ‐‐> 'One day, I will achieve my goal!'
output ‐> 'Pof!ebz‐!J!xjmm!bdijfwf!nz!hpbm"'
HINT: use chr() and ord() builtin functions
Assignment : Define a function histogram() that takes a list of integers and prints a histogram to the screen.
For example, histogram([2, 5, 7]) should print:
**
*****
*******
Assignment : Write a function to get the fibinocci series of numbers till 100; and another function to print the
corresponding number of astericks. Hint: 0, 1, 1, 2, 3, 5, 8, ...
[Link] 147/338
13/11/2016 python Basic and Intermediate Level Modules
Output expected :
*
*
**
***
*****
********
...so on
Assignment : Write a function to calculate the factorial of a number.
Hint: factorial(4) = 4*3*2*1 = 24
Recursive functions
def funcName(<input paramaters>):
<some logic>
return funcName(<input parameters>)
Recursion is a programming technique in which a call to a function results in another call to that same
function. Iteration is calling an object, and moving over it.
Problem : Return the fibonacci series (0, 1, 1, 2, 3, 5, 8) using recursions
In [25]: def fib(n): # method 1 recursion
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n‐1)+fib(n‐2)
print fib(20)
6765
In [26]: fib(5) # 5th element # fib(4)+fib(3)
# fib(4) ‐> fib(3)+fib(2)
# fib ...
Out[26]: 5
In [27]: fib(6) # 6th element
Out[27]: 8
Assignment : Using recursive functions, generate the fibonacci series and display all the series still a given
number
[Link] 148/338
13/11/2016 python Basic and Intermediate Level Modules
Problem : Write a recursive function to get the factorial of a given number
In [28]: reduce((lambda x,y: x*y), xrange(1,12)) # method 2
Out[28]: 39916800
In [29]: def factorial(n): # method 3 recursion
if n == 1 or 0:
return 1
return n * factorial(n‐1)
factorial(12‐1)
Out[29]: 39916800
In [30]: factorial(5) # 5*3*2*1*1
Out[30]: 120
In [31]: def factorial(n): # same logic, but consolidated
# method 4 recursion
return 1 if n==0 else n*factorial(n‐1)
In [32]: factorial(12‐1)
Out[32]: 39916800
In [33]: def factorial(n): # same logic, but consolidated
# method 5 recursion
n = abs(n)
return 1 if n==0 else n*factorial(n‐1)
In [34]: factorial(11)
Out[34]: 39916800
In [35]: factorial(‐11)
Out[35]: 39916800
In [36]: def factorial(n): # method
6 recursion
if n == 0:
return 1
elif n <0:
return n*factorial(n+1)
else:
return n*factorial(n‐1)
In [37]: factorial(0)
Out[37]: 1
[Link] 149/338
13/11/2016 python Basic and Intermediate Level Modules
In [38]: factorial(11)
Out[38]: 39916800
In [39]: factorial(‐11)
Out[39]: ‐39916800
Assignment : using timeit module, conclude which of the above factorial() functions, is faster?
Interview Question : write a recursive function to display the reverse of a given string, using recursive
function.
ex:'Python' > 'nohtyP'
In [40]: def stringReverse(string): # method 1
if string == '':
return ''
else:
return stringReverse(string[1:]) + string[0]
stringReverse('Python Programming')
Out[40]: 'gnimmargorP nohtyP'
In [41]: def stringReverse(string): # method 2
if string == '':
return ''
return string[‐1]+stringReverse(string[:‐1])
stringReverse('Python')
Out[41]: 'nohtyP'
Assignment : Modify this stringReverse() recursive function to result as below:
'Python Programming' > 'nohtyP gnimmargorP'
NOTE: We also know that string reversal is possible using slicing (str[::1]) and using builtin function
reversed()
Problem : Write a recursive function to display the sum of squares of whole numbers, till given number, n.
1**2 +2**2 + 3**2 + ...
[Link] 150/338
13/11/2016 python Basic and Intermediate Level Modules
In [42]: def squareSum(n):
if n == 0:
return 0
else:
return pow(n,2) + squareSum(n‐1)
squareSum(4) # 0**2 + 1**2 + 2**2 + 3**2 + 4**2
Out[42]: 30
Assignmnet : Write a recursive function to compute the sum of first 'n' whole number
Hint : 10 > 10+9+8+ ......1+0
if n is 0, return 0
Assignment : Write a recursive function which displays the Pascal's triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
HINT: use fibonacci series
Assignments (Interview Question): Implement a recursive function to multiply and divide 2 numbers
recursively using + and operators only.
Assignment : Write a recursive function to display first 50 prime numbers
Assignment : WAP to display the sum of digits of a number Hint: n//10, n%10
Problem : Create an infinite loop using recursive functions
In [43]: global noOfRecursions
noOfRecursions = 0
def loop(noOfRecursions): # Infinite loop
print 'Hi! I am in Loop '
global noOfRecursions
noOfRecursions+=1 # to get the count of number of recursions
occurred
print 'This is Loop %d'%noOfRecursions
return loop(noOfRecursions)
File "<ipython‐input‐43‐c4e39b05cf08>", line 3
def loop(noOfRecursions): # Infinite loop
SyntaxError: name 'noOfRecursions' is local and global
[Link] 151/338
13/11/2016 python Basic and Intermediate Level Modules
In [44]: loop(noOfRecursions)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐44‐479a6f6d0cf4> in <module>()
‐‐‐‐> 1 loop(noOfRecursions)
NameError: name 'loop' is not defined
In [45]: print noOfRecursions # It results the default value given before entering th
e function, as returns from subframes was halted.
0
Notice that in python, infinite loop will not run for infinite time. But, they gets halted after reaching the
maximum recursion depth.
Interview question : what is the maximum recursion depth of any python object? Does it differ for different
types of objects? Is it dependent on the machine being used?
Problem : Write a function to demonstrate the MUTUAL recursion between two functions
In [46]: # Infinite loop between these functions ‐‐‐ Mutual recursion
global noOfloops
noOfloops = 0
def func1():
print 'I am in function 1 .'
global noOfloops
noOfloops+=1
print noOfloops
return func2()
def func2():
print 'I am in function 2 .'
global noOfloops
noOfloops+=1
print noOfloops
return func1()
[Link] 152/338
13/11/2016 python Basic and Intermediate Level Modules
In [47]: func1()
[Link] 153/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 154/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
1
I am in function 2 .
2
I am in function 1 .
3
I am in function 2 .
4
I am in function 1 .
5
I am in function 2 .
6
I am in function 1 .
7
I am in function 2 .
8
I am in function 1 .
9
I am in function 2 .
10
I am in function 1 .
11
I am in function 2 .
12
I am in function 1 .
13
I am in function 2 .
14
I am in function 1 .
15
I am in function 2 .
16
I am in function 1 .
17
I am in function 2 .
18
I am in function 1 .
19
I am in function 2 .
20
I am in function 1 .
21
I am in function 2 .
22
I am in function 1 .
23
I am in function 2 .
24
I am in function 1 .
25
I am in function 2 .
26
I am in function 1 .
27
I am in function 2 .
28
I am in function 1 .
[Link] 155/338
13/11/2016 python Basic and Intermediate Level Modules
29
I am in function 2 .
30
I am in function 1 .
31
I am in function 2 .
32
I am in function 1 .
33
I am in function 2 .
34
I am in function 1 .
35
I am in function 2 .
36
I am in function 1 .
37
I am in function 2 .
38
I am in function 1 .
39
I am in function 2 .
40
I am in function 1 .
41
I am in function 2 .
42
I am in function 1 .
43
I am in function 2 .
44
I am in function 1 .
45
I am in function 2 .
46
I am in function 1 .
47
I am in function 2 .
48
I am in function 1 .
49
I am in function 2 .
50
I am in function 1 .
51
I am in function 2 .
52
I am in function 1 .
53
I am in function 2 .
54
I am in function 1 .
55
I am in function 2 .
56
I am in function 1 .
57
[Link] 156/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
58
I am in function 1 .
59
I am in function 2 .
60
I am in function 1 .
61
I am in function 2 .
62
I am in function 1 .
63
I am in function 2 .
64
I am in function 1 .
65
I am in function 2 .
66
I am in function 1 .
67
I am in function 2 .
68
I am in function 1 .
69
I am in function 2 .
70
I am in function 1 .
71
I am in function 2 .
72
I am in function 1 .
73
I am in function 2 .
74
I am in function 1 .
75
I am in function 2 .
76
I am in function 1 .
77
I am in function 2 .
78
I am in function 1 .
79
I am in function 2 .
80
I am in function 1 .
81
I am in function 2 .
82
I am in function 1 .
83
I am in function 2 .
84
I am in function 1 .
85
I am in function 2 .
[Link] 157/338
13/11/2016 python Basic and Intermediate Level Modules
86
I am in function 1 .
87
I am in function 2 .
88
I am in function 1 .
89
I am in function 2 .
90
I am in function 1 .
91
I am in function 2 .
92
I am in function 1 .
93
I am in function 2 .
94
I am in function 1 .
95
I am in function 2 .
96
I am in function 1 .
97
I am in function 2 .
98
I am in function 1 .
99
I am in function 2 .
100
I am in function 1 .
101
I am in function 2 .
102
I am in function 1 .
103
I am in function 2 .
104
I am in function 1 .
105
I am in function 2 .
106
I am in function 1 .
107
I am in function 2 .
108
I am in function 1 .
109
I am in function 2 .
110
I am in function 1 .
111
I am in function 2 .
112
I am in function 1 .
113
I am in function 2 .
114
[Link] 158/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
115
I am in function 2 .
116
I am in function 1 .
117
I am in function 2 .
118
I am in function 1 .
119
I am in function 2 .
120
I am in function 1 .
121
I am in function 2 .
122
I am in function 1 .
123
I am in function 2 .
124
I am in function 1 .
125
I am in function 2 .
126
I am in function 1 .
127
I am in function 2 .
128
I am in function 1 .
129
I am in function 2 .
130
I am in function 1 .
131
I am in function 2 .
132
I am in function 1 .
133
I am in function 2 .
134
I am in function 1 .
135
I am in function 2 .
136
I am in function 1 .
137
I am in function 2 .
138
I am in function 1 .
139
I am in function 2 .
140
I am in function 1 .
141
I am in function 2 .
142
I am in function 1 .
[Link] 159/338
13/11/2016 python Basic and Intermediate Level Modules
143
I am in function 2 .
144
I am in function 1 .
145
I am in function 2 .
146
I am in function 1 .
147
I am in function 2 .
148
I am in function 1 .
149
I am in function 2 .
150
I am in function 1 .
151
I am in function 2 .
152
I am in function 1 .
153
I am in function 2 .
154
I am in function 1 .
155
I am in function 2 .
156
I am in function 1 .
157
I am in function 2 .
158
I am in function 1 .
159
I am in function 2 .
160
I am in function 1 .
161
I am in function 2 .
162
I am in function 1 .
163
I am in function 2 .
164
I am in function 1 .
165
I am in function 2 .
166
I am in function 1 .
167
I am in function 2 .
168
I am in function 1 .
169
I am in function 2 .
170
I am in function 1 .
171
[Link] 160/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
172
I am in function 1 .
173
I am in function 2 .
174
I am in function 1 .
175
I am in function 2 .
176
I am in function 1 .
177
I am in function 2 .
178
I am in function 1 .
179
I am in function 2 .
180
I am in function 1 .
181
I am in function 2 .
182
I am in function 1 .
183
I am in function 2 .
184
I am in function 1 .
185
I am in function 2 .
186
I am in function 1 .
187
I am in function 2 .
188
I am in function 1 .
189
I am in function 2 .
190
I am in function 1 .
191
I am in function 2 .
192
I am in function 1 .
193
I am in function 2 .
194
I am in function 1 .
195
I am in function 2 .
196
I am in function 1 .
197
I am in function 2 .
198
I am in function 1 .
199
I am in function 2 .
[Link] 161/338
13/11/2016 python Basic and Intermediate Level Modules
200
I am in function 1 .
201
I am in function 2 .
202
I am in function 1 .
203
I am in function 2 .
204
I am in function 1 .
205
I am in function 2 .
206
I am in function 1 .
207
I am in function 2 .
208
I am in function 1 .
209
I am in function 2 .
210
I am in function 1 .
211
I am in function 2 .
212
I am in function 1 .
213
I am in function 2 .
214
I am in function 1 .
215
I am in function 2 .
216
I am in function 1 .
217
I am in function 2 .
218
I am in function 1 .
219
I am in function 2 .
220
I am in function 1 .
221
I am in function 2 .
222
I am in function 1 .
223
I am in function 2 .
224
I am in function 1 .
225
I am in function 2 .
226
I am in function 1 .
227
I am in function 2 .
228
[Link] 162/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
229
I am in function 2 .
230
I am in function 1 .
231
I am in function 2 .
232
I am in function 1 .
233
I am in function 2 .
234
I am in function 1 .
235
I am in function 2 .
236
I am in function 1 .
237
I am in function 2 .
238
I am in function 1 .
239
I am in function 2 .
240
I am in function 1 .
241
I am in function 2 .
242
I am in function 1 .
243
I am in function 2 .
244
I am in function 1 .
245
I am in function 2 .
246
I am in function 1 .
247
I am in function 2 .
248
I am in function 1 .
249
I am in function 2 .
250
I am in function 1 .
251
I am in function 2 .
252
I am in function 1 .
253
I am in function 2 .
254
I am in function 1 .
255
I am in function 2 .
256
I am in function 1 .
[Link] 163/338
13/11/2016 python Basic and Intermediate Level Modules
257
I am in function 2 .
258
I am in function 1 .
259
I am in function 2 .
260
I am in function 1 .
261
I am in function 2 .
262
I am in function 1 .
263
I am in function 2 .
264
I am in function 1 .
265
I am in function 2 .
266
I am in function 1 .
267
I am in function 2 .
268
I am in function 1 .
269
I am in function 2 .
270
I am in function 1 .
271
I am in function 2 .
272
I am in function 1 .
273
I am in function 2 .
274
I am in function 1 .
275
I am in function 2 .
276
I am in function 1 .
277
I am in function 2 .
278
I am in function 1 .
279
I am in function 2 .
280
I am in function 1 .
281
I am in function 2 .
282
I am in function 1 .
283
I am in function 2 .
284
I am in function 1 .
285
[Link] 164/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
286
I am in function 1 .
287
I am in function 2 .
288
I am in function 1 .
289
I am in function 2 .
290
I am in function 1 .
291
I am in function 2 .
292
I am in function 1 .
293
I am in function 2 .
294
I am in function 1 .
295
I am in function 2 .
296
I am in function 1 .
297
I am in function 2 .
298
I am in function 1 .
299
I am in function 2 .
300
I am in function 1 .
301
I am in function 2 .
302
I am in function 1 .
303
I am in function 2 .
304
I am in function 1 .
305
I am in function 2 .
306
I am in function 1 .
307
I am in function 2 .
308
I am in function 1 .
309
I am in function 2 .
310
I am in function 1 .
311
I am in function 2 .
312
I am in function 1 .
313
I am in function 2 .
[Link] 165/338
13/11/2016 python Basic and Intermediate Level Modules
314
I am in function 1 .
315
I am in function 2 .
316
I am in function 1 .
317
I am in function 2 .
318
I am in function 1 .
319
I am in function 2 .
320
I am in function 1 .
321
I am in function 2 .
322
I am in function 1 .
323
I am in function 2 .
324
I am in function 1 .
325
I am in function 2 .
326
I am in function 1 .
327
I am in function 2 .
328
I am in function 1 .
329
I am in function 2 .
330
I am in function 1 .
331
I am in function 2 .
332
I am in function 1 .
333
I am in function 2 .
334
I am in function 1 .
335
I am in function 2 .
336
I am in function 1 .
337
I am in function 2 .
338
I am in function 1 .
339
I am in function 2 .
340
I am in function 1 .
341
I am in function 2 .
342
[Link] 166/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
343
I am in function 2 .
344
I am in function 1 .
345
I am in function 2 .
346
I am in function 1 .
347
I am in function 2 .
348
I am in function 1 .
349
I am in function 2 .
350
I am in function 1 .
351
I am in function 2 .
352
I am in function 1 .
353
I am in function 2 .
354
I am in function 1 .
355
I am in function 2 .
356
I am in function 1 .
357
I am in function 2 .
358
I am in function 1 .
359
I am in function 2 .
360
I am in function 1 .
361
I am in function 2 .
362
I am in function 1 .
363
I am in function 2 .
364
I am in function 1 .
365
I am in function 2 .
366
I am in function 1 .
367
I am in function 2 .
368
I am in function 1 .
369
I am in function 2 .
370
I am in function 1 .
[Link] 167/338
13/11/2016 python Basic and Intermediate Level Modules
371
I am in function 2 .
372
I am in function 1 .
373
I am in function 2 .
374
I am in function 1 .
375
I am in function 2 .
376
I am in function 1 .
377
I am in function 2 .
378
I am in function 1 .
379
I am in function 2 .
380
I am in function 1 .
381
I am in function 2 .
382
I am in function 1 .
383
I am in function 2 .
384
I am in function 1 .
385
I am in function 2 .
386
I am in function 1 .
387
I am in function 2 .
388
I am in function 1 .
389
I am in function 2 .
390
I am in function 1 .
391
I am in function 2 .
392
I am in function 1 .
393
I am in function 2 .
394
I am in function 1 .
395
I am in function 2 .
396
I am in function 1 .
397
I am in function 2 .
398
I am in function 1 .
399
[Link] 168/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
400
I am in function 1 .
401
I am in function 2 .
402
I am in function 1 .
403
I am in function 2 .
404
I am in function 1 .
405
I am in function 2 .
406
I am in function 1 .
407
I am in function 2 .
408
I am in function 1 .
409
I am in function 2 .
410
I am in function 1 .
411
I am in function 2 .
412
I am in function 1 .
413
I am in function 2 .
414
I am in function 1 .
415
I am in function 2 .
416
I am in function 1 .
417
I am in function 2 .
418
I am in function 1 .
419
I am in function 2 .
420
I am in function 1 .
421
I am in function 2 .
422
I am in function 1 .
423
I am in function 2 .
424
I am in function 1 .
425
I am in function 2 .
426
I am in function 1 .
427
I am in function 2 .
[Link] 169/338
13/11/2016 python Basic and Intermediate Level Modules
428
I am in function 1 .
429
I am in function 2 .
430
I am in function 1 .
431
I am in function 2 .
432
I am in function 1 .
433
I am in function 2 .
434
I am in function 1 .
435
I am in function 2 .
436
I am in function 1 .
437
I am in function 2 .
438
I am in function 1 .
439
I am in function 2 .
440
I am in function 1 .
441
I am in function 2 .
442
I am in function 1 .
443
I am in function 2 .
444
I am in function 1 .
445
I am in function 2 .
446
I am in function 1 .
447
I am in function 2 .
448
I am in function 1 .
449
I am in function 2 .
450
I am in function 1 .
451
I am in function 2 .
452
I am in function 1 .
453
I am in function 2 .
454
I am in function 1 .
455
I am in function 2 .
456
[Link] 170/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
457
I am in function 2 .
458
I am in function 1 .
459
I am in function 2 .
460
I am in function 1 .
461
I am in function 2 .
462
I am in function 1 .
463
I am in function 2 .
464
I am in function 1 .
465
I am in function 2 .
466
I am in function 1 .
467
I am in function 2 .
468
I am in function 1 .
469
I am in function 2 .
470
I am in function 1 .
471
I am in function 2 .
472
I am in function 1 .
473
I am in function 2 .
474
I am in function 1 .
475
I am in function 2 .
476
I am in function 1 .
477
I am in function 2 .
478
I am in function 1 .
479
I am in function 2 .
480
I am in function 1 .
481
I am in function 2 .
482
I am in function 1 .
483
I am in function 2 .
484
I am in function 1 .
[Link] 171/338
13/11/2016 python Basic and Intermediate Level Modules
485
I am in function 2 .
486
I am in function 1 .
487
I am in function 2 .
488
I am in function 1 .
489
I am in function 2 .
490
I am in function 1 .
491
I am in function 2 .
492
I am in function 1 .
493
I am in function 2 .
494
I am in function 1 .
495
I am in function 2 .
496
I am in function 1 .
497
I am in function 2 .
498
I am in function 1 .
499
I am in function 2 .
500
I am in function 1 .
501
I am in function 2 .
502
I am in function 1 .
503
I am in function 2 .
504
I am in function 1 .
505
I am in function 2 .
506
I am in function 1 .
507
I am in function 2 .
508
I am in function 1 .
509
I am in function 2 .
510
I am in function 1 .
511
I am in function 2 .
512
I am in function 1 .
513
[Link] 172/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
514
I am in function 1 .
515
I am in function 2 .
516
I am in function 1 .
517
I am in function 2 .
518
I am in function 1 .
519
I am in function 2 .
520
I am in function 1 .
521
I am in function 2 .
522
I am in function 1 .
523
I am in function 2 .
524
I am in function 1 .
525
I am in function 2 .
526
I am in function 1 .
527
I am in function 2 .
528
I am in function 1 .
529
I am in function 2 .
530
I am in function 1 .
531
I am in function 2 .
532
I am in function 1 .
533
I am in function 2 .
534
I am in function 1 .
535
I am in function 2 .
536
I am in function 1 .
537
I am in function 2 .
538
I am in function 1 .
539
I am in function 2 .
540
I am in function 1 .
541
I am in function 2 .
[Link] 173/338
13/11/2016 python Basic and Intermediate Level Modules
542
I am in function 1 .
543
I am in function 2 .
544
I am in function 1 .
545
I am in function 2 .
546
I am in function 1 .
547
I am in function 2 .
548
I am in function 1 .
549
I am in function 2 .
550
I am in function 1 .
551
I am in function 2 .
552
I am in function 1 .
553
I am in function 2 .
554
I am in function 1 .
555
I am in function 2 .
556
I am in function 1 .
557
I am in function 2 .
558
I am in function 1 .
559
I am in function 2 .
560
I am in function 1 .
561
I am in function 2 .
562
I am in function 1 .
563
I am in function 2 .
564
I am in function 1 .
565
I am in function 2 .
566
I am in function 1 .
567
I am in function 2 .
568
I am in function 1 .
569
I am in function 2 .
570
[Link] 174/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
571
I am in function 2 .
572
I am in function 1 .
573
I am in function 2 .
574
I am in function 1 .
575
I am in function 2 .
576
I am in function 1 .
577
I am in function 2 .
578
I am in function 1 .
579
I am in function 2 .
580
I am in function 1 .
581
I am in function 2 .
582
I am in function 1 .
583
I am in function 2 .
584
I am in function 1 .
585
I am in function 2 .
586
I am in function 1 .
587
I am in function 2 .
588
I am in function 1 .
589
I am in function 2 .
590
I am in function 1 .
591
I am in function 2 .
592
I am in function 1 .
593
I am in function 2 .
594
I am in function 1 .
595
I am in function 2 .
596
I am in function 1 .
597
I am in function 2 .
598
I am in function 1 .
[Link] 175/338
13/11/2016 python Basic and Intermediate Level Modules
599
I am in function 2 .
600
I am in function 1 .
601
I am in function 2 .
602
I am in function 1 .
603
I am in function 2 .
604
I am in function 1 .
605
I am in function 2 .
606
I am in function 1 .
607
I am in function 2 .
608
I am in function 1 .
609
I am in function 2 .
610
I am in function 1 .
611
I am in function 2 .
612
I am in function 1 .
613
I am in function 2 .
614
I am in function 1 .
615
I am in function 2 .
616
I am in function 1 .
617
I am in function 2 .
618
I am in function 1 .
619
I am in function 2 .
620
I am in function 1 .
621
I am in function 2 .
622
I am in function 1 .
623
I am in function 2 .
624
I am in function 1 .
625
I am in function 2 .
626
I am in function 1 .
627
[Link] 176/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
628
I am in function 1 .
629
I am in function 2 .
630
I am in function 1 .
631
I am in function 2 .
632
I am in function 1 .
633
I am in function 2 .
634
I am in function 1 .
635
I am in function 2 .
636
I am in function 1 .
637
I am in function 2 .
638
I am in function 1 .
639
I am in function 2 .
640
I am in function 1 .
641
I am in function 2 .
642
I am in function 1 .
643
I am in function 2 .
644
I am in function 1 .
645
I am in function 2 .
646
I am in function 1 .
647
I am in function 2 .
648
I am in function 1 .
649
I am in function 2 .
650
I am in function 1 .
651
I am in function 2 .
652
I am in function 1 .
653
I am in function 2 .
654
I am in function 1 .
655
I am in function 2 .
[Link] 177/338
13/11/2016 python Basic and Intermediate Level Modules
656
I am in function 1 .
657
I am in function 2 .
658
I am in function 1 .
659
I am in function 2 .
660
I am in function 1 .
661
I am in function 2 .
662
I am in function 1 .
663
I am in function 2 .
664
I am in function 1 .
665
I am in function 2 .
666
I am in function 1 .
667
I am in function 2 .
668
I am in function 1 .
669
I am in function 2 .
670
I am in function 1 .
671
I am in function 2 .
672
I am in function 1 .
673
I am in function 2 .
674
I am in function 1 .
675
I am in function 2 .
676
I am in function 1 .
677
I am in function 2 .
678
I am in function 1 .
679
I am in function 2 .
680
I am in function 1 .
681
I am in function 2 .
682
I am in function 1 .
683
I am in function 2 .
684
[Link] 178/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
685
I am in function 2 .
686
I am in function 1 .
687
I am in function 2 .
688
I am in function 1 .
689
I am in function 2 .
690
I am in function 1 .
691
I am in function 2 .
692
I am in function 1 .
693
I am in function 2 .
694
I am in function 1 .
695
I am in function 2 .
696
I am in function 1 .
697
I am in function 2 .
698
I am in function 1 .
699
I am in function 2 .
700
I am in function 1 .
701
I am in function 2 .
702
I am in function 1 .
703
I am in function 2 .
704
I am in function 1 .
705
I am in function 2 .
706
I am in function 1 .
707
I am in function 2 .
708
I am in function 1 .
709
I am in function 2 .
710
I am in function 1 .
711
I am in function 2 .
712
I am in function 1 .
[Link] 179/338
13/11/2016 python Basic and Intermediate Level Modules
713
I am in function 2 .
714
I am in function 1 .
715
I am in function 2 .
716
I am in function 1 .
717
I am in function 2 .
718
I am in function 1 .
719
I am in function 2 .
720
I am in function 1 .
721
I am in function 2 .
722
I am in function 1 .
723
I am in function 2 .
724
I am in function 1 .
725
I am in function 2 .
726
I am in function 1 .
727
I am in function 2 .
728
I am in function 1 .
729
I am in function 2 .
730
I am in function 1 .
731
I am in function 2 .
732
I am in function 1 .
733
I am in function 2 .
734
I am in function 1 .
735
I am in function 2 .
736
I am in function 1 .
737
I am in function 2 .
738
I am in function 1 .
739
I am in function 2 .
740
I am in function 1 .
741
[Link] 180/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
742
I am in function 1 .
743
I am in function 2 .
744
I am in function 1 .
745
I am in function 2 .
746
I am in function 1 .
747
I am in function 2 .
748
I am in function 1 .
749
I am in function 2 .
750
I am in function 1 .
751
I am in function 2 .
752
I am in function 1 .
753
I am in function 2 .
754
I am in function 1 .
755
I am in function 2 .
756
I am in function 1 .
757
I am in function 2 .
758
I am in function 1 .
759
I am in function 2 .
760
I am in function 1 .
761
I am in function 2 .
762
I am in function 1 .
763
I am in function 2 .
764
I am in function 1 .
765
I am in function 2 .
766
I am in function 1 .
767
I am in function 2 .
768
I am in function 1 .
769
I am in function 2 .
[Link] 181/338
13/11/2016 python Basic and Intermediate Level Modules
770
I am in function 1 .
771
I am in function 2 .
772
I am in function 1 .
773
I am in function 2 .
774
I am in function 1 .
775
I am in function 2 .
776
I am in function 1 .
777
I am in function 2 .
778
I am in function 1 .
779
I am in function 2 .
780
I am in function 1 .
781
I am in function 2 .
782
I am in function 1 .
783
I am in function 2 .
784
I am in function 1 .
785
I am in function 2 .
786
I am in function 1 .
787
I am in function 2 .
788
I am in function 1 .
789
I am in function 2 .
790
I am in function 1 .
791
I am in function 2 .
792
I am in function 1 .
793
I am in function 2 .
794
I am in function 1 .
795
I am in function 2 .
796
I am in function 1 .
797
I am in function 2 .
798
[Link] 182/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
799
I am in function 2 .
800
I am in function 1 .
801
I am in function 2 .
802
I am in function 1 .
803
I am in function 2 .
804
I am in function 1 .
805
I am in function 2 .
806
I am in function 1 .
807
I am in function 2 .
808
I am in function 1 .
809
I am in function 2 .
810
I am in function 1 .
811
I am in function 2 .
812
I am in function 1 .
813
I am in function 2 .
814
I am in function 1 .
815
I am in function 2 .
816
I am in function 1 .
817
I am in function 2 .
818
I am in function 1 .
819
I am in function 2 .
820
I am in function 1 .
821
I am in function 2 .
822
I am in function 1 .
823
I am in function 2 .
824
I am in function 1 .
825
I am in function 2 .
826
I am in function 1 .
[Link] 183/338
13/11/2016 python Basic and Intermediate Level Modules
827
I am in function 2 .
828
I am in function 1 .
829
I am in function 2 .
830
I am in function 1 .
831
I am in function 2 .
832
I am in function 1 .
833
I am in function 2 .
834
I am in function 1 .
835
I am in function 2 .
836
I am in function 1 .
837
I am in function 2 .
838
I am in function 1 .
839
I am in function 2 .
840
I am in function 1 .
841
I am in function 2 .
842
I am in function 1 .
843
I am in function 2 .
844
I am in function 1 .
845
I am in function 2 .
846
I am in function 1 .
847
I am in function 2 .
848
I am in function 1 .
849
I am in function 2 .
850
I am in function 1 .
851
I am in function 2 .
852
I am in function 1 .
853
I am in function 2 .
854
I am in function 1 .
855
[Link] 184/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 2 .
856
I am in function 1 .
857
I am in function 2 .
858
I am in function 1 .
859
I am in function 2 .
860
I am in function 1 .
861
I am in function 2 .
862
I am in function 1 .
863
I am in function 2 .
864
I am in function 1 .
865
I am in function 2 .
866
I am in function 1 .
867
I am in function 2 .
868
I am in function 1 .
869
I am in function 2 .
870
I am in function 1 .
871
I am in function 2 .
872
I am in function 1 .
873
I am in function 2 .
874
I am in function 1 .
875
I am in function 2 .
876
I am in function 1 .
877
I am in function 2 .
878
I am in function 1 .
879
I am in function 2 .
880
I am in function 1 .
881
I am in function 2 .
882
I am in function 1 .
883
I am in function 2 .
[Link] 185/338
13/11/2016 python Basic and Intermediate Level Modules
884
I am in function 1 .
885
I am in function 2 .
886
I am in function 1 .
887
I am in function 2 .
888
I am in function 1 .
889
I am in function 2 .
890
I am in function 1 .
891
I am in function 2 .
892
I am in function 1 .
893
I am in function 2 .
894
I am in function 1 .
895
I am in function 2 .
896
I am in function 1 .
897
I am in function 2 .
898
I am in function 1 .
899
I am in function 2 .
900
I am in function 1 .
901
I am in function 2 .
902
I am in function 1 .
903
I am in function 2 .
904
I am in function 1 .
905
I am in function 2 .
906
I am in function 1 .
907
I am in function 2 .
908
I am in function 1 .
909
I am in function 2 .
910
I am in function 1 .
911
I am in function 2 .
912
[Link] 186/338
13/11/2016 python Basic and Intermediate Level Modules
I am in function 1 .
913
I am in function 2 .
914
I am in function 1 .
915
I am in function 2 .
916
I am in function 1 .
917
I am in function 2 .
918
I am in function 1 .
919
I am in function 2 .
920
I am in function 1 .
921
I am in function 2 .
922
I am in function 1 .
923
I am in function 2 .
924
I am in function 1 .
925
I am in function 2 .
926
I am in function 1 .
927
I am in function 2 .
928
I am in function 1 .
929
I am in function 2 .
930
I am in function 1 .
931
I am in function 2 .
932
I am in function 1 .
933
I am in function 2 .
934
I am in function 1 .
935
I am in function 2 .
936
I am in function 1 .
937
I am in function 2 .
938
I am in function 1 .
939
I am in function 2 .
940
I am in function 1 .
[Link] 187/338
13/11/2016 python Basic and Intermediate Level Modules
941
I am in function 2 .
942
I am in function 1 .
943
I am in function 2 .
944
I am in function 1 .
945
I am in function 2 .
946
I am in function 1 .
947
I am in function 2 .
948
I am in function 1 .
949
I am in function 2 .
950
I am in function 1 .
951
I am in function 2 .
952
I am in function 1 .
953
I am in function 2 .
954
I am in function 1 .
955
I am in function 2 .
956
I am in function 1 .
957
I am in function 2 .
958
I am in function 1 .
959
I am in function 2 .
960
I am in function 1 .
961
I am in function 2 .
962
I am in function 1 .
963
I am in function 2 .
964
I am in function 1 .
965
I am in function 2 .
966
I am in function 1 .
967
I am in function 2 .
968
I am in function 1 .
969
[Link] 188/338
13/11/2016 python Basic and Intermediate Level Modules
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
RuntimeError Traceback (most recent call last)
<ipython‐input‐47‐043607b7b3c9> in <module>()
‐‐‐‐> 1 func1()
<ipython‐input‐46‐fec7b5a3b174> in func1()
7 noOfloops+=1
8 print noOfloops
‐‐‐‐> 9 return func2()
10
11 def func2():
<ipython‐input‐46‐fec7b5a3b174> in func2()
14 noOfloops+=1
15 print noOfloops
‐‐‐> 16 return func1()
... last 2 frames repeated, from the frame below ...
<ipython‐input‐46‐fec7b5a3b174> in func1()
7 noOfloops+=1
8 print noOfloops
‐‐‐‐> 9 return func2()
10
11 def func2():
RuntimeError: maximum recursion depth exceeded while calling a Python object
NOTE :
Python doesnot have Tail Call optimization(TCO), to handle the recursive functions.
It is very difficult to add TCO to python, as it is a dynamic language.
[Link] 189/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Execute the below Script in a new .py file, and observe the result
#!/usr/bin/python
# [Link]
# Purpose: To list the directories and files, in the given location
import os
def get_dirlist(path):
"""
Return a sorted list of all entries in path.
This returns just the names, not the full path to the names.
"""
dirlist = [Link](path)
[Link]()
return dirlist
def print_files(path, prefix = ""):
""" Print recursive listing of contents of path """
if prefix == "": # Detect outermost call, print a heading
print("Folder listing for", path)
prefix = "| "
dirlist = get_dirlist(path)
for f in dirlist:
print(prefix+f) # Print the line
fullname = [Link](path, f) # Turn name into full pathname
if [Link](fullname): # If a directory, recurse.
print_files(fullname, prefix + "| ")
import sys
if [Link] == 'win32':
d = raw_input('Enter a dircetory path: ')
print_files(repr(d)) # r'C:\Python27\Tools'
else:
d = raw_input('Enter a dircetory path: ')
print_files(repr(d))
In conclusion, note that recursive calls are expensive (inefficient) as they take up a lot of memory and time.
Recursive functions are hard to debug.
Recommended References:
1. Animated explanation of recursions ([Link]
[Link])
[Link] 190/338
13/11/2016 python Basic and Intermediate Level Modules
In [65]: #!/usr/bin/python
# Switch case equivalent implementation in python
# [Link]
case = {'a':'apple', 'b': 'banana', 'c': 'cat'}
caseChoice = raw_input('Enter the case choice(a, b or c only):')
if caseChoice in case:
if caseChoice == 'a':
print [Link]('a', None)
elif caseChoice == 'b':
print [Link]('b', None)
else:
print [Link]('c', None)
else:
print "Please enter a valid case choice: a, b or c"
Enter the case choice(a, b or c only):a
apple
Assignment : In [Link], try to add a choice for reattempt, to the user.
6.0 Modules
Both buitin (ex: os), installed (ex: django) or userdefined
It is a collection of functions, to serve a particular purpose
imported in the functions, using 'import'
Not all modules will be part of basic distribution
To install a new module, pip install moduleName
To search a module, pip search moduleName
In [66]: import sys
In [67]: print dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__',
'__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_fram
es', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_name
s', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dl
lhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'except
hook', 'exec_prefix', 'executable', 'exit', 'exitfunc', 'flags', 'float_inf
o', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesys
temencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof',
'gettrace', 'getwindowsversion', 'hexversion', 'last_traceback', 'last_typ
e', 'last_value', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_pat
h', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'pref
ix', 'ps1', 'ps2', 'ps3', 'py3kwarning', 'setcheckinterval', 'setprofile', 's
etrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 've
rsion', 'version_info', 'warnoptions', 'winver']
[Link] 191/338
13/11/2016 python Basic and Intermediate Level Modules
In [68]: [Link]
Out[68]: '2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v.1500 32 bit (Int
el)]'
In [69]: sys.version_info
Out[69]: sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
In [70]: [Link]
Out[70]: '2.7'
In [71]: [Link]
Out[71]: ['',
'C:\\Windows\\system32\\[Link]',
'c:\\python27\\DLLs',
'c:\\python27\\lib',
'c:\\python27\\lib\\plat‐win',
'c:\\python27\\lib\\lib‐tk',
'c:\\python27',
'c:\\python27\\lib\\site‐packages',
'c:\\python27\\lib\\site‐packages\\IPython\\extensions',
'C:\\Users\\HOME\\.ipython']
Assignment : Display the [Link] from interpreter and a script file separately and notice the difference in the
outputs
In [72]: [Link]
Out[72]: 'win32'
In [73]: [Link]
Out[73]: <function [Link]>
In [74]: callable([Link]) # callable() builtin function to get whether
a particular object is callable or not
Out[74]: True
In [75]: [Link]()
Out[75]: [Link](major=6, minor=1, build=7600, platform=2, service_pack
='')
In [76]: callable([Link])
Out[76]: False
[Link] 192/338
13/11/2016 python Basic and Intermediate Level Modules
In [77]: [Link]
Out[77]: 2147483647
In [78]: [Link]
Out[78]: 2147483647
NOTE: Userdefined modules are prioritized to builtin(or installed) modules
[Link] 193/338
13/11/2016 python Basic and Intermediate Level Modules
In [79]: help(sys) # help([Link])
[Link] 194/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 195/338
13/11/2016 python Basic and Intermediate Level Modules
Help on built‐in module sys:
NAME
sys
FILE
(built‐in)
MODULE DOCS
[Link]
DESCRIPTION
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
Dynamic objects:
argv ‐‐ command line arguments; argv[0] is the script pathname if known
path ‐‐ module search path; path[0] is the script directory, else ''
modules ‐‐ dictionary of loaded modules
displayhook ‐‐ called to show results in an interactive session
excepthook ‐‐ called to handle any uncaught exception other than SystemEx
it
To customize printing in an interactive session or to install a custom
top‐level exception handler, assign other functions to replace these.
exitfunc ‐‐ if [Link] exists, this routine is called when Python ex
its
Assigning to [Link] is deprecated; use the atexit module instead.
stdin ‐‐ standard input file object; used by raw_input() and input()
stdout ‐‐ standard output file object; used by the print statement
stderr ‐‐ standard error object; used for error messages
By assigning other file objects (or objects that behave like files)
to these, it is possible to redirect all of the interpreter's I/O.
last_type ‐‐ type of last uncaught exception
last_value ‐‐ value of last uncaught exception
last_traceback ‐‐ traceback of last uncaught exception
These three are only available in an interactive session after a
traceback has been printed.
exc_type ‐‐ type of exception currently being handled
exc_value ‐‐ value of exception currently being handled
exc_traceback ‐‐ traceback of exception currently being handled
The function exc_info() should be used instead of these three,
because it is thread‐safe.
Static objects:
float_info ‐‐ a dict with information about the float inplementation.
long_info ‐‐ a struct sequence with information about the long implementa
tion.
maxint ‐‐ the largest supported integer (the smallest is ‐maxint‐1)
maxsize ‐‐ the largest supported length of containers.
maxunicode ‐‐ the largest supported character
[Link] 196/338
13/11/2016 python Basic and Intermediate Level Modules
builtin_module_names ‐‐ tuple of module names built into this interpreter
version ‐‐ the version of this interpreter as a string
version_info ‐‐ version information as a named tuple
hexversion ‐‐ version information encoded as a single integer
copyright ‐‐ copyright notice pertaining to this interpreter
platform ‐‐ platform identifier
executable ‐‐ absolute path of the executable binary of the Python interp
reter
prefix ‐‐ prefix used to find the Python library
exec_prefix ‐‐ prefix used to find the machine‐specific Python library
float_repr_style ‐‐ string indicating the style of repr() output for floa
ts
dllhandle ‐‐ [Windows only] integer handle of the Python DLL
winver ‐‐ [Windows only] version number of the Python DLL
__stdin__ ‐‐ the original stdin; don't touch!
__stdout__ ‐‐ the original stdout; don't touch!
__stderr__ ‐‐ the original stderr; don't touch!
__displayhook__ ‐‐ the original displayhook; don't touch!
__excepthook__ ‐‐ the original excepthook; don't touch!
Functions:
displayhook() ‐‐ print an object to the screen, and save it in __builtin_
_._
excepthook() ‐‐ print an exception and its traceback to [Link]
exc_info() ‐‐ return thread‐safe information about the current exception
exc_clear() ‐‐ clear the exception state for the current thread
exit() ‐‐ exit the interpreter by raising SystemExit
getdlopenflags() ‐‐ returns flags to be used for dlopen() calls
getprofile() ‐‐ get the global profiling function
getrefcount() ‐‐ return the reference count for an object (plus one :‐)
getrecursionlimit() ‐‐ return the max recursion depth for the interpreter
getsizeof() ‐‐ return the size of an object in bytes
gettrace() ‐‐ get the global debug tracing function
setcheckinterval() ‐‐ control how often the interpreter checks for events
setdlopenflags() ‐‐ set the flags to be used for dlopen() calls
setprofile() ‐‐ set the global profiling function
setrecursionlimit() ‐‐ set the max recursion depth for the interpreter
settrace() ‐‐ set the global debug tracing function
FUNCTIONS
__displayhook__ = displayhook(...)
displayhook(object) ‐> None
Print an object to [Link] and also save it in __builtin__._
__excepthook__ = excepthook(...)
excepthook(exctype, value, traceback) ‐> None
Handle an exception by displaying it with a traceback on [Link].
call_tracing(...)
call_tracing(func, args) ‐> object
Call func(*args), while tracing is enabled. The tracing state is
saved, and restored afterwards. This is intended to be called from
a debugger from a checkpoint, to recursively debug some other code.
[Link] 197/338
13/11/2016 python Basic and Intermediate Level Modules
callstats(...)
callstats() ‐> tuple of integers
Return a tuple of function call statistics, if CALL_PROFILE was defin
ed
when Python was built. Otherwise, return None.
When enabled, this function returns detailed, implementation‐specific
details about the number of function calls executed. The return value
is
a 11‐tuple where the entries in the tuple are counts of:
0. all function calls
1. calls to PyFunction_Type objects
2. PyFunction calls that do not create an argument tuple
3. PyFunction calls that do not create an argument tuple
and bypass PyEval_EvalCodeEx()
4. PyMethod calls
5. PyMethod calls on bound methods
6. PyType calls
7. PyCFunction calls
8. generator calls
9. All other calls
10. Number of stack pops performed by call_function()
exc_clear(...)
exc_clear() ‐> None
Clear global information on the current exception. Subsequent calls
to
exc_info() will return (None,None,None) until another exception is ra
ised
in the current thread or the execution stack returns to a frame where
another exception is being handled.
exc_info(...)
exc_info() ‐> (type, value, traceback)
Return information about the most recent exception caught by an excep
t
clause in the current stack frame or in an older stack frame.
exit(...)
exit([status])
Exit the interpreter by raising SystemExit(status).
If the status is omitted or None, it defaults to zero (i.e., succes
s).
If the status is an integer, it will be used as the system exit statu
s.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
getcheckinterval(...)
getcheckinterval() ‐> current check interval; see setcheckinterval().
getdefaultencoding(...)
[Link] 198/338
13/11/2016 python Basic and Intermediate Level Modules
getdefaultencoding() ‐> string
Return the current default string encoding used by the Unicode
implementation.
getfilesystemencoding(...)
getfilesystemencoding() ‐> string
Return the encoding used to convert Unicode filenames in
operating system filenames.
getprofile(...)
getprofile()
Return the profiling function set with [Link].
See the profiler chapter in the library manual.
getrecursionlimit(...)
getrecursionlimit()
Return the current value of the recursion limit, the maximum depth
of the Python interpreter stack. This limit prevents infinite
recursion from causing an overflow of the C stack and crashing Pytho
n.
getrefcount(...)
getrefcount(object) ‐> integer
Return the reference count of object. The count returned is generall
y
one higher than you might expect, because it includes the (temporary)
reference as an argument to getrefcount().
getsizeof(...)
getsizeof(object, default) ‐> int
Return the size of object in bytes.
gettrace(...)
gettrace()
Return the global debug tracing function set with [Link].
See the debugger chapter in the library manual.
getwindowsversion(...)
getwindowsversion()
Return information about the running version of Windows as a named tu
ple.
The members are named: major, minor, build, platform, service_pack,
service_pack_major, service_pack_minor, suite_mask, and product_type.
For
backward compatibility, only the first 5 items are available by index
ing.
All elements are numbers, except service_pack which is a string. Plat
form
may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vi
[Link] 199/338
13/11/2016 python Basic and Intermediate Level Modules
sta/7,
3 for Windows CE. Product_type may be 1 for a workstation, 2 for a do
main
controller, 3 for a server.
setcheckinterval(...)
setcheckinterval(n)
Tell the Python interpreter to check for asynchronous events every
n instructions. This also affects how often thread switches occur.
setprofile(...)
setprofile(function)
Set the profiling function. It will be called on each function call
and return. See the profiler chapter in the library manual.
setrecursionlimit(...)
setrecursionlimit(n)
Set the maximum depth of the Python interpreter stack to n. This
limit prevents infinite recursion from causing an overflow of the C
stack and crashing Python. The highest possible limit is platform‐
dependent.
settrace(...)
settrace(function)
Set the global debug tracing function. It will be called on each
function call. See the debugger chapter in the library manual.
DATA
__stderr__ = <open file '<stderr>', mode 'w'>
__stdin__ = <open file '<stdin>', mode 'r'>
__stdout__ = <open file '<stdout>', mode 'w'>
api_version = 1013
argv = [r'c:\python27\lib\site‐packages\ipykernel\__main__.py', '‐f', ...
builtin_module_names = ('__builtin__', '__main__', '_ast', '_bisect', ...
byteorder = 'little'
copyright = 'Copyright (c) 2001‐2016 Python Software Foundati...ematis...
displayhook = <[Link] object>
dllhandle = 1608122368
dont_write_bytecode = False
exc_value = TypeError("<module 'sys' (built‐in)> is a built‐in module"...
exec_prefix = r'c:\python27'
executable = r'c:\python27\[Link]'
flags = [Link](debug=0, py3k_warning=0, division_warn...unicode=0, ...
float_info = sys.float_info(max=1.7976931348623157e+308, max_...epsilo...
float_repr_style = 'short'
hexversion = 34016496
last_value = KeyError('popitem(): dictionary is empty',)
long_info = sys.long_info(bits_per_digit=15, sizeof_digit=2)
maxint = 2147483647
maxsize = 2147483647
maxunicode = 65535
meta_path = [<six._SixMetaPathImporter object>, <pkg_resources.extern....
modules = {'IPython': <module 'IPython' from 'c:\python27\lib\site‐pac...
[Link] 200/338
13/11/2016 python Basic and Intermediate Level Modules
path = ['', r'C:\Windows\system32\[Link]', r'c:\python27\DLLs', ...
path_hooks = [<type '[Link]'>]
path_importer_cache = {'': None, r'C:\Users\HOME\.ipython': None, r'C:...
platform = 'win32'
prefix = r'c:\python27'
ps1 = 'In : '
ps2 = '...: '
ps3 = 'Out: '
py3kwarning = False
stderr = <[Link] object>
stdin = <open file '<stdin>', mode 'r'>
stdout = <[Link] object>
subversion = ('CPython', '', '')
version = '2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v...
version_info = sys.version_info(major=2, minor=7, micro=12, releaselev...
warnoptions = []
winver = '2.7'
Methods of importing
import sys
from sys import * # Not recommended by PEP 8
from sys import version
from sys import version as vr # alias importing
In [80]: import sys
[Link]
Out[80]: '2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v.1500 32 bit (Int
el)]'
In [81]: del sys # 'sys' object will be removed from the heap memory
In [82]: sys
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐82‐d7099a5e9c44> in <module>()
‐‐‐‐> 1 sys
NameError: name 'sys' is not defined
In [83]: from sys import version
print "version = ", version
version = 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v.1500 3
2 bit (Intel)]
[Link] 201/338
13/11/2016 python Basic and Intermediate Level Modules
In [84]: from sys import version as vr
print "The Version is ", vr
The Version is 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, [Link]) [MSC v.1
500 32 bit (Intel)]
In [85]: del vr # removes 'vr' object from the heap memory
In [86]: vr
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐86‐8264846d88d6> in <module>()
‐‐‐‐> 1 vr
NameError: name 'vr' is not defined
Note: Selective imports optimizes the memory usage; but care should be taken when userdefined identifiers
in the script/project have the same names as these functions of modules
In [87]: import os
In [88]: [Link]("echo 'Hello World!'") # 0 means the statement executed properly.
Output will be displayed in console; not here
Out[88]: 0
In [89]: combinedDir = [Link]('first', 'second', 'third', 'fourth')
print 'combinedDir is ', combinedDir
combinedDir is first\second\third\fourth
In [90]: print [Link](combinedDir)
False
In [91]: [Link]()
Out[91]: 'C:\\Users\\HOME\\Google Drive\\python\\tut\\complete Material'
In [92]: [Link]('newFolder')
In [93]: [Link]([Link]())
Out[93]: ['.ipynb_checkpoints',
'[Link]',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [94]: [Link](combinedDir) # Common gotcha: It is 'makedirs' and not 'mkdirs'
[Link] 202/338
13/11/2016 python Basic and Intermediate Level Modules
In [95]: [Link]([Link]())
Out[95]: ['.ipynb_checkpoints',
'[Link]',
'first',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [96]: with open('[Link]', 'ab+') as myTst:
[Link]("Will power can do a lot of things!")
[Link]()
# working with files will be done in next chapter.
In [97]: [Link]([Link]())
Out[97]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [98]: [Link]('[Link]', '[Link]') # renaming files
In [99]: [Link]([Link]())
Out[99]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [100]: [Link]('newFolder', 'myNewFolder') # renaming directories
In [101]: [Link]([Link]())
Out[101]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [102]: [Link]('first/')
[Link] 203/338
13/11/2016 python Basic and Intermediate Level Modules
In [103]: [Link]([Link]())
Out[103]: ['[Link]', 'second']
In [104]: [Link]('..') # changing to previous directories
In [105]: [Link]()
Out[105]: 'C:\\Users\\HOME\\Google Drive\\python\\tut\\complete Material'
In [106]: [Link]([Link]())
Out[106]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [107]: [Link]('[Link]') # for more details, goto help([Link]('[Link]
f'))
Out[107]: nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_
gid=0, st_size=34L, st_atime=1478694576L, st_mtime=1478694576L, st_ctime=1478
694576L)
In [108]: modifiedTime = [Link]('[Link]').st_mtime
print "[Link] was last modified on ", modifiedTime # epoch time
[Link] was last modified on 1478694576.73
In [109]: from datetime import datetime
print [Link](modifiedTime)
2016‐11‐09 [Link].726220
In [110]: print "[Link] was last modified on ", [Link](modifiedTi
me)
[Link] was last modified on 2016‐11‐09 [Link].726220
[Link] 204/338
13/11/2016 python Basic and Intermediate Level Modules
In [112]: #!/usr/bin/python
import sys
import os
if [Link] == 'win32':
dirToCheck = raw_input("Enter a directory path to check ") #'C:\Python27\T
ools'
else:
dirToCheck = raw_input("Enter a directory path to check ")
for dirpath, dirnames, filenames in [Link](dirToCheck):
print 'Current Path:', dirpath
print 'Directories:', dirnames
print 'Files:', filenames
print '‐'*50
[Link] 205/338
13/11/2016 python Basic and Intermediate Level Modules
Enter a directory path to check C:\Python27\Tools
Current Path: C:\Python27\Tools
Directories: ['i18n', 'pynche', 'Scripts', 'versioncheck', 'webchecker']
Files: []
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\i18n
Directories: []
Files: ['[Link]', '[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\pynche
Directories: ['X']
Files: ['[Link]', '[Link]', '[Link]', '[Link]
t', '[Link]', '[Link]', '[Link]', '[Link]', 'pync
[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', '__init_
_.py']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\pynche\X
Directories: []
Files: ['[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\Scripts
Directories: []
Files: ['[Link]', 'analyze_dxp.py', '[Link]', '[Link]', 'checkappen
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'combin
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'diff.p
y', '[Link]', '[Link]', '[Link]', '[Link]', 'findnocoding.p
y', 'find_recursionlimit.py', '[Link]', '[Link]', '[Link]', 'fixn
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'hotshotmain.
py', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'mailerda
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'o
[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'RE
[Link]', '[Link]', 'reindent‐[Link]', '[Link]', '[Link]', 'serve.
py', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'tree
[Link]', '[Link]', '[Link]', 'win_add2path.py', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\versioncheck
Directories: []
Files: ['[Link]', '[Link]', '[Link]', '_checkversio
[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\webchecker
Directories: []
Files: ['[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
6.0 Modules
In [113]: import os
[Link] 206/338
13/11/2016 python Basic and Intermediate Level Modules
In [114]: [Link]()
Out[114]: 'C:\\Users\\HOME\\Google Drive\\python\\tut\\complete Material'
In [115]: for dirpath, dirnames, filenames in [Link]('C:\Python27\Tools'):
print 'Current Path:', dirpath
print 'Directories:', dirnames
print 'Files:', filenames
print '‐'*50
Current Path: C:\Python27\Tools
Directories: ['i18n', 'pynche', 'Scripts', 'versioncheck', 'webchecker']
Files: []
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\i18n
Directories: []
Files: ['[Link]', '[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\pynche
Directories: ['X']
Files: ['[Link]', '[Link]', '[Link]', '[Link]
t', '[Link]', '[Link]', '[Link]', '[Link]', 'pync
[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', '__init_
_.py']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\pynche\X
Directories: []
Files: ['[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\Scripts
Directories: []
Files: ['[Link]', 'analyze_dxp.py', '[Link]', '[Link]', 'checkappen
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'combin
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'diff.p
y', '[Link]', '[Link]', '[Link]', '[Link]', 'findnocoding.p
y', 'find_recursionlimit.py', '[Link]', '[Link]', '[Link]', 'fixn
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'hotshotmain.
py', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'mailerda
[Link]', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'o
[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'RE
[Link]', '[Link]', 'reindent‐[Link]', '[Link]', '[Link]', 'serve.
py', '[Link]', '[Link]', '[Link]', '[Link]', '[Link]', 'tree
[Link]', '[Link]', '[Link]', 'win_add2path.py', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\versioncheck
Directories: []
Files: ['[Link]', '[Link]', '[Link]', '_checkversio
[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Current Path: C:\Python27\Tools\webchecker
Directories: []
Files: ['[Link]', '[Link]', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]']
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
[Link] 207/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : exceute this, and observe the output
`
for i in [Link]: # To get the environmental variables
print i
[Link] 208/338
13/11/2016 python Basic and Intermediate Level Modules
In [116]: [en for en in [Link]]
Out[116]: ['TMP',
'CM2013DIR',
'USERDOMAIN',
'COMPUTERNAME',
'PSMODULEPATH',
'COMMONPROGRAMFILES',
'PROCESSOR_IDENTIFIER',
'PROGRAMFILES',
'PROCESSOR_REVISION',
'SYSTEMROOT',
'PATH',
'UGII_BASE_DIR',
'PROGRAMFILES(X86)',
'TERM',
'TEMP',
'COMMONPROGRAMFILES(X86)',
'PROCESSOR_ARCHITECTURE',
'ALLUSERSPROFILE',
'LOCALAPPDATA',
'HOMEPATH',
'JPY_INTERRUPT_EVENT',
'PROGRAMW6432',
'USERNAME',
'UGII_ROOT_DIR',
'LOGONSERVER',
'PROMPT',
'COMSPEC',
'JPY_PARENT_PID',
'PROGRAMDATA',
'UGS_LICENSE_SERVER',
'MPLBACKEND',
'GIT_PAGER',
'SESSIONNAME',
'PATHEXT',
'FP_NO_HOST_CHECK',
'WINDIR',
'MOZ_PLUGIN_PATH',
'HOMEDRIVE',
'PAGER',
'SYSTEMDRIVE',
'CLICOLOR',
'NUMBER_OF_PROCESSORS',
'UGII_LANG',
'APPDATA',
'PROCESSOR_LEVEL',
'PROCESSOR_ARCHITEW6432',
'ILBDIR',
'COMMONPROGRAMW6432',
'OS',
'PUBLIC',
'IPY_INTERRUPT_EVENT',
'USERPROFILE']
[Link] 209/338
13/11/2016 python Basic and Intermediate Level Modules
In [118]: print "[Link]('TMP') is ", [Link]('TMP')
[Link]('TMP') is C:\Users\HOME\AppData\Local\Temp
In [119]: print "[Link]('TMP') is ", [Link]('USERPROFILE')
[Link]('TMP') is C:\Users\HOME
In [120]: print "[Link]('TMP') is ", [Link]('USERNAME')
[Link]('TMP') is HOME
In [121]: print "[Link]('TEMP') is ", [Link]('TEMP')
[Link]('TEMP') is C:\Users\HOME\AppData\Local\Temp
In [122]: enVar = [Link]
print type(enVar)
<type 'instance'>
In [123]: filePath = [Link]([Link]('TMP'), '[Link]')
print filePath
C:\Users\HOME\AppData\Local\Temp\[Link]
In [124]: print [Link](filePath)
False
In [125]: with open(filePath, 'ab+') as f:
[Link]("This is the right time to invest my energies, to get success")
[Link]()
In [126]: print [Link](filePath)
True
In [127]: [Link](filePath)
Out[127]: '[Link]'
In [128]: [Link](filePath)
Out[128]: 'C:\\Users\\HOME\\AppData\\Local\\Temp'
In [129]: [Link](filePath) + [Link](filePath)
Out[129]: 'C:\\Users\\HOME\\AppData\\Local\\[Link]'
In [130]: filePath
Out[130]: 'C:\\Users\\HOME\\AppData\\Local\\Temp\\[Link]'
[Link] 210/338
13/11/2016 python Basic and Intermediate Level Modules
In [131]: [Link](filePath)
Out[131]: ('C:\\Users\\HOME\\AppData\\Local\\Temp\\test', '.txt')
In [132]: [Link]('[Link]')
Out[132]: ('someRandomfile', '.pdf')
In [133]: [Link]([Link]())
Out[133]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [134]: [Link]('.')
Out[134]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [135]: [Link]('newFolder')
In [136]: [Link]('.')
Out[136]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [137]: [Link]('newFolder/')
[Link]('..')
Out[137]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
[Link] 211/338
13/11/2016 python Basic and Intermediate Level Modules
In [138]: [Link]()
Out[138]: 'C:\\Users\\HOME\\Google Drive\\python\\tut\\complete Material\\newFolder'
time related modules time, datetime, pytz, ...
In [140]: import time
In [141]: print dir(time)
['__doc__', '__name__', '__package__', 'accept2dyear', 'altzone', 'asctime',
'clock', 'ctime', 'daylight', 'gmtime', 'localtime', 'mktime', 'sleep', 'str
ftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']
[Link] 212/338
13/11/2016 python Basic and Intermediate Level Modules
In [142]: print time.__doc__
This module provides various functions to manipulate time values.
There are two standard representations of time. One is the number
of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer
or a floating point number (to represent fractions of seconds).
The Epoch is system‐defined; on Unix, it is generally January 1st, 1970.
The actual value can be retrieved by calling gmtime(0).
The other representation is a tuple of 9 integers giving local time.
The tuple items are:
year (four digits, e.g. 1998)
month (1‐12)
day (1‐31)
hours (0‐23)
minutes (0‐59)
seconds (0‐59)
weekday (0‐6, Monday is 0)
Julian day (day in the year, 1‐366)
DST (Daylight Savings Time) flag (‐1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is ‐1, mktime() should guess based on the date and time.
Variables:
timezone ‐‐ difference in seconds between UTC and local standard time
altzone ‐‐ difference in seconds between UTC and local DST time
daylight ‐‐ whether local time should reflect DST
tzname ‐‐ tuple of (standard time zone name, DST time zone name)
Functions:
time() ‐‐ return current time in seconds since the Epoch as a float
clock() ‐‐ return CPU time since process start as a float
sleep() ‐‐ delay for a number of seconds given as a float
gmtime() ‐‐ convert seconds since Epoch to UTC tuple
localtime() ‐‐ convert seconds since Epoch to local time tuple
asctime() ‐‐ convert time tuple to string
ctime() ‐‐ convert time in seconds to string
mktime() ‐‐ convert local time tuple to seconds since Epoch
strftime() ‐‐ convert time tuple to string according to format specification
strptime() ‐‐ parse string to time tuple according to format specification
tzset() ‐‐ change the local timezone
In [143]: [Link]
Out[143]: ('India Standard Time', 'India Standard Time')
In [144]: [Link][0]
Out[144]: 'India Standard Time'
[Link] 213/338
13/11/2016 python Basic and Intermediate Level Modules
In [145]: [Link] # Results in boolean result of existence or absence of DST, in t
hat time zone
Out[145]: 0
In [146]: [Link]
Out[146]: ‐19800
In [147]: [Link]() #seconds past from epoch time, till now
Out[147]: 1478695362.258
In [148]: [Link]()
Out[148]: 'Wed Nov 09 [Link] 2016'
In [149]: [Link]()
Out[149]: 'Wed Nov 09 [Link] 2016'
In [150]: type([Link]())
Out[150]: str
In [151]: [Link]()
Out[151]: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=9, tm_hour=12, tm_min=43, t
m_sec=12, tm_wday=2, tm_yday=314, tm_isdst=0)
In [152]: type([Link]())
Out[152]: time.struct_time
In [153]: [Link]()
Out[153]: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=9, tm_hour=18, tm_min=13, t
m_sec=24, tm_wday=2, tm_yday=314, tm_isdst=0)
In [154]: t = [Link]()
print type(t)
<type 'time.struct_time'>
In [155]: [Link]()
Out[155]: 1.5395415860973236e‐06
Assignment : what is the difference between [Link]() and [Link]()
In [156]: [Link](6) # To let the interpreter to sleep for 6 seconds
[Link] 214/338
13/11/2016 python Basic and Intermediate Level Modules
In [157]: [Link]('Fri Aug 19 [Link] 2016') # String to time tuple conversion
Out[157]: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=19, tm_hour=7, tm_min=33, tm
_sec=1, tm_wday=4, tm_yday=232, tm_isdst=‐1)
In [158]: [Link]([Link]())
Out[158]: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=9, tm_hour=18, tm_min=14, t
m_sec=27, tm_wday=2, tm_yday=314, tm_isdst=‐1)
In [159]: [Link]([Link]())
Out[159]: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=9, tm_hour=18, tm_min=14, t
m_sec=34, tm_wday=2, tm_yday=314, tm_isdst=‐1)
Assignment : what is the difference between [Link]() and [Link]()?
In [160]: [Link]("8/4/1988", "%d/%m/%Y")
Out[160]: time.struct_time(tm_year=1988, tm_mon=4, tm_mday=8, tm_hour=0, tm_min=0, tm_s
ec=0, tm_wday=4, tm_yday=99, tm_isdst=‐1)
In [161]: [Link]("08 Apr 1988", "%d %b %Y")
Out[161]: time.struct_time(tm_year=1988, tm_mon=4, tm_mday=8, tm_hour=0, tm_min=0, tm_s
ec=0, tm_wday=4, tm_yday=99, tm_isdst=‐1)
In [162]: [Link]("15‐Aug‐1947", "%d‐%b‐%Y")
Out[162]: time.struct_time(tm_year=1947, tm_mon=8, tm_mday=15, tm_hour=0, tm_min=0, tm_
sec=0, tm_wday=4, tm_yday=227, tm_isdst=‐1)
In [163]: myTime = [Link]()
print myTime
newCreatedTime = [Link]([Link](myTime))
print newCreatedTime
Wed Nov 09 [Link] 2016
1478695520.0
Interesting Article: [Link]
([Link]
In [164]: import datetime
In [165]: print datetime.__doc__
Fast implementation of the datetime type.
[Link] 215/338
13/11/2016 python Basic and Intermediate Level Modules
In [166]: print dir(datetime)
['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date', 'datetim
e', 'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
In [167]: print [Link].__doc__
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinf
o]]]]])
The year, month and day arguments are required. tzinfo may be None, or an
instance of a tzinfo subclass. The remaining arguments may be ints or longs.
In [168]: print dir([Link])
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '_
_ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt
__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr_
_', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclas
shook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fromordina
l', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max',
'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'se
cond', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'today', 'toord
inal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctim
etuple', 'weekday', 'year']
In [169]: [Link]() # local time
Out[169]: [Link](2016, 11, 9, 18, 16, 18, 343000)
In [170]: type([Link]())
Out[170]: [Link]
In [171]: [Link]()
Out[171]: [Link](2016, 11, 9, 12, 46, 30, 850000)
In [172]: [Link]()
Out[172]: [Link](2016, 11, 9)
In [173]: tdy = [Link]()
print tdy
2016‐11‐09
In [174]: print [Link]("four‐digit year: %Y, two‐digit year: %y, month: %m, day: %
d, seconds: %S")
four‐digit year: 2016, two‐digit year: 16, month: 11, day: 09, seconds: 00
[Link] 216/338
13/11/2016 python Basic and Intermediate Level Modules
In [175]: print [Link]("four‐digit year: %Y, two‐digit year: %y, month: %m, monthI
nWords: %b, day: %d")
four‐digit year: 2016, two‐digit year: 16, month: 11, monthInWords: Nov, day:
09
NOTE: Both time and datetime modules can be used together
In [176]: t = [Link]() # For the local timezone
print t
print [Link]()
print "Epoch Seconds:", [Link]([Link]())
2016‐11‐09 [Link].509000
time.struct_time(tm_year=2016, tm_mon=11, tm_mday=9, tm_hour=18, tm_min=17, t
m_sec=39, tm_wday=2, tm_yday=314, tm_isdst=‐1)
Epoch Seconds: 1478695659.0
In [177]: t = [Link]() # For UTC
print t
print "Epoch Seconds:", [Link]([Link]())
2016‐11‐09 [Link].812000
Epoch Seconds: 1478675874.0
timeit module
In [178]: import timeit
In [179]: logic = '[x for x in xrange(10) if x%2 != 0]'
eval(logic) # eval() ‐ builtin function to execute a statement
Out[179]: [1, 3, 5, 7, 9]
In [180]: t = [Link](logic)
print t
print "1000 repeats of this logic takes :", [Link](1000), " seconds"
<[Link] instance at 0x0363D058>
1000 repeats of this logic takes : 0.00273679175956 seconds
In [181]: print "10,00,000 repeats of this logic takes :", [Link](1000000), " seconds"
10,00,000 repeats of this logic takes : 2.25790503286 seconds
In [183]: [Link]('range(12)')
Out[183]: 0.6488182478785234
[Link] 217/338
13/11/2016 python Basic and Intermediate Level Modules
In [184]: [Link]('xrange(12)')
Out[184]: 0.26588960871009704
Python file types
.pyw This is windows executable
.pyc compiled python bytecode file, for a particular .py file.
.pyd python dll file
.pyc file is platformindependent, yet interpreter dependent. The interpreter checks for the .py file last modified
time stamp with that of .pyc file. If there is a mismatch, then that .pyc file will be discarded, and a new .pyc file
will be created.
It is created either
1. when a particular _.py_ file is imported in another python script and/or in pyth
on interpreter.
2. Manually _.pyc_ file can be created, when the _.py_ file is compiled using py_co
mpile
python ‐m py_compile [Link]
In [186]: import os
In [187]: [Link]('.')
Out[187]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'myNewFolder',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
In [188]: code = "\
with open('[Link]', 'ab+') as myf:\
[Link]('This is my final struggle to succes. So, I will give my best')\
[Link]()\
"
with open('[Link]', 'ab+') as f:
[Link](code)
[Link] 218/338
13/11/2016 python Basic and Intermediate Level Modules
In [189]: [Link]('.')
Out[189]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'python ‐ Basic Level [Link]',
'[Link]']
INFERENCE: .pyw is executable in windows, and it can be used when no i/o operations are involved for the
script
[Link] 219/338
13/11/2016 python Basic and Intermediate Level Modules
In [190]: #!/usr/bin/python
'''
Purpose: module importing demonstration
'''
# [Link]
vowels = 'aeiou'
luckyNumber = 1321
def firstFunction():
'''
This is firstFunction
:return: None
'''
print "This is first function"
def addition(a,b):
'''
performs addition operation
ex: addition(12, 34)
returns: a+b
'''
return a+b
def subtraction(a,b):
'''
performs subtraction operation
'''
return a‐b
def multiplication(a,b):
'''
performs multiplication operation
'''
return a*b
if __name__ == '__main__':
print 'This script is executed directly'
else:
print 'This script is imported from another module'
This script is executed directly
[Link] 220/338
13/11/2016 python Basic and Intermediate Level Modules
In [193]: [Link]('.')
Out[193]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'python ‐ Basic Level [Link]',
'[Link]']
In [194]: import newScript #.py extension should not be given
This script is imported from another module
In [195]: [Link]('.') # observe the .pyc file
Out[195]: ['.ipynb_checkpoints',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'[Link]',
'python ‐ Basic Level [Link]',
'[Link]']
In [196]: print newScript.__doc__
Purpose: module importing demonstration
In [197]: print dir(newScript)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'additio
n', 'firstFunction', 'luckyNumber', 'multiplication', 'subtraction', 'vowel
s']
In [198]: [Link]()
This is first function
[Link] 221/338
13/11/2016 python Basic and Intermediate Level Modules
In [199]: help([Link])
Help on function addition in module newScript:
addition(a, b)
performs addition operation
ex: addition(12, 34)
returns: a+b
In [200]: [Link](99, 67)
Out[200]: 166
callable() buitlin function; results whether a particular object is callable or not
In [201]: callable([Link])
Out[201]: True
In [202]: [Link](12.23, 6.07)
Out[202]: 18.3
In [203]: [Link](12.23, 6.07)
Out[203]: 6.16
The [Link] file was modified with better help for functions. Also, two static variables were added.
In [204]: help([Link])
Help on function addition in module newScript:
addition(a, b)
performs addition operation
ex: addition(12, 34)
returns: a+b
Notice that the changes were not reflected.
[Link] 222/338
13/11/2016 python Basic and Intermediate Level Modules
Modifying the script of load modules needs reloading the module to get the changes to be affected in the
working script, or interpreter.
In python 2.x,
reload(<user‐defined Module name>)
ex: reload(newScript)
In python 3.x,
import imp
[Link](<user‐defined Module name>)
[Link](newScript)
or
import importlib
[Link](<user‐defined Module name>)
[Link](newScript)
There are various other modules like xreload, reimport with additional functionalities, for reloading the
modules.
In [205]: reload(newScript) # To get the changes
This script is imported from another module
Out[205]: <module 'newScript' from '[Link]'>
In [206]: help([Link])
Help on function addition in module newScript:
addition(a, b)
performs addition operation
ex: addition(12, 34)
returns: a+b
In [207]: print dir(newScript)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'additio
n', 'firstFunction', 'luckyNumber', 'multiplication', 'subtraction', 'vowel
s']
In [208]: [Link]
Out[208]: 1321
To ensure that certain part of logic should be executed only when the script is independently called, write that
logic under if __name__ == '__main__' condition
[Link] 223/338
13/11/2016 python Basic and Intermediate Level Modules
In [209]: reload(newScript)
This script is imported from another module
Out[209]: <module 'newScript' from '[Link]'>
At times, if the imported module has any dependencies on other imported modules, those functionality will not
get refreshed.
In that case, it would be better to delete that imported module object.
#del <importedModuleName>
del newScript
or
# using sys module
import sys
#del [Link][<importedModuleName>]
del [Link][newScript]
In [210]: del newScript # deletes the imported object from the interpreter cache
In [211]: newScript # results in error, as that object is no more present in inter
preter cache
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐211‐9b0d1d72cb8a> in <module>()
‐‐‐‐> 1 newScript # results in error, as that object is no more present
in interpreter cache
NameError: name 'newScript' is not defined
In [212]: import newScript
print dir(newScript)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'additio
n', 'firstFunction', 'luckyNumber', 'multiplication', 'subtraction', 'vowel
s']
In [213]: newScript.__file__ # Observe that the compiled bytecode object is called; no
t the .py file
Out[213]: '[Link]'
In [214]: newScript.__doc__
Out[214]: '\n\tPurpose: module importing demonstration\n\t\n'
[Link] 224/338
13/11/2016 python Basic and Intermediate Level Modules
In [215]: print newScript.__doc__
Purpose: module importing demonstration
In [216]: help(newScript)
Help on module newScript:
NAME
newScript ‐ Purpose: module importing demonstration
FILE
c:\users\home\google drive\python\tut\complete material\[Link]
FUNCTIONS
addition(a, b)
performs addition operation
ex: addition(12, 34)
returns: a+b
firstFunction()
This is firstFunction
:return: None
multiplication(a, b)
performs multiplication operation
subtraction(a, b)
performs subtraction operation
DATA
luckyNumber = 1321
vowels = 'aeiou'
NOTE: Ensure that the script related docstrings were written, just after the shebang line.
eval,exec, execfile, compile and py_compile
eval(str,globals,locals) This function executes an expression string and returns the result.
In [1]: eval('2+3') # returns the output
Out[1]: 5
In [2]: eval("'Python'*3") # observe both single(') and double(") quotes
Out[2]: 'PythonPythonPython'
[Link] 225/338
13/11/2016 python Basic and Intermediate Level Modules
In [3]: eval("eval('2+3')")
Out[3]: 5
In [4]: eval("print 'Hello World!'")
File "<string>", line 1
print 'Hello World!'
^
SyntaxError: invalid syntax
Inference: statement execution is not possible with eval()
exec(filename,globals,locals) function executes the statements
In [5]: exec("print 'Hello World!'") # exec() executes a string containing arbitrary
python code
Hello World!
In [6]: exec('2+34') # doesn't return output
In [7]: exec("exec('2+34')") # doesn't return output
Interview Question : what is the difference between eval() and exec() ?
In [8]: a=[1,2,3,45]
exec "for i in a: print i"
1
2
3
45
In [9]: exec '\
a=[1,2,3,45];\
exec "for i in a: print i"'
1
2
3
45
In [10]: exec("'Hello World!'")
In [11]: exec("print 'Hello World!'")
Hello World!
[Link] 226/338
13/11/2016 python Basic and Intermediate Level Modules
execfile(filename,globals,locals) function executes the contents of a file
In [12]: #!/usr/bin/python
# [Link]
print "HelloWorld!"
birds=['parrot','hen','hyna']
for b in birds:
print b
HelloWorld!
parrot
hen
hyna
In [13]: del birds, b
In [14]: import os;
[Link]([Link]())
Out[14]: ['.ipynb_checkpoints',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'[Link]',
'python ‐ Basic Level [Link]',
'python+‐+Basic+Level+[Link]']
Observe that '[Link]' is placed in the current working directory
In [15]: execfile("[Link]") # To execute the python script
HelloWorld!
parrot
hen
hyna
In [16]: myGlobals={'x':7,'y':10,'birds':['parrot','pigeon','sparrow']}
myLocals={'x':2,'y':20}
[Link] 227/338
13/11/2016 python Basic and Intermediate Level Modules
In [17]: a=eval("3*x+4*y",myGlobals,myLocals) # locals are preferred ‐ 3*2+ 4*20 =
86
print a
86
In [18]: a=eval("3*x+4*y",myLocals, myGlobals)
# here, also, locals are preferred.
# syntax: eval(string, global_variables, local_variables)
print a
61
In [19]: myLocals ={}
a=eval("3*x+4*y",myLocals, myGlobals) # In the absence of locals, globals ar
e choosen
print a
61
In [20]: myLocals ={'birds':['localBird','myLocal Bird','sparrow']}
In [21]: exec "for b in birds: print b" in myGlobals,myLocals
localBird
myLocal Bird
sparrow
In [22]: exec "for b in birds: print b" in myLocals, myGlobals # preference will be don
e based on position
parrot
pigeon
sparrow
In [23]: execfile("[Link]",myGlobals,myLocals) # Values present within script are
preferred. there are more local
HelloWorld!
parrot
hen
hyna
Observe that the values for 'birds' is preferred to from '[Link]'
In [24]: del birds
[Link] 228/338
13/11/2016 python Basic and Intermediate Level Modules
In [25]: #!/usr/bin/python
# [Link]
print "HelloWorld!"
for b in birds:
print b
HelloWorld!
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐25‐74f39257017d> in <module>()
4
5
‐‐‐‐> 6 for b in birds:
7 print b
NameError: name 'birds' is not defined
In [26]: execfile("[Link]",myGlobals,myLocals) # problem with ipython environmen
t, as [Link] is still not updated
HelloWorld!
parrot
hen
hyna
In [30]: del fileName
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐30‐574981b7b5bd> in <module>()
‐‐‐‐> 1 del fileName
NameError: name 'fileName' is not defined
In [31]: execfile("[Link]",myGlobals,myLocals) # problem with ipython environmen
t, as [Link] is still not updated
HelloWorld!
parrot
hen
hyna
In [32]: execfile("[Link]",myGlobals,myLocals) # problem with ipython environmen
t, as [Link] is still not updated
HelloWorld!
parrot
hen
hyna
In [33]: myLocals = {'birds':['ostrich','vulchur','Bat']}
[Link] 229/338
13/11/2016 python Basic and Intermediate Level Modules
In [34]: execfile("[Link]",myGlobals,myLocals)
HelloWorld!
ostrich
vulchur
Bat
In [35]: myLocals
Out[35]: {'b': 'Bat', 'birds': ['ostrich', 'vulchur', 'Bat']}
[Link] 230/338
13/11/2016 python Basic and Intermediate Level Modules
In [36]: myGlobals
[Link] 231/338
13/11/2016 python Basic and Intermediate Level Modules
Out[36]:
[Link] 232/338
13/11/2016 python Basic and Intermediate Level Modules
{'__builtins__': {'ArithmeticError': ArithmeticError,
'AssertionError': AssertionError,
'AttributeError': AttributeError,
'BaseException': BaseException,
'BufferError': BufferError,
'BytesWarning': BytesWarning,
'DeprecationWarning': DeprecationWarning,
'EOFError': EOFError,
'Ellipsis': Ellipsis,
'EnvironmentError': EnvironmentError,
'Exception': Exception,
'False': False,
'FloatingPointError': FloatingPointError,
'FutureWarning': FutureWarning,
'GeneratorExit': GeneratorExit,
'IOError': IOError,
'ImportError': ImportError,
'ImportWarning': ImportWarning,
'IndentationError': IndentationError,
'IndexError': IndexError,
'KeyError': KeyError,
'KeyboardInterrupt': KeyboardInterrupt,
'LookupError': LookupError,
'MemoryError': MemoryError,
'NameError': NameError,
'None': None,
'NotImplemented': NotImplemented,
'NotImplementedError': NotImplementedError,
'OSError': OSError,
'OverflowError': OverflowError,
'PendingDeprecationWarning': PendingDeprecationWarning,
'ReferenceError': ReferenceError,
'RuntimeError': RuntimeError,
'RuntimeWarning': RuntimeWarning,
'StandardError': StandardError,
'StopIteration': StopIteration,
'SyntaxError': SyntaxError,
'SyntaxWarning': SyntaxWarning,
'SystemError': SystemError,
'SystemExit': SystemExit,
'TabError': TabError,
'True': True,
'TypeError': TypeError,
'UnboundLocalError': UnboundLocalError,
'UnicodeDecodeError': UnicodeDecodeError,
'UnicodeEncodeError': UnicodeEncodeError,
'UnicodeError': UnicodeError,
'UnicodeTranslateError': UnicodeTranslateError,
'UnicodeWarning': UnicodeWarning,
'UserWarning': UserWarning,
'ValueError': ValueError,
'Warning': Warning,
'WindowsError': WindowsError,
'ZeroDivisionError': ZeroDivisionError,
'__IPYTHON__': True,
'__debug__': True,
'__doc__': "Built‐in functions, exceptions, and other objects.\n\nNoteworth
[Link] 233/338
13/11/2016 python Basic and Intermediate Level Modules
y: None is the `nil' object; Ellipsis represents `...' in slices.",
'__import__': <function __import__>,
'__name__': '__builtin__',
'__package__': None,
'abs': <function abs>,
'all': <function all>,
'any': <function any>,
'apply': <function apply>,
'basestring': basestring,
'bin': <function bin>,
'bool': bool,
'buffer': buffer,
'bytearray': bytearray,
'bytes': str,
'callable': <function callable>,
'chr': <function chr>,
'classmethod': classmethod,
'cmp': <function cmp>,
'coerce': <function coerce>,
'compile': <function compile>,
'complex': complex,
'copyright': Copyright (c) 2001‐2016 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 [Link].
All Rights Reserved.
Copyright (c) 1995‐2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991‐1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.,
'credits': Thanks to CWI, CNRI, [Link], Zope Corporation and a cast
of thousands
for supporting Python development. See [Link] for more informa
tion.,
'delattr': <function delattr>,
'dict': dict,
'dir': <function dir>,
'divmod': <function divmod>,
'dreload': <function [Link]._dreload>,
'enumerate': enumerate,
'eval': <function eval>,
'execfile': <function execfile>,
'file': file,
'filter': <function filter>,
'float': float,
'format': <function format>,
'frozenset': frozenset,
'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.
[Link] object at 0x03479A70>>,
'getattr': <function getattr>,
'globals': <function globals>,
'hasattr': <function hasattr>,
'hash': <function hash>,
'help': Type help() for interactive help, or help(object) for help about ob
ject.,
[Link] 234/338
13/11/2016 python Basic and Intermediate Level Modules
'hex': <function hex>,
'id': <function id>,
'input': <function [Link].<lambda>>,
'int': int,
'intern': <function intern>,
'isinstance': <function isinstance>,
'issubclass': <function issubclass>,
'iter': <function iter>,
'len': <function len>,
'license': Type license() to see the full license text,
'list': list,
'locals': <function locals>,
'long': long,
'map': <function map>,
'max': <function max>,
'memoryview': memoryview,
'min': <function min>,
'next': <function next>,
'object': object,
'oct': <function oct>,
'open': <function open>,
'ord': <function ord>,
'pow': <function pow>,
'print': <function print>,
'property': property,
'range': <function range>,
'raw_input': <bound method IPythonKernel.raw_input of <[Link].I
PythonKernel object at 0x03479B70>>,
'reduce': <function reduce>,
'reload': <function reload>,
'repr': <function repr>,
'reversed': reversed,
'round': <function round>,
'set': set,
'setattr': <function setattr>,
'slice': slice,
'sorted': <function sorted>,
'staticmethod': staticmethod,
'str': str,
'sum': <function sum>,
'super': super,
'tuple': tuple,
'type': type,
'unichr': <function unichr>,
'unicode': unicode,
'vars': <function vars>,
'xrange': xrange,
'zip': <function zip>},
'b': 'sparrow',
'birds': ['parrot', 'pigeon', 'sparrow'],
'x': 7,
'y': 10}
[Link] 235/338
13/11/2016 python Basic and Intermediate Level Modules
when a string is passed to exec,eval(), or execfile(), parser first compiles to create bytecode.
To remove this redundant process every time, compile will create precompiled bytecode, which can be used
everytime, till the code is not changed
compile (str, filename, kind) function a string compiled into byte code, str is the string to be compiled, the
filename is to define the string variable file, the kind parameter specifies the type of code is compiled
' Single 'refers to a single statement,
' exec 'means more than one statement,
' eval 'means an expression.
compile () function returns a code object, the object, of course, can also be passed to the eval () function and
the exec statement to perform, for example, :
In [37]: str = "for i in range (0,10): print i,"
c = compile (str,'', 'exec') # compiled to byte code object
exec c # execution
0 1 2 3 4 5 6 7 8 9
In [38]: str2 = "for i in range (0,10): print i,"
c2 = compile (str2,'', 'eval') # eval() can't execute statements
eval(c2)
File "<string>", line 1
for i in range (0,10): print i,
^
SyntaxError: invalid syntax
In [57]: str2 = "[0,1,2,3,4,5,6,7,8,9]+ [99,88,77]"
c2 = compile (str2,'', 'eval') # eval() can execute expressions
eval(c2)
Out[57]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99, 88, 77]
In [58]: print c2, type(c2) # code object
<code object <module> at 0384DCC8, file "", line 1> <type 'code'>
In [59]: print dir(c2)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '_
_ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt
__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setat
tr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellva
rs', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co
_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize',
'co_varnames']
py_compile It is a module to create bytecode file, .pyc
[Link] 236/338
13/11/2016 python Basic and Intermediate Level Modules
In [64]: import py_compile
py_compile.compile('[Link]')
In [65]: [Link]('.')
Out[65]: ['.ipynb_checkpoints',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'[Link]',
'python ‐ Basic Level [Link]',
'python+‐+Basic+Level+[Link]']
In Python 2.x, input(...) is equivalent to eval(raw_input(...))
In Python 3.x, raw_input() was renamed input()
Iterators
Process to iterate through all the elements of a collection
In [217]: for i in range(7):
print 'Hey ', i
Hey 0
Hey 1
Hey 2
Hey 3
Hey 4
Hey 5
Hey 6
In [218]: for i in [12, 23, 34, 54, 56]:
print i,
12 23 34 54 56
In [219]: print [char for char in 'Python Programming']
['P', 'y', 't', 'h', 'o', 'n', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm',
'i', 'n', 'g']
[Link] 237/338
13/11/2016 python Basic and Intermediate Level Modules
In [220]: for key in {'a': 'Apple', 'b': 'Ball'}:
print key,
a b
NOTE: The default iterator for dictionary is keys()
In [221]: for item in {'a': 'Apple', 'b': 'Ball'}.items():
print item,
('a', 'Apple') ('b', 'Ball')
Also, iterators can be used in other ways
In [222]: '‐'.join(['Date', 'Month', 'Year'])
Out[222]: 'Date‐Month‐Year'
In [223]: '‐'.join({'Date':8, 'Month':4, 'Year': 2016})
Out[223]: 'Date‐Year‐Month'
In [224]: num = {'Date':8, 'Month':4, 'Year': 2016}
print [Link]()
'‐'.join(str([Link]()))
[8, 2016, 4]
Out[224]: '[‐8‐,‐ ‐2‐0‐1‐6‐,‐ ‐4‐]'
Assignment : using join method of strings try to display as '842016' if the input dictionary is {'Date':8,
'Month':4, 'Year': 2016}
In [227]: list('Programming')
Out[227]: ['P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']
In [228]: list({'Date':8, 'Month':4, 'Year': 2016})
Out[228]: ['Date', 'Year', 'Month']
Iteration (Iter) protocol
iter() takes an iterable object and retuns an iterator
next() method call to retun elements from the iterator. Results in StopIterator error, if the elements are not
present
[Link] 238/338
13/11/2016 python Basic and Intermediate Level Modules
In [229]: range(9)
Out[229]: [0, 1, 2, 3, 4, 5, 6, 7, 8]
In [230]: xrange(9)
Out[230]: xrange(9)
In [231]: a = xrange(9)
In [232]: print a
xrange(9)
In [233]: for i in xrange(9):
print i,
print "\n"
for i in range(9):
print i,
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
In [234]: li = iter([12,23,34]) # list iterator # iter() builtin function ‐ to
create iterators
print li, type(li)
<listiterator object at 0x0354C930> <type 'listiterator'>
In [235]: l = [12, 23, 45, 56]
print l, type(l)
li = iter(l)
print li, type(li)
[12, 23, 45, 56] <type 'list'>
<listiterator object at 0x0354C990> <type 'listiterator'>
In [236]: print dir(l)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__del
slice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '_
_getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '_
_init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__
new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul_
_', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '_
_subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'rem
ove', 'reverse', 'sort']
[Link] 239/338
13/11/2016 python Basic and Intermediate Level Modules
In [237]: print dir(li)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__
hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subc
lasshook__', 'next']
In [238]: print [Link]()
12
In [239]: print [Link]()
23
In [240]: print [Link](), [Link]()
45 56
In [241]: print [Link]() # As there are no more values in it
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐241‐27f9b92ed352> in <module>()
‐‐‐‐> 1 print [Link]()
StopIteration:
In [242]: t = (12, 23, 45, 56)
print t, type(t)
ti = iter(t) # tuple iterator
print ti, type(ti)
(12, 23, 45, 56) <type 'tuple'>
<tupleiterator object at 0x0354CF50> <type 'tupleiterator'>
In [243]: print dir(ti)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__
hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subc
lasshook__', 'next']
In [244]: [Link]()
Out[244]: 12
In [245]: print [Link](), [Link](), [Link]()
23 45 56
[Link] 240/338
13/11/2016 python Basic and Intermediate Level Modules
In [246]: print [Link]()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐246‐6ce46c9248fd> in <module>()
‐‐‐‐> 1 print [Link]()
StopIteration:
In [247]: s = {12,23,34}
print s, type(s)
si = iter(s) # set iterator
print si, type(si)
set([34, 12, 23]) <type 'set'>
<setiterator object at 0x035421E8> <type 'setiterator'>
In [248]: print dir(si)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__
hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subc
lasshook__', 'next']
In [249]: print [Link](), [Link](), [Link](), [Link]() # exception is for the 4th
call
34 12 23
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐249‐8c532c0610cf> in <module>()
‐‐‐‐> 1 print [Link](), [Link](), [Link](), [Link]()
StopIteration:
Assignment : Try the iter() protocol for frozenset
In [250]: d = {'a':12, 'b':23, 'c':34}
print d, type(d)
di = iter(d) # dictionary iterator
print di, type(di)
{'a': 12, 'c': 34, 'b': 23} <type 'dict'>
<dictionary‐keyiterator object at 0x035F57B0> <type 'dictionary‐keyiterator'>
In [251]: print dir(di)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__
hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subc
lasshook__', 'next']
[Link] 241/338
13/11/2016 python Basic and Intermediate Level Modules
In [252]: print [Link]()
a
In [253]: print [Link](), [Link](), [Link]()
c b
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐253‐35f26283dd3e> in <module>()
‐‐‐‐> 1 print [Link](), [Link](), [Link]()
StopIteration:
Assignment : Try to get the dictionary pair in dictionary iterator object
Generators
It simplifies the creation of iterators
It is a function that returns a sequence of results, rather than a single result.
If a function uses the 'yield' keyword, it creates a generator object.
yield is different from return.
Interview Question : what is the difference between iterator and generator?
Interview Question : what is the difference between funnction and generator?
In [254]: def count(n):
print "Stating to count!"
i = 0
while i<n:
yield i
i+=1
print '$', i
#return i # PEP8 strongly discourages usage of yield and retun, in same
function
In [255]: c = count(6)
In [256]: print c
<generator object count at 0x03542698>
In [257]: print [Link]() # execution starts when .next() is given # It stores the s
tate, after execution
Stating to count!
0
[Link] 242/338
13/11/2016 python Basic and Intermediate Level Modules
In [258]: [Link]()
Out[258]: 1
In [259]: print [Link](), [Link](), [Link]()
2 3 4
In [260]: [Link]()
Out[260]: 5
In [261]: [Link]() # because there are no more values
$ 6
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐261‐69882ed18d78> in <module>()
‐‐‐‐> 1 [Link]() # because there are no more values
StopIteration:
This function doesn't get executed when the function call is made; but executed when the next() method call is
done.
In [262]: def foo():
print "Start the function!"
for i in range(3):
print "before yield", i
yield i
print "after yield", i
print "end of function "
In [263]: f = foo()
In [264]: type(f)
Out[264]: generator
In [265]: [Link]()
Start the function!
before yield 0
Out[265]: 0
In [266]: [Link]()
after yield 0
before yield 1
Out[266]: 1
[Link] 243/338
13/11/2016 python Basic and Intermediate Level Modules
In [267]: [Link]()
after yield 1
before yield 2
Out[267]: 2
In [268]: [Link]()
after yield 2
end of function
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐268‐c3e65e5362fb> in <module>()
‐‐‐‐> 1 [Link]()
StopIteration:
Interview Question : What is the difference between yield and return?
yield will halt the execution, untill the next next() method is encountered. Where as return will return the result
at only, and won't go back to the function
Generator function terminates by calling either return or by raising StopIteration error.
It is not recommended to place both yield and return for the same function.
In [269]: def yrange(n):
i = 0
while i<n:
yield i
i+=1
In [270]: for i in yrange(3):
print i
0
1
2
In [271]: y = yrange(3)
In [272]: print y, type(y)
<generator object yrange at 0x035FD4B8> <type 'generator'>
In [273]: [Link]()
Out[273]: 0
[Link] 244/338
13/11/2016 python Basic and Intermediate Level Modules
In [274]: for i in y:
print i # prints the remaining elements in the generator object
1
2
In [275]: def integers():
"""Infinite sequence of integers."""
i = 1
while True:
yield i
i = i + 1
def squares():
for i in integers():
yield i * i
def take(n, seq):
"""Returns first n values from the given sequence."""
seq = iter(seq)
result = []
try:
for i in range(n):
[Link]([Link]())
except StopIteration:
pass
return result
print take(5, squares()) # prints [1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
In [276]: #!/usr/bin/python
# Purpose: To make a Fibonacci generator.
# [Link]
def fibonacci(max):
n, a, b = 0, 0, 1
while n < max:
yield a
a, b = b, a + b
n = n + 1
if __name__ == '__main__': # This condition gets executed, only if the pyt
hon script is directly executed
fib10 = fibonacci(10)
for i in fib10:
print i,
0 1 1 2 3 5 8 13 21 34
[Link] 245/338
13/11/2016 python Basic and Intermediate Level Modules
In [277]: import os; [Link]([Link]())
Out[277]: ['.ipynb_checkpoints',
'[Link]',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'[Link]',
'python ‐ Basic Level [Link]',
'[Link]']
In [278]: from fibGenerator import fibonacci
In [279]: fibonacci
Out[279]: <function [Link]>
In [280]: type(fibonacci)
Out[280]: function
In [281]: fib = fibonacci(10)
In [282]: print fib, type(fib)
<generator object fibonacci at 0x0355C3C8> <type 'generator'>
In [283]: [Link]()
Out[283]: 0
In [284]: for i in fib:
print i,
1 1 2 3 5 8 13 21 34
In [285]: reload(fibonacci)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐285‐e616c20e4b49> in <module>()
‐‐‐‐> 1 reload(fibonacci)
TypeError: reload() argument must be module
[Link] 246/338
13/11/2016 python Basic and Intermediate Level Modules
In [286]: #del fibonacci
import fibGenerator
reload(fibGenerator) # .pyc file will be recreated
Out[286]: <module 'fibGenerator' from '[Link]'>
In [287]: f = [Link](10)
for i in f:
print i,
0 1 1 2 3 5 8 13 21 34
Generator Expressions
tuple comprehension
It is generator version of list comprehension.
List comprehension creates a sequence that contains the resulting data. Generator expression
creates a generator that knows how to produce data on demand.
Generator Expression (GE) improves performance and memory usage
GE creates objects, which can't be indexed.
syntax:
(expression for item1 in iterable1
for item2 in iterable2
for item3 in iterable3
.
.
.
for itemN in iterableN
if condition)
`
In [288]: b = (10*i for i in [1,2,3,4])
In [289]: print b, type(b)
<generator object <genexpr> at 0x03542878> <type 'generator'>
In [290]: [Link]()
Out[290]: 10
In [291]: [Link]()
Out[291]: 20
[Link] 247/338
13/11/2016 python Basic and Intermediate Level Modules
In [292]: [Link](), [Link](), [Link]()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
StopIteration Traceback (most recent call last)
<ipython‐input‐292‐10d82aa87e61> in <module>()
‐‐‐‐> 1 [Link](), [Link](), [Link]()
StopIteration:
In [293]: c = list(b) # Generator expression to list conversion
print c, type(c)
[] <type 'list'>
In [294]: b = (10*i for i in [1,2,3,4])
c = list(b) # Generator expression to list conversion
print c, type(c)
[10, 20, 30, 40] <type 'list'>
Interview Question : what is the result of this :
sum(i*i for i in range(10))
In [295]: sum(i*i for i in range(10)) # sum() ‐ builtin function to result the summat
ion
Out[295]: 285
In [296]: sum([i*i for i in range(10)])
Out[296]: 285
Assignment : Try to evaluate the decimal and complex numbers in sum() function
Assignment : Try to mimic the sum() function functionality using reduce function
Itertools
chain chains multiple iterators together
izip iterable version of zip
product computes the cartesian product of input iterables
product(A,B) is same as ((x,y) for x in A for y in B)
In [297]: import itertools
[Link] 248/338
13/11/2016 python Basic and Intermediate Level Modules
In [298]: li1 = iter([1,2,3])
li2 = iter([4,5,6])
a = [Link](li1, li2)
print type(a)
print a
<type '[Link]'>
<[Link] object at 0x03545530>
In [299]: list(a)
Out[299]: [1, 2, 3, 4, 5, 6]
In [300]: list([Link]([1,2,3], [4,5,6]))
Out[300]: [1, 2, 3, 4, 5, 6]
In [301]: list([Link]([[1,2,3], [4,5,6]]))
Out[301]: [[1, 2, 3], [4, 5, 6]]
In [302]: list([Link]([[1,2,3], [4,5,6]], [99, 79, 69]))
Out[302]: [[1, 2, 3], [4, 5, 6], 99, 79, 69]
In [303]: list([Link](['ABC', 'DEF']))
Out[303]: ['ABC', 'DEF']
In [304]: list([Link].from_iterable(['ABC', 'DEF']))
Out[304]: ['A', 'B', 'C', 'D', 'E', 'F']
In [305]: list([Link].from_iterable([[1,2,3], [4,5,6]], [99, 79, 69]))
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐305‐8061948b2310> in <module>()
‐‐‐‐> 1 list([Link].from_iterable([[1,2,3], [4,5,6]], [99, 79, 69]))
TypeError: from_iterable() takes exactly one argument (2 given)
Interview Question : How to convert a multidimensional list to a flat list
input: [[1,2,3], [4,5,6], [99, 79, 69]]
output: [1, 2, 3, 4, 5, 6, 99, 79, 69]
In [306]: list([Link].from_iterable([[1,2,3], [4,5,6], [99, 79, 69]]))
Out[306]: [1, 2, 3, 4, 5, 6, 99, 79, 69]
[Link] 249/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Try to do multidimensional list to single dimensional list, or list flattening, using itertools
In [307]: for x, y in [Link](["a", "b", "c"], [1, 2, 3]):
print x,y
a 1
b 2
c 3
In [308]: list(itertools.izip_longest('abcd', 'ABCD', fillvalue='‐'))
Out[308]: [('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D')]
In [309]: list(itertools.izip_longest('abcd', 'AB', fillvalue='‐'))
Out[309]: [('a', 'A'), ('b', 'B'), ('c', '‐'), ('d', '‐')]
In [310]: list(itertools.izip_longest('ab', 'ABCD', fillvalue='‐'))
Out[310]: [('a', 'A'), ('b', 'B'), ('‐', 'C'), ('‐', 'D')]
In [311]: list(itertools.izip_longest('cd', 'ABCD', fillvalue='‐'))
Out[311]: [('c', 'A'), ('d', 'B'), ('‐', 'C'), ('‐', 'D')]
In [312]: print list([Link]([1,2,3], repeat = 2))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
In [313]: print list([Link]([1,2,3], repeat = 0))
[()]
In [314]: print list([Link]([1,2,3], repeat = 1))
[(1,), (2,), (3,)]
In [315]: print list([Link]([1,2,3], repeat = 3))
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1),
(1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2),
(2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3),
(3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
In [316]: print list([Link]([1,2,3],[3,4]))
[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
In [317]: s = [[1,2,3],[3,4,5]]
print list([Link](*s)) # UNPACKING
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
[Link] 250/338
13/11/2016 python Basic and Intermediate Level Modules
In [318]: print list([Link](s))
[([1, 2, 3],), ([3, 4, 5],)]
In [319]: s = [(1,2,3),[3,4,5]] # non‐homegeneous list
print list([Link](*s))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
In [320]: s = [(1,2,(45,78,9),3),[3,4,[33, (44,)],5]] # multi‐dimensional list
list([Link](*s))
Out[320]: [(1, 3),
(1, 4),
(1, [33, (44,)]),
(1, 5),
(2, 3),
(2, 4),
(2, [33, (44,)]),
(2, 5),
((45, 78, 9), 3),
((45, 78, 9), 4),
((45, 78, 9), [33, (44,)]),
((45, 78, 9), 5),
(3, 3),
(3, 4),
(3, [33, (44,)]),
(3, 5)]
In [321]: t = ((1,2,(45,78,9),3),[3,4,[33, 44],5]) # multi‐dimensional tuple
print list([Link](*t)) # displaying as a list
[(1, 3), (1, 4), (1, [33, 44]), (1, 5), (2, 3), (2, 4), (2, [33, 44]), (2,
5), ((45, 78, 9), 3), ((45, 78, 9), 4), ((45, 78, 9), [33, 44]), ((45, 78,
9), 5), (3, 3), (3, 4), (3, [33, 44]), (3, 5)]
In [322]: print tuple([Link](*t)) # displaying as a tuple
((1, 3), (1, 4), (1, [33, 44]), (1, 5), (2, 3), (2, 4), (2, [33, 44]), (2,
5), ((45, 78, 9), 3), ((45, 78, 9), 4), ((45, 78, 9), [33, 44]), ((45, 78,
9), 5), (3, 3), (3, 4), (3, [33, 44]), (3, 5))
In [323]: list([Link]('AB',2))
Out[323]: [('A', 'B'), ('B', 'A')]
In [324]: list([Link]('AB',2))
Out[324]: [('A', 'B')]
In [325]: list(itertools.combinations_with_replacement('AB',2))
Out[325]: [('A', 'A'), ('A', 'B'), ('B', 'B')]
[Link] 251/338
13/11/2016 python Basic and Intermediate Level Modules
In [326]: list([Link]('ABC',2))
Out[326]: [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
In [327]: list([Link]('ABC',3))
Out[327]: [('A', 'B', 'C'),
('A', 'C', 'B'),
('B', 'A', 'C'),
('B', 'C', 'A'),
('C', 'A', 'B'),
('C', 'B', 'A')]
In [328]: list([Link]('ABC',3))
Out[328]: [('A', 'B', 'C')]
In [329]: list(itertools.combinations_with_replacement('ABC',3))
Out[329]: [('A', 'A', 'A'),
('A', 'A', 'B'),
('A', 'A', 'C'),
('A', 'B', 'B'),
('A', 'B', 'C'),
('A', 'C', 'C'),
('B', 'B', 'B'),
('B', 'B', 'C'),
('B', 'C', 'C'),
('C', 'C', 'C')]
In [330]: list([Link]('ABCDEF', [1,0,1,0,1,1]))
Out[330]: ['A', 'C', 'E', 'F']
In [331]: list([Link]('ABCDEF', [1,0,1,0,1,1,1]))
Out[331]: ['A', 'C', 'E', 'F']
In [332]: list([Link]('ABCDEF', [1,1,1,1,1,1,1]))
Out[332]: ['A', 'B', 'C', 'D', 'E', 'F']
In [333]: list([Link]('ABCDEF', [0,0,0,0,0,0,0]))
Out[333]: []
In [334]: list([Link]('ABCDEF', [1,0,0,0,0,0,0,1, 1, 1, 1, 1]))
Out[334]: ['A']
[Link] 252/338
13/11/2016 python Basic and Intermediate Level Modules
Exceptions
Almost all programming languages, except shell scripting and some scripting languages, possess exception
handling capabilities.
There are two kinds of errors in Python.
1. error code If something went wrong, the resulting error code is 1 to indicate the failure of a call.
2. Exception Used to handle exceptional cases.
In Python, the errors are handled by the interpreter by raising an exception and allowing that exception to be
handled.
Exceptions indicate errors and break out of the normal control flow of a program. An exception is raised using
raise statement.
Syntax:
try:
logic
...
except <ExceptionName1>, <alias identifier>:
logic to handle that exception
...
except <ExceptionName2> as <alias identifier>:
logic to handle that exception.
This logic gets executed, if error is not covered
in ExceptionName1 exception
...
else:
logic to execute if
there is no exception
...
finally:
logic to execute either
if exception occurs are not
...
Note : try and except are mandatory blocks. And, else and finally are optional blocks.
In [66]: result = 21/0
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ZeroDivisionError Traceback (most recent call last)
<ipython‐input‐66‐123e673b3321> in <module>()
‐‐‐‐> 1 result = 21/0
ZeroDivisionError: integer division or modulo by zero
[Link] 253/338
13/11/2016 python Basic and Intermediate Level Modules
In [67]: try:
result = 21/0
except:
print 'An error occurred!'
An error occurred!
This code handles all exceptions. But, we should know the error to do corresponding action
In [68]: try:
result = 21/0
except:
print 'An error occurred'
print 'The error is %s'%(ex)
An error occurred
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐68‐e878884284c3> in <module>()
3 except:
4 print 'An error occurred'
‐‐‐‐> 5 print 'The error is %s'%(ex)
6
NameError: name 'ex' is not defined
Here, the exception was resulted in the exception block
In [69]: try:
result = (1+2.3)/(2*4*0)
except:
print 'An error occurred'
try:
print 'The error is %s'%(ex)
except:
print 'variable "ex" is not defined'
An error occurred
variable "ex" is not defined
In [70]: import exceptions
try:
result = (1+2.3)/(2*4*0)
except [Link], ex:
print "The error is ", ex
The error is float division by zero
Ensure that all the exceptions are handled in the code
[Link] 254/338
13/11/2016 python Basic and Intermediate Level Modules
In [71]: try:
result = (1+2.3)/(2*4*0)
except ZeroDivisionError, ex: # better handling of exception
print "The error is ", repr(ex)
print "The error is ", ex
The error is ZeroDivisionError('float division by zero',)
The error is float division by zero
In [72]: try:
result = (w)*(1+2.3)/(2*4*0)
except ZeroDivisionError, ex: # better handling of exception
print "The error is ", ex
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐72‐d6eb5a1e6e41> in <module>()
1 try:
‐‐‐‐> 2 result = (w)*(1+2.3)/(2*4*0)
3 except ZeroDivisionError, ex: # better handling of exception
4 print "The error is ", ex
NameError: name 'w' is not defined
Interview Question : If there are two errors, suppose ZeroDivisionError and NameError, which error will be
resulted
In [73]: try:
result = (w)*(1+2.3)/(2*4*0)
except ZeroDivisionError, ex: # better handling of exception
print "The error is ", ex
print "The error is ", repr(ex)
except NameError, ex:
print "The error is ", ex
print "The error is ", repr(ex)
The error is name 'w' is not defined
The error is NameError("name 'w' is not defined",)
There should be a except to accept any unknown exception
In [74]: try:
result = (w)*(1+2.3)/(2*4*0)
except ZeroDivisionError, ex: # better handling of exception
print "The ZeroDivisionError is ", ex
except NameError, ex:
print "The NameError is ", ex
except [Link], ex:
print "The other error is ", ex
The NameError is name 'w' is not defined
[Link] 255/338
13/11/2016 python Basic and Intermediate Level Modules
control flow:
when there is no exception:
try ‐> else ‐> finally
when there is exception:
try ‐> except ‐> finally
In [75]: try:
result = (w)*(1+2.3)/(2*4*0)
except ZeroDivisionError, ex: # better handling of exception
print "The ZeroDivisionError is ", ex
except NameError, ex:
print "The NameError is ", ex
except [Link], ex:
print "The other error is ", ex
else:
print "try block executed successfully"
print "The result is ", result
finally:
print "Completed the try exception block"
The NameError is name 'w' is not defined
Completed the try exception block
In [76]: try:
result = (9)*(1+2.3)/(2*4*10)
except ZeroDivisionError, ex: # better handling of exception
print "The ZeroDivisionError is ", ex
except NameError, ex:
print "The NameError is ", ex
except [Link], ex:
print "The other error is ", ex
else:
print "try block executed successfully"
print "The result is ", result
finally:
print "Completed the try exception block"
try block executed successfully
The result is 0.37125
Completed the try exception block
As 'finally' gets executed in either case, it is seldom used. finally is generally used for cleanup action
[Link] 256/338
13/11/2016 python Basic and Intermediate Level Modules
In [77]: try:
x = float(raw_input("Your number: "))
inverse = 1.0 / x
finally:
print("There may or may not have been an exception.")
print "The inverse: ", inverse
Your number: 0.0
There may or may not have been an exception.
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ZeroDivisionError Traceback (most recent call last)
<ipython‐input‐77‐cd0c760fc506> in <module>()
1 try:
2 x = float(raw_input("Your number: "))
‐‐‐‐> 3 inverse = 1.0 / x
4 finally:
5 print("There may or may not have been an exception.")
ZeroDivisionError: float division by zero
Here, exception is throwed, as except block is missing
In [78]: try:
x = float(raw_input("Your number: "))
inverse = 1.0 / x
except [Link], ex:
print "The error is ", ex
finally:
print("There may or may not have been an exception.")
print "The inverse: ", inverse
Your number: 12
There may or may not have been an exception.
The inverse: 0.0833333333333
[Link] 257/338
13/11/2016 python Basic and Intermediate Level Modules
Builtin exceptions:
‐ Exception ‐ root of all exceptions
‐ SystemExit ‐ generated by [Link]()
‐ StopIteration ‐ Raised to stop iteration
‐ StandardError ‐ Base of all built‐in exceptions
‐ ArithmeticError ‐ Base for arithmetic exceptions
‐ FloatingPointError ‐ faliture of a floating‐point operation
‐ OverflowError ‐ Arithmetic overflow
‐ ZeroDivisionError ‐ Division or modulus operation with 0
‐ AssertionError ‐ Raised by assert statement
‐ AttributeError ‐ Raised when an attribute name is inval
id
‐ EnvironmentError ‐ Errors that occur externally to python
‐ IOError ‐ I/O or file‐related error
‐ OSError ‐ Operating System error
‐ EOFError ‐ Raised when end of file is reached
‐ ImportError ‐ Failure of import statement
‐ KeyboardInterrupt ‐ Generated by interrupt key (ctrl+C or
ctrl+D)
‐ LookupError ‐ Indexing and key errors
‐ IndexError ‐ Out‐of‐range sequence offset
‐ KeyError ‐ Non‐existent dictionary key
‐ MemoryError ‐ Out of memory error
‐ NameError ‐ Failure to find a local or global name
‐ UnboundLocalError ‐ unbound local variable
‐ ReferenceError ‐ Weak reference used after referent des
troyed
‐ RuntimeError ‐ A generic catch‐all error
‐ NotImplementedError ‐ unimplemented feature
‐ SyntaxError ‐ Parsing Error
‐ IndentationError ‐ Indentation error
‐ TabError ‐ Inconsistent tab usage(generated with
‐tt option)
‐ SystemError ‐ Non‐fatal system error in interpreter
‐ TypeError ‐ Passing an inappropriate type to an op
eration
‐ ValueError ‐ Invalid type
‐ UnicodeError ‐ unicode error
‐ UnicodeDecodeError ‐ unicode decoding error
‐ UnicodeEncodeError ‐ unicode encoding error
‐ UnicodeTranslateError ‐ unicode translation error
[Link] 258/338
13/11/2016 python Basic and Intermediate Level Modules
In [80]: try:
result = (w)*(1+2.3)/(2*4*0) # This statement contains both NameErro
r and ZeroDivisionError
except NameError, ex:
print "The NameError is ", ex
except ZeroDivisionError, ex: # better handling of exception
print "The ZeroDivisionError is ", ex
except StandardError, ex:
print "All Standard Errors are handled here"
print "The error is ",ex
except [Link], ex:
print "The other error is ", ex
else:
print "try block executed successfully"
print "The result is ", result
finally:
print "Completed the try exception block"
The NameError is name 'w' is not defined
Completed the try exception block
In the above example, notice that NameError caught the exception, rather than StandardError, due to its
placement preceeding StandardError exception.
In [81]: import exceptions
try:
result = (12)*(1+2.3)/(2*4*0) # This statement contains ONLY ZeroDiv
isionError
except [Link], ex: # It handles all exceptions
print "ALL the errors are handled here"
print "The other error is ", ex
except StandardError, ex: # handles only Standard Errors
print "All Standard Errors are handled here"
print "The error is ",ex
except ZeroDivisionError, ex:
print "The ZeroDivisionError is ", ex
except NameError, ex:
print "The NameError is ", ex
else:
print "try block executed successfully"
print "The result is ", result
finally:
print "Completed the try exception block"
ALL the errors are handled here
The other error is float division by zero
Completed the try exception block
In the above scenario, notice that Exception method of exceptions module is handling all the exceptions,
rather than the StandardError exception, due to its heirarchy.
[Link] 259/338
13/11/2016 python Basic and Intermediate Level Modules
In [82]: import exceptions
try:
result = (12)*(1+2.3)/(2*4*0) # This statement contains ONLY ZeroDiv
isionError
except StandardError, ex: # handles only Standard Errors
print "All Standard Errors are handled here"
print "The error is ",ex
except ZeroDivisionError, ex:
print "The ZeroDivisionError is ", ex
except NameError, ex:
print "The NameError is ", ex
except [Link], ex: # It handles all exceptions
print "ALL the errors are handled here"
print "The other error is ", ex
else:
print "try block executed successfully"
print "The result is ", result
finally:
print "Completed the try exception block"
All Standard Errors are handled here
The error is float division by zero
Completed the try exception block
In [84]: ### !/usr/bin/python
# [Link]
import exceptions
try:
a = int(input('please enter your number:'))
except NameError:
print "we should not enter zero" # It is a logic error. Logical erro
rs can't be handled
else:
print "the number a:%d" %(a)
finally:
print "finally clause"
please enter your number:12
the number a:12
finally clause
[Link] 260/338
13/11/2016 python Basic and Intermediate Level Modules
In [85]: #!/usr/bin/python
# [Link]
import exceptions
try:
a = int(input('please enter your number:'))
except NameError as ex:
print 'NameError occurred'
print ex
else:
print "the number a:%d" %(a)
finally:
print "finally clause"
please enter your number:'python'
finally clause
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ValueError Traceback (most recent call last)
<ipython‐input‐85‐200be7b5394b> in <module>()
4
5 try:
‐‐‐‐> 6 a = int(input('please enter your number:'))
7 except NameError as ex:
8 print 'NameError occurred'
ValueError: invalid literal for int() with base 10: 'python'
[Link] 261/338
13/11/2016 python Basic and Intermediate Level Modules
In [86]: #!/usr/bin/python
# [Link]
try:
age = input('please enter your age:')
print "my age is:", age
except NameError,error:
print "please enter your age in numeric"
except:
print 'someElse error'
print "‐‐‐‐‐ report ‐‐‐‐‐‐‐‐"
print "We got the following error : %s" %(error)
# identifier 'error' is being used outside of the try‐except blocks
please enter your age:30
my age is: 30
‐‐‐‐‐ report ‐‐‐‐‐‐‐‐
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐86‐0831f7fcbb6b> in <module>()
12
13 print "‐‐‐‐‐ report ‐‐‐‐‐‐‐‐"
‐‐‐> 14 print "We got the following error : %s" %(error)
15 # identifier 'error' is being used outside of the try‐exce
pt blocks
NameError: name 'error' is not defined
In [87]: #!/usr/bin/python
import sys
try:
value1 = input("Please enter the value1:")
value2 = input("Please enter the value2:")
except NameError,error:
print "Please enter only numbers !!!"
print "ERROR: %s" %(error)
Please enter the value1:12
Please enter the value2:python
Please enter only numbers !!!
ERROR: name 'python' is not defined
[Link] 262/338
13/11/2016 python Basic and Intermediate Level Modules
In [88]: #!/usr/bin/python
try:
a = input('please enter your number 1:')
b = input('please enter your number 2:')
except NameError,error:
print "please enter numbers only \n"
print "exception:", error
else:
print "You enter the right number a:%d b:%d" %(a,b)
#try:
# print "division of both numbers:" , a/b
#except ZeroDivisionError:
# print "we cannot divide by zero"
please enter your number 1:123
please enter your number 2:456
You enter the right number a:123 b:456
In [89]: #!/usr/bin/python
try:
num1 = int(raw_input("please enter a number1:"))
num2 = int(raw_input("please enter a number2:"))
except ValueError:
print "Please enter a number[0‐9]"
else:
print num1,num2
please enter a number1:python
Please enter a number[0‐9]
In [91]: #!/usr/bin/python
import sys
try:
value1 = input("Please enter the value1:")
value2 = input("Please enter the value2:")
except NameError:
print "Please enter only numbers !!!"
[Link](1)
except [Link], ex: # It handles all exceptions
print "ALL the errors are handled here"
print "The other error is ", ex
else:
print "The division of two numbers:" , value1/value2*1.0
Please enter the value1:124
Please enter the value2:345
The division of two numbers: 0.0
[Link] 263/338
13/11/2016 python Basic and Intermediate Level Modules
In [92]: #!/usr/bin/python
import sys
try:
value1 = input("Please enter the value1:")
value2 = input("Please enter the value2:")
except NameError:
print "Please enter only numbers !!!"
[Link](1)
except [Link], ex: # It handles all exceptions
print "ALL the errors are handled here"
print "The other error is ", ex
else:
print "The division of two numbers:" , value1/value2*1.0
Please enter the value1:123ADFEDF
ALL the errors are handled here
The other error is unexpected EOF while parsing (<string>, line 1)
In [93]: del value1, value2
In [94]: #!/usr/bin/python
try:
value1 = input("Please enter your first number: ")
value2 = input("please enter your second number: ")
except NameError:
print "please enter numbers only \n"
try:
print "division of two numbers is : " , float(value1)/value2
except (NameError,ZeroDivisionError): # handling these two execp
tions together
print " \n please enter a valid number"
Please enter your first number: 919
please enter your second number: 234
[Link] 264/338
13/11/2016 python Basic and Intermediate Level Modules
Multiple Exception Handling:
try:
<logic>
except TypeError, ex:
<logic to handle Type errors>
except IOError, ex:
<logic to handle IO errors>
except NameError, ex:
<logic to handle name errors>
except Exception, ex:
<logic to handle any other error>
Also, single handler can catch multiple exception types.
try:
<logic>
except (IOError, TypeError, NameError), ex:
<logic to handle IOError, TypeError, NameError>
In [95]: #!/usr/bin/python
try:
value1 = int(raw_input("please enter number 1:"))
value2 = int(raw_input("please enter number 2:"))
except ValueError:
print "Buddy.. its number \n"
try:
print "division of numbers", value1/value2
except ValueError,error:
print "Buddy .. no number given .. try again .. \n"
print "ERROR:{}".format(error)
except ZeroDivisionError,error:
print "Zero is not the number you would enter \n"
print "ERROR:{}".format(error)
except NameError,error:
print "value is not defined"
print "ERROR:{}".format(error)
else:
print "The division of numbers is success \n"
please enter number 1:12A
Buddy.. its number
division of numbers 3
The division of numbers is success
[Link] 265/338
13/11/2016 python Basic and Intermediate Level Modules
In [96]: #!/usr/bin/python
try:
value1 = int(raw_input("please enter number 1:"))
value2 = int(raw_input("please enter number 2:"))
except:
print "Buddy.. its number \n"
try:
print "division of numbers", value1/value2
except (ValueError,ZeroDivisionError, NameError),error:
print "Buddy .. it is either ValueError,ZeroDivisionError, NameError ..
try again .. \n"
print "ERROR:{}".format(error)
else:
print "The division of numbers is success \n"
please enter number 1:111
please enter number 2:0.0
Buddy.. its number
division of numbers 0
The division of numbers is success
Raising exceptions
raise instance
raise class
In [97]: raise
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐97‐26814ed17a01> in <module>()
‐‐‐‐> 1 raise
TypeError: exceptions must be old‐style classes or derived from BaseExceptio
n, not NoneType
In [98]: raise IOError
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
IOError Traceback (most recent call last)
<ipython‐input‐98‐f6cf32b12b43> in <module>()
‐‐‐‐> 1 raise IOError
IOError:
[Link] 266/338
13/11/2016 python Basic and Intermediate Level Modules
In [99]: raise KeyboardInterrupt
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
KeyboardInterrupt Traceback (most recent call last)
<ipython‐input‐99‐7d145351408f> in <module>()
‐‐‐‐> 1 raise KeyboardInterrupt
KeyboardInterrupt:
In [100]: raise MemoryError("The memory is running out of space")
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
MemoryError Traceback (most recent call last)
<ipython‐input‐100‐9c0834b76ffd> in <module>()
‐‐‐‐> 1 raise MemoryError("The memory is running out of space")
MemoryError: The memory is running out of space
In [101]: try:
a = int(input("Enter a positive integer: "))
if a <= 0:
raise ValueError("That is not a positive number!")
except ValueError as ve:
print(ve)
Enter a positive integer: ‐89
That is not a positive number!
In [103]: #!/usr/bin/python
size = int(raw_input("please enter the size:"))
if size < 50:
print "we have good amount of space"
if size > 50 and size < 80:
raise UserWarning,"We are hitting 80 percentage on disk"
if size > 90:
raise UserWarning,"we have a issue with 100 full space"
please enter the size:69
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
UserWarning Traceback (most recent call last)
<ipython‐input‐103‐ec1f0799c490> in <module>()
6 print "we have good amount of space"
7 if size > 50 and size < 80:
‐‐‐‐> 8 raise UserWarning,"We are hitting 80 percentage on disk"
9 if size > 90:
10 raise UserWarning,"we have a issue with 100 full space"
UserWarning: We are hitting 80 percentage on disk
Raising custom exceptions
[Link] 267/338
13/11/2016 python Basic and Intermediate Level Modules
In [104]: print 'Navya'
Navya
In [105]: 'Navya'
Out[105]: 'Navya'
In [106]: Navya
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐106‐82ddaec09024> in <module>()
‐‐‐‐> 1 Navya
NameError: name 'Navya' is not defined
In [107]: raise NameError("This is a name error") # python 2 and python 3
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐107‐b2d352285537> in <module>()
‐‐‐‐> 1 raise NameError("This is a name error") # python 2 and python 3
NameError: This is a name error
In [108]: raise NameError, "This is a name error" # only in python 2.x
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐108‐1da8dbca750e> in <module>()
‐‐‐‐> 1 raise NameError, "This is a name error" # only in python 2.x
NameError: This is a name error
exception need to be a class object
In [109]: class Naseer(Exception(" This is Naseer exception")):
pass
In [110]: raise Naseer
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Exception Traceback (most recent call last)
<ipython‐input‐110‐033e3c8c976f> in <module>()
‐‐‐‐> 1 raise Naseer
Exception: ('Naseer', (Exception(' This is Naseer exception',),), {'__module_
_': '__main__'})
In [111]: import exceptions
class Naseer([Link](" This is Naseer exception")):
pass
[Link] 268/338
13/11/2016 python Basic and Intermediate Level Modules
Defining custom exceptions
All builtin exceptions are defined in terms of classes.
To create a new exception, create a new calss definition that inherits from [Link]
In [112]: #Ex1:
class Networkerror(RuntimeError):
def __init__(self, arg):
[Link] = arg
try:
raise Networkerror("Bad hostname")
except Networkerror,e:
print [Link]
('B', 'a', 'd', ' ', 'h', 'o', 's', 't', 'n', 'a', 'm', 'e')
In [113]: #Ex2:
import exceptions
#Exception class
class NetworkError([Link]):
def __init__(self, errno, msg):
[Link] = (errno, msg)
[Link] = errno
[Link] = msg
# Raises an exception (multiple arguments)
def error2():
raise NetworkError(1, 'Host not found')
# Raises an exception (multiple arguments supplied as a tuple)
def error3():
raise NetworkError, (1, 'Host not found')
In [114]: error2() # function call
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NetworkError Traceback (most recent call last)
<ipython‐input‐114‐c024a2970039> in <module>()
‐‐‐‐> 1 error2() # function call
<ipython‐input‐113‐8077a043d45e> in error2()
10 # Raises an exception (multiple arguments)
11 def error2():
‐‐‐> 12 raise NetworkError(1, 'Host not found')
13
14 # Raises an exception (multiple arguments supplied as a tuple)
NetworkError: (1, 'Host not found')
[Link] 269/338
13/11/2016 python Basic and Intermediate Level Modules
Asserts
used to introduce debugging code into the program.
If the testlogic evaluates to False, assert raises an AssertionError exception with the optional data
supplied.
Syntax:
if not <some_test>:
raise AssertionError(<message>)
(or)
assert <some_test>, <message>
In [115]: # Ex1:
x = 5
y = 3
assert x<y, "x has to be smaller than y"
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AssertionError Traceback (most recent call last)
<ipython‐input‐115‐9bec1dfa8fff> in <module>()
2 x = 5
3 y = 3
‐‐‐‐> 4 assert x<y, "x has to be smaller than y"
AssertionError: x has to be smaller than y
In [116]: assert y,x
In [117]: assert 67>89
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AssertionError Traceback (most recent call last)
<ipython‐input‐117‐f937111075ea> in <module>()
‐‐‐‐> 1 assert 67>89
AssertionError:
Assertions are not checked when python runs in optimized mode (i.e., with o option).
python ‐o [Link]
[Link] 270/338
13/11/2016 python Basic and Intermediate Level Modules
Hint: assert should not be used to catch programming errors like x / 0, because Python traps such
programming errors itself!.
Assert should be used for trapping userdefined constraints!
Working with files
File operation modes
r read only
w write only
a appending the data
Note: If you open an existing file with 'w' mode, it's existing data get vanished.
r+ both for read and write
a+ both for read and append
In windows, the data is stored in binary format. Placing this 'b' doesn't effect in unix and linux.
rb read only
wb write only
ab append only
ab+ Both reading and appending data
Default file operation is read only.
Accessing a file
Taking a sample file, named [Link]
[Link]
Python programming is interesting
It is coming with batteries, in built
It means that almost every operation has a module !
[Link] 271/338
13/11/2016 python Basic and Intermediate Level Modules
In [118]: str1 = "Python programming is interesting\n\
It is coming with batteries, in built\n\
It means that almost every operation has a module !\n\
"
fileHandler = open('[Link]', 'wb')# creating a new file
[Link](str1)
[Link]()
In [119]: import os; [Link]([Link]())
Out[119]: ['.ipynb_checkpoints',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'[Link]',
'first',
'[Link]',
'[Link]',
'myNewFolder',
'newFolder',
'[Link]',
'[Link]',
'python ‐ Basic Level [Link]',
'python+‐+Basic+Level+[Link]',
'[Link]']
In [120]: f = open('[Link]', 'rb') # Opening an existing file for reading
data1 = [Link]() # reads one line
[Link]()
print type(data1), data1
<type 'str'> Python programming is interesting
In [122]: f = open('[Link]', 'rb')
data2 = [Link]() # reads all lines, but results list of each line, a
s a string
[Link]()
print type(data2), '\n',data2
<type 'list'>
['Python programming is interesting\n', 'It is coming with batteries, in buil
t\n', 'It means that almost every operation has a module !\n']
In [123]: f = open('[Link]', 'rb')
data3 = [Link]() # reads entire file as a single string
[Link]()
print type(data3), "\n", data3
<type 'str'>
Python programming is interesting
It is coming with batteries, in built
It means that almost every operation has a module !
[Link] 272/338
13/11/2016 python Basic and Intermediate Level Modules
Interview Question : what is the advantage of performing file operations with context manager
In [124]: with open('[Link]', 'rb') as f: # file operation with context manager
data4 = [Link]()
[Link]()
print type(data4), data4
<type 'str'> Python programming is interesting
It is coming with batteries, in built
It means that almost every operation has a module !
Interview Question : How to add content add the end of file (EOF) in a file?
In [126]: with open('[Link]', 'ab') as f:
[Link]("This is some line")
[Link]()
with open('[Link]', 'ab') as f:
[Link]("\n This is another line")
# Observe the importance of '\n' escape sequence character, here.
[Link]()
with open('[Link]', 'rb+') as g:
data5 = [Link]()
[Link]()
print type(data5), '\n', data5
<type 'str'>
Python programming is interesting
It is coming with batteries, in built
It means that almost every operation has a module !
This is some line
This is another lineThis is some line
This is another line
In [127]: with open('[Link]', 'wb') as f:
[Link]("\n This is another line")
[Link]()
with open('[Link]', 'rb+') as g:
data6 = [Link]()
[Link]()
print type(data6), "\n", data6
# Observe that existing data is deleted, as the file is opened in write mode
<type 'str'>
This is another line
[Link] 273/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 274/338
13/11/2016 python Basic and Intermediate Level Modules
In [128]: #!/usr/bin/python
# [Link]
'''
Purpose: File operations demonstration
'''
# accessing an exising file
f = open('[Link]', 'rb')
# 'f' is the file handler
print f, type(f)
# reading the data present in the [Link]
data = [Link]()
print "data = ", data
print 'Again trying to print the data from file'
data = [Link]()
print "data = ", data
print 'The current position of cursor in file is ', [Link]()
print 'moving the cursor position to 0'
[Link](0)
print 'The current position of cursor in file is ', [Link]()
print 'The first 12 characters in the file are ', [Link](12)
print 'The next 6 characters in the file are \n', [Link](6)
print 'The current line in file is \n', [Link]()
print 'The current line in file is \n', [Link]()
print 'checking whether file is closed or not'
print 'return True, if the file is closed', [Link]
[Link]()
print 'checking whether file is closed or not'
print 'return True, if the file is closed', [Link]
try:
[Link]() #No operation can be performed on a closed file object, as the ob
ject gets dereferenced
# garbage collector deletes all the unreferenced objects
except ValueError, ve:
print ve
print "IO operation can't be performed on closed file handler"
g = open('[Link]', 'wb') # opening existing file in read mode will erase i
ts existing data.
try:
datag = [Link]()
print "datag = ", datag
[Link] 275/338
13/11/2016 python Basic and Intermediate Level Modules
except IOError, ex:
print ex
print 'opened file in write mode. can not read the data'
[Link]('Python programming is interesting\n')
[Link]('It is coming with batteries, in built\n')
[Link]('It means that almost every operation has a module !')
[Link]() # it is not mandatory , but extremely recommended.
# python interpreter with close the file, but
# IronPython, Jython, ... may not close the file automatically.
# Using Context manager for file handling
with open('[Link]', 'ab+') as f:
print 'The cursor position is at %d'%([Link]())
dataf = [Link]()
print 'The cursor position is at %d' % ([Link]())
print 'The content of the data is \n', dataf
# append mode supports both read and write operations
print 'The cursor position is at %d' % ([Link]())
print [Link](123)
[Link]('This is last line')
print 'The cursor position is at %d' % ([Link]())
[Link]()
[Link] 276/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 277/338
13/11/2016 python Basic and Intermediate Level Modules
<open file '[Link]', mode 'rb' at 0x038AF758> <type 'file'>
data =
This is another line
Again trying to print the data from file
data =
The current position of cursor in file is 22
moving the cursor position to 0
The current position of cursor in file is 0
The first 12 characters in the file are
This is an
The next 6 characters in the file are
other
The current line in file is
line
The current line in file is
checking whether file is closed or not
return True, if the file is closed False
checking whether file is closed or not
return True, if the file is closed True
I/O operation on closed file
IO operation can't be performed on closed file handler
File not open for reading
opened file in write mode. can not read the data
The cursor position is at 0
The cursor position is at 123
The content of the data is
Python programming is interesting
It is coming with batteries, in built
It means that almost every operation has a module !
The cursor position is at 123
The cursor position is at 140
Assignment : Try to open the same file in two different modes, in parallel, and observe the problem occurring
Assignment : Write a script for camel casing to underscore casing, and vice versa; need to be done on an
existing .py file. Ensure that functions imported from modules, are not disturbed.
Working with CSV files
Taking a sample csv file
[Link]
fruits, vegetables, cars
Apple, Cabbagge, Benz
Mango, Cucumber, Volvo
Banana, Raddish, Maruthi suzuki
[Link] 278/338
13/11/2016 python Basic and Intermediate Level Modules
Creating a csv file
In [129]: with open('[Link]', 'ab+') as myCsv:
[Link]("fruits, vegetables, cars\n")
[Link]("Apple, Cabbagge, Benz\n")
[Link]("Mango, Cucumber, Volvo\n")
[Link]("Banana, Raddish, Maruthi suzuki\n")
[Link]()
In [130]: import os;
print [Link]([Link]())
['.ipynb_checkpoints', '[Link]', '[Link]', '[Link]',
'[Link]', '[Link]', 'first', '[Link]', '[Link]', 'myNe
wFolder', 'newFolder', '[Link]', '[Link]', 'python ‐ Basic Level
[Link]', 'python+‐+Basic+Level+[Link]', '[Link]', 'tes
[Link]']
In [131]: with open('[Link]', 'rb') as c:
data = [Link]()
[Link]()
print type(data), '\n', data
<type 'str'>
fruits, vegetables, cars
Apple, Cabbagge, Benz
Mango, Cucumber, Volvo
Banana, Raddish, Maruthi suzuki
In [132]: import csv
In [133]: print dir(csv)
['Dialect', 'DictReader', 'DictWriter', 'Error', 'QUOTE_ALL', 'QUOTE_MINIMA
L', 'QUOTE_NONE', 'QUOTE_NONNUMERIC', 'Sniffer', 'StringIO', '_Dialect', '__a
ll__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__v
ersion__', 'excel', 'excel_tab', 'field_size_limit', 'get_dialect', 'list_dia
lects', 're', 'reader', 'reduce', 'register_dialect', 'unregister_dialect',
'writer']
[Link] 279/338
13/11/2016 python Basic and Intermediate Level Modules
In [134]: #!/usr/bin/python
# [Link]
import csv
'''
Purpose : Working with CSV files
'''
# with is called as a context manager
with open('[Link]') as csvFile:
data = [Link](csvFile, delimiter = ',')
print data # it is an iterator object
for row in data:
#print row
print row[0]
[Link]()
<_csv.reader object at 0x038797F0>
fruits
Apple
Mango
Banana
[Link] 280/338
13/11/2016 python Basic and Intermediate Level Modules
In [135]: with open('[Link]') as csvFile:
readCSV = [Link](csvFile, delimiter=',') # delimiter
print readCSV
print type(readCSV)
for row in readCSV:
print "row ‐‐> ", row
print "row[0] ‐‐> ",row[0]
print "row[0], row[1] ‐‐> ",row[0], row[1]
print '‐'*70
[Link]()
# It is recommended to close the file, after it's use.
# It deletes that file object.
# so, file handler can't be used, after its closure.
<_csv.reader object at 0x038799F0>
<type '_csv.reader'>
row ‐‐> ['fruits', ' vegetables', ' cars']
row[0] ‐‐> fruits
row[0], row[1] ‐‐> fruits vegetables
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
row ‐‐> ['Apple', ' Cabbagge', ' Benz']
row[0] ‐‐> Apple
row[0], row[1] ‐‐> Apple Cabbagge
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
row ‐‐> ['Mango', ' Cucumber', ' Volvo']
row[0] ‐‐> Mango
row[0], row[1] ‐‐> Mango Cucumber
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
row ‐‐> ['Banana', ' Raddish', ' Maruthi suzuki']
row[0] ‐‐> Banana
row[0], row[1] ‐‐> Banana Raddish
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Assignment : write a function to display all the cars, in the [Link]
[Link] 281/338
13/11/2016 python Basic and Intermediate Level Modules
In [136]: with open('[Link]') as csvFile1:
data = [Link](csvFile1, delimiter=',')
print '\n',data
for row in data:
#cars = row[2]
(fruits, vegetables, cars) = row # tuple‐ unpacking
print fruits, vegetables, cars
[Link]()
print "outside the loop"
print fruits, vegetables, cars
<_csv.reader object at 0x03879AB0>
fruits vegetables cars
Apple Cabbagge Benz
Mango Cucumber Volvo
Banana Raddish Maruthi suzuki
outside the loop
Banana Raddish Maruthi suzuki
[Link] 282/338
13/11/2016 python Basic and Intermediate Level Modules
In [137]: with open('[Link]') as csvFile1:
data = [Link](csvFile1, delimiter=',')
print '\n',data
#vegetables = fruits = cars = []
#It will result in all these objects will point to the same location
vegetables = []
fruits = []
cars = []
#for i in dir(data):
# print i,
for index, row in enumerate(data):
if index == 0:
continue # to skip first iteration
[Link](index,row[0])
#print fruits
[Link](index,row[1])
[Link](index,row[2])
#print row, type(row)
#print row[0], type(row[0])
#[Link](row[0])
#print [Link](row[0])
#print [Link](row[1])
#print [Link](row[2])
print '‐'*50
print 'fruits :',fruits
print 'vegetables', vegetables
print 'cars : ', cars
print id(fruits), id(vegetables), id(cars)
[Link]()
<_csv.reader object at 0x03879B30>
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
fruits : ['Apple', 'Mango', 'Banana']
vegetables [' Cabbagge', ' Cucumber', ' Raddish']
cars : [' Benz', ' Volvo', ' Maruthi suzuki']
58103848 59318800 59321080
In [138]: import csv
with open('[Link]', 'w') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = [Link](csvfile, fieldnames=fieldnames)
[Link]()
[Link]({'first_name': 'Baked', 'last_name': 'Beans'})
[Link]({'first_name': 'Lovely', 'last_name': 'Spam'})
[Link]({'first_name': 'Wonderful', 'last_name': 'Spam'})
[Link] 283/338
13/11/2016 python Basic and Intermediate Level Modules
In [139]: import csv
inputNames = [Link](open("[Link]"))
for name in inputNames:
print name
{'first_name': 'Baked', 'last_name': 'Beans'}
{'first_name': 'Lovely', 'last_name': 'Spam'}
{'first_name': 'Wonderful', 'last_name': 'Spam'}
Working with XML
In [140]: from lxml import etree
# create XML
root = [Link]('root')
[Link]([Link]('child'))
# another child with text
child = [Link]('child')
[Link] = 'some text'
[Link](child)
# pretty string
s = [Link](root, pretty_print=True)
print s
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ImportError Traceback (most recent call last)
<ipython‐input‐140‐507ddfa1579b> in <module>()
‐‐‐‐> 1 from lxml import etree
2
3 # create XML
4 root = [Link]('root')
5 [Link]([Link]('child'))
ImportError: No module named lxml
In [146]: from [Link] import Element, SubElement, tostring
root = Element('root')
child = SubElement(root, "child")
[Link] = "I am a child"
print tostring(root)
<root><child>I am a child</child></root>
Assignment : work with xml and xls files
[Link] 284/338
13/11/2016 python Basic and Intermediate Level Modules
Byte array
In [150]: sentenceA = "Tomarrow is ours!!!"
print type(sentenceA)
<type 'str'>
In [151]: sentenceB = bytearray("Tomarrow is ours!!!")
print type(sentenceB)
<type 'bytearray'>
NOTE: string objects are immutable, whereas bytearray objects are mutable
In [152]: sentenceA[9:11]
Out[152]: 'is'
In [153]: sentenceA[9:11] = "will be"
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
TypeError Traceback (most recent call last)
<ipython‐input‐153‐e72736eb4fc1> in <module>()
‐‐‐‐> 1 sentenceA[9:11] = "will be"
TypeError: 'str' object does not support item assignment
In [154]: sentenceB[9:11]
Out[154]: bytearray(b'is')
In [155]: sentenceB[9:11] = "will be"
In [156]: print sentenceB
Tomarrow will be ours!!!
In [157]: print sentenceB[1], sentenceA[1]
111 o
In [158]: chr(111)
Out[158]: 'o'
In [159]: ord('o')
Out[159]: 111
[Link] 285/338
13/11/2016 python Basic and Intermediate Level Modules
NOTE: bytearray will store the characters with their corresponding ASCII values
Data Serialization
‐ converting the objects into byte form .
‐ Used for transferring the objects.
‐ Serialization is the process of converting a data structure or object state into
a format that can be stored.
‐ Serialization is also called deflating or marshalling
‐ DeSerialization is also called Inflating or unmarshalling.
Various ways of data serialization
‐ Marshall ‐‐ It is primitive, and no more used.
‐ Pickle ‐‐ Pickle is a standard module which serializes and deserializes a pytho
n object structure.
‐ cPickle ‐‐ c implementation of pickle]
‐ shelve ‐‐ Advanced version, to address the security flaws of pickel/cpickle
‐ xml
‐ json ‐‐ Most popular, now‐a‐days
‐ db
Python 2.x has both pickle and cpickle. Whereas python 3.x has only cPickle, and it is renamed as pickle.
Pickle files has .pkl or .pickle extensions. The pickled data format is python specific.
Interview Question : Difference between compression and serialization ?
Ans: compression may be lossy or lossless process. Whereas Serialization is a lossless reversible process.
compression is used to reduce the data redundancy, whereas serialization is used for inflating or deflating an
object, and communicating data with other languages.
[Link] 286/338
13/11/2016 python Basic and Intermediate Level Modules
In [160]: #!/usr/bin/python
# [Link]
import pickle
'''
Purpose: Working with Pickle files
pickling
'''
# Serialization
students = ['Mujeeb', 'Harini', 'Mamatha', 'Ankit', 'Naseer','Shoban', 123]
f = open('[Link]', 'ab+')
[Link](students, f)
[Link]()
[Link]()
# Deserialization
g = open('[Link]', 'rb')
myStudents = [Link](g)
print "myStudents are ", myStudents
[Link]()
myStudents are ['Mujeeb', 'Harini', 'Mamatha', 'Ankit', 'Naseer', 'Shoban',
123]
Interview Question : what is the difference between dump and dumps methods
In [161]: # serialization
mystud = [Link](students) # dumping into a string
print mystud
print type(mystud)
(lp0
S'Mujeeb'
p1
aS'Harini'
p2
aS'Mamatha'
p3
aS'Ankit'
p4
aS'Naseer'
p5
aS'Shoban'
p6
aI123
a.
<type 'str'>
[Link] 287/338
13/11/2016 python Basic and Intermediate Level Modules
In [162]: # Deserialization
orgStud = [Link](mystud) # loading from a string
print orgStud
print type(orgStud)
print orgStud == students
['Mujeeb', 'Harini', 'Mamatha', 'Ankit', 'Naseer', 'Shoban', 123]
<type 'list'>
True
In [163]: with open('[Link]', 'rb+') as ktf:
myStudents = [Link](ktf)
print "myStudents are ", myStudents
myStudents are ['Mujeeb', 'Harini', 'Mamatha', 'Ankit', 'Naseer', 'Shoban',
123]
In conclusion, Pickle and cpickle has their importance in interfacing with c and C++. As pickled data format is
python specific, it is not used in interfacing with other languages.
In [164]: def func():
pass
z = func()
print z, type(z)
print func, type(func)
None <type 'NoneType'>
<function func at 0x03912730> <type 'function'>
In [165]: try:
zz = [Link](z)
except [Link] as pe:
print 'The error is ', pe
In [166]: print zz
N.
Interview Question : When a function object is deleted, but the object created with that function call is
retained, will it malfunction, or work properly?
In [167]: del func
print zz
N.
[Link] 288/338
13/11/2016 python Basic and Intermediate Level Modules
In [168]: try:
zz1 = [Link](z)
except [Link] as pe:
print 'The error is ', pe
In [169]: print zz1
N.
In [170]: class class1():
pass
b = class1()
[Link](b)
del class1
print b
<__main__.class1 instance at 0x038FF350>
Though class is deleted, its instance survivies; same as the case with functions
In [171]: [Link]('I am pickling')
Out[171]: "S'I am pickling'\np0\n."
In [172]: [Link]("S'I am pickling'\np0\n.")
Out[172]: 'I am pickling'
In [173]: [Link]([Link]('I am pickling'))
Out[173]: 'I am pickling'
Limitations of Pickle
The pickle module is not secure against erroneous or maliciously constructed data.
The pickled data can be modfied onthefly, mainly by Middleman attack.
Never unpickle data received from an untrusted or unauthenticated source.
Shelve
shelve is a tool that uses pickle to store python objects in an accessbykey file system. It is same as
keys in dictionary.
It is used as a simple persistent storage option for python objects when a relational database is
overkill.
The values are pickled and written to a database created and managed by anydbm.
anydbm is a frontend interface to establish communication with database.
[Link] 289/338
13/11/2016 python Basic and Intermediate Level Modules
In [174]: import shelve
In [175]: s= [Link]('[Link]')
try:
s['key1'] = {'int': 8, 'float': 8.0, 'string': '8'}
except Exception, ex:
print ex
finally:
[Link]()
In [176]: # To access the data again,
s = [Link]('[Link]') # default is read only mode
try:
myShelveContent = s['key1'] # accessing using the key
except Exception, ex1:
print ex1
finally:
[Link]()
print 'myShelveContent = ', myShelveContent
myShelveContent = {'int': 8, 'float': 8.0, 'string': '8'}
opening shelve in readonly mode.
In [177]: s = [Link]('[Link]', flag = 'r')
try:
myShelveContent = s['key1'] # accessing using the key
except Exception, ex1:
print ex1
finally:
[Link]()
print 'myShelveContent = ', myShelveContent
myShelveContent = {'int': 8, 'float': 8.0, 'string': '8'}
[Link] 290/338
13/11/2016 python Basic and Intermediate Level Modules
In [178]: s = [Link]('[Link]', flag = 'r')
try:
print s['key1'] # accessing using the key
s['key1']['newValue'] = 'This is a new value'
# trying to write data in read mode; observe that this line has no reflect
ion
print s['key1']
except Exception, ex1:
print ex1
finally:
[Link]()
s = [Link]('[Link]', flag = 'r')
try:
print s['key1'] # accessing using the key
except Exception, ex1:
print ex1
finally:
[Link]()
{'int': 8, 'float': 8.0, 'string': '8'}
{'int': 8, 'float': 8.0, 'string': '8'}
{'int': 8, 'float': 8.0, 'string': '8'}
NOTE: By defaults, shelve do not track modifications to volatile objects. If necessary, open the shelve file in
writeback enabled
In [179]: s = [Link]('[Link]', writeback=True)
try:
print s['key1'] # accessing using the key
s['key1']['newValue'] = 'This is a new value'
print s['key1']
except Exception, ex1:
print ex1
finally:
[Link]()
s = [Link]('[Link]', flag = 'r')
try:
print s['key1'] # accessing using the key
except Exception, ex1:
print ex1
finally:
[Link]()
{'int': 8, 'float': 8.0, 'string': '8'}
{'int': 8, 'float': 8.0, 'string': '8', 'newValue': 'This is a new value'}
{'int': 8, 'float': 8.0, 'string': '8', 'newValue': 'This is a new value'}
Note: Shelve must not be opened with writeback enabled, unless it is essential
[Link] 291/338
13/11/2016 python Basic and Intermediate Level Modules
JSON
JSON is an abbreviation for Java Script Object notation
json is supported by almost all languages.
official website is [Link]
stored in .json file. But, it can be stored in other format too, like .template file in AWS cloudformation,
etc.
sample json
{
"employees": {
"employee": [
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"photo": "[Link]
he/story_header/photos/tom‐cruise‐[Link]"
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"photo": "[Link]
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"photo": "[Link]
50_ge_pierece_brosnan.jpg"
}
]
}
}
In [180]: import json
In [181]: students = {'batch 1' : {'Name': 'Ankit', 'regNumber': 1},
'batch 2' : {'Name': 'Mujeeb', 'regNumber': 2}
}
[Link] 292/338
13/11/2016 python Basic and Intermediate Level Modules
In [182]: print [Link](students) # dictionary to json
{"batch 2": {"regNumber": 2, "Name": "Mujeeb"}, "batch 1": {"regNumber": 1,
"Name": "Ankit"}}
In [183]: print [Link](students, sort_keys = True)
{"batch 1": {"Name": "Ankit", "regNumber": 1}, "batch 2": {"Name": "Mujeeb",
"regNumber": 2}}
tuple to json array
In [184]: tup1 = ('Red', 'Black', 'white', True, None, [])
[Link](tup1)
Out[184]: '["Red", "Black", "white", true, null, []]'
Observe that True is changed to true; and None to null
In [185]: string = 'This is a string'
[Link](string)
Out[185]: '"This is a string"'
In [186]: b = True
[Link](b)
Out[186]: 'true'
In [187]: a = ‐123
b = ‐2.34
z = 1.3e‐10
In [188]: [Link](a)
Out[188]: '‐123'
In [189]: [Link](b)
Out[189]: '‐2.34'
In [190]: [Link](z)
Out[190]: '1.3e‐10'
[Link] 293/338
13/11/2016 python Basic and Intermediate Level Modules
Interview Question : How different is python dictionary, from that of json?
json is different from dictionary.
‐ All the content within the dictionary must be in key‐value pairs; whereas there c
an be arrays in dictionary.
‐ And, json data types are different from that of python data types.
json doesn't cover all the datatypes of python, mainly tuples and bytes.
Decoding the json data, back to python data types can be achieved using [Link] correspondingly.
Assignment : Use [Link] to correspondingly deserialize it
json to python object conversion pairs:
JSON Python
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
object dict
array list
string str
number(int) int
number(real) float
true True
false False
null None
There are various online json lints to validate a written json file; and online json to other format convertors, like
code Beautify ([Link]
etc
Assignment : Go through this Yahoo weblink ([Link]
an insight on accessing web content using python.
In [191]: import urllib
url = '[Link]
u = [Link](url)
# u is a file‐like object
data = [Link]()
f = open('[Link]', 'ab+')
[Link](data)
[Link]()
[Link] 294/338
13/11/2016 python Basic and Intermediate Level Modules
NOTE: Both urllib and urllib serve the same purpose; But, urllib2 module can handle HTTP Errors better than
urllib
In [192]: import urllib2
try:
data = [Link](url).read()
except [Link], e:
print "HTTP error: %d" % [Link]
except [Link], e:
print "Network error: %s" % [Link][1]
with open('[Link]', 'ab+') as g:
[Link](data)
[Link]()
webservices: urllib, urllib2, BeautifulSoup (bs4), httplib, cookielib
Debugger in Python
Debugging is essential to understand the call flow in run time. It is also used to debug an error.
There are various debuggers available in python, as listed here
([Link]
1. Interactive mode, or
2. within the IDE
In all the cases, it is not feasible to work with an IDE. ex: Devops jobs of logging into a remote linux/Windows
server, to solve an issue. Especially, in such cases, interactive debuggers such as pdb, ipdb, ... can be helpful.
pydev
Majority of the IDEs will have pydev configured in it.
Basic features of pydev are
step Over
step In
step Out
Placing debugger
Visualizing the objects in debug mode ....
[Link] 295/338
13/11/2016 python Basic and Intermediate Level Modules
pdb
It is the basic interactive Debugger
Features
pause a program
look at the values of variables
watch program execution stepbystep
Usage:
1. In the terminal :
$ python m pdb [Link]
2. Within the script using pdb module
import pdb
In the script, place the following statement from where we want to make debugging.
pdb.set_trace()
Now, run the program normally
At the output console, it returns pdb prompt, and wait for the commands.
(pdb) l # To list the complete script, irrespective of the line in which
the set_trace() statement is placed.
(pdb) l 1,5 # To list 1st line to 5th line (including) irrespective of place i
n which set_trace() statement is placed.
(pdb) c # Continue execution till a break point is encountered
(pdb) help # To get help find all the options possible with pdb
(pdb) n # Continue execution until the next line in current function is rea
ched or it returns
(pdb) r # continue execution, until the current function returns
(pdb) u # shows the flow. Shows previous step in execution flow. but, it wi
ll not execute
(pdb) d # shows next step in execution flow.
[Link] 296/338
13/11/2016 python Basic and Intermediate Level Modules
Most commonly used among them are:
l(ist)
n(ext)
c(ontinue)
s(tep)
r(eturn)
b(reak)
pdb ? ‐ to get help
pdb l ‐ show the cursor position
pdb l 18 ‐ to list line 18 in file
pdb passesLeft ‐ to get the number of passes left
pdb <any variable> ‐ to get the value in variable
pdb b 18 ‐ to place a breakpoint
pdb l
pdb n ‐ to execute next step
pdb comes with four modes of operation:
‐ Script, postmortem, run and Trace modes
Working with pdb, within the python scripts
save the below scripts, and execute them in terminal, like general python scripts.
[Link] 297/338
13/11/2016 python Basic and Intermediate Level Modules
In [193]: #!/usr/bin/python
# [Link]
import pdb
version = '3.0'
def hello():
''' this is just for printing hello '''
print "hello today is modules class"
def add(a=1,b=2):
''' this is an addition program '''
sum = a + b
return sum
pdb.set_trace()
if __name__ == '__main__':
hello()
sum = add()
sum1 = add(5,6)
print sum1
print sum
print version
else:
print "Hello this has to be part of my modules"
‐‐Return‐‐
> <ipython‐input‐193‐ca0a9d73001e>(14)<module>()‐>None
‐> pdb.set_trace()
(Pdb) l
9 ''' this is an addition program '''
10 sum = a + b
11 return sum
12
13
14 ‐> pdb.set_trace()
15 if __name__ == '__main__':
16 hello()
17 sum = add()
18 sum1 = add(5,6)
19 print sum1
(Pdb) l 9,15
9 ''' this is an addition program '''
10 sum = a + b
11 return sum
12
13
14 ‐> pdb.set_trace()
15 if __name__ == '__main__':
(Pdb) n
> c:\python27\lib\site‐packages\ipython\core\[Link](2884)run_cod
e()
‐> [Link] = old_excepthook
(Pdb) c
hello today is modules class
11
3
3.0
[Link] 298/338
13/11/2016 python Basic and Intermediate Level Modules
In [194]: #!/usr/bin/python
# [Link]
import pdb
pdb.set_trace()
import first as f
print [Link]()
print [Link](10,20)
print [Link]
[Link] 299/338
13/11/2016 python Basic and Intermediate Level Modules
‐‐Return‐‐
> <ipython‐input‐194‐6c3a48b9f2b1>(4)<module>()‐>None
‐> pdb.set_trace()
(Pdb) help
Documented commands (type help <topic>):
========================================
EOF bt cont enable jump pp run unt
a c continue exit l q s until
alias cl d h list quit step up
args clear debug help n r tbreak w
b commands disable ignore next restart u whatis
break condition down j p return unalias where
Miscellaneous help topics:
==========================
exec pdb
Undocumented commands:
======================
retval rv
(Pdb) ?
Documented commands (type help <topic>):
========================================
EOF bt cont enable jump pp run unt
a c continue exit l q s until
alias cl d h list quit step up
args clear debug help n r tbreak w
b commands disable ignore next restart u whatis
break condition down j p return unalias where
Miscellaneous help topics:
==========================
exec pdb
Undocumented commands:
======================
retval rv
(Pdb) b 6
Breakpoint 1 at <ipython‐input‐194‐6c3a48b9f2b1>:6
(Pdb) l
1 #!/usr/bin/python
2 # [Link]
3 import pdb
4 ‐> pdb.set_trace()
5 import first as f
6 B print [Link]()
7 print [Link](10,20)
8 print [Link]
[EOF]
(Pdb) b 7
Breakpoint 2 at <ipython‐input‐194‐6c3a48b9f2b1>:7
(Pdb) c
[Link] 300/338
13/11/2016 python Basic and Intermediate Level Modules
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
ImportError Traceback (most recent call last)
<ipython‐input‐194‐6c3a48b9f2b1> in <module>()
3 import pdb
4 pdb.set_trace()
‐‐‐‐> 5 import first as f
6 print [Link]()
7 print [Link](10,20)
ImportError: No module named first
Note: You can observe some code which is getting executed here, though it is not part of our [Link]. It is
beacuse of ipython.
Also, with this debugging, we clearly identified that '[Link]' is not located in the current working directory.
Now, after placing [Link] file in 'debugging' folder, It got executed.
In [196]: #!/usr/bin/python
# [Link]
import pdb
pdb.set_trace()
import [Link] as f
print [Link]()
print [Link](10,20)
print [Link]
‐‐Return‐‐
> <ipython‐input‐196‐977263d8e280>(4)<module>()‐>None
‐> pdb.set_trace()
(Pdb) c
> c:\users\home\google drive\python\tut\complete material\debugging\[Link]
(15)<module>()
‐> if __name__ == '__main__':
(Pdb) c
Hello this has to be part of my modules
hello today is modules class
None
30
3.0
[Link] 301/338
13/11/2016 python Basic and Intermediate Level Modules
In [1]: #!/usr/bin/python
# [Link]
import pdb
for i in (1,2,3):
pdb.set_trace()
print i
[Link] 302/338
13/11/2016 python Basic and Intermediate Level Modules
> <ipython‐input‐1‐0bad0586cb8f>(7)<module>()
‐> print i
(Pdb) n
1
> <ipython‐input‐1‐0bad0586cb8f>(5)<module>()
‐> for i in (1,2,3):
(Pdb) l
1 #!/usr/bin/python
2 # [Link]
3 import pdb
4
5 ‐> for i in (1,2,3):
6 pdb.set_trace()
7 print i
[EOF]
(Pdb) n
> <ipython‐input‐1‐0bad0586cb8f>(6)<module>()
‐> pdb.set_trace()
(Pdb) l
1 #!/usr/bin/python
2 # [Link]
3 import pdb
4
5 for i in (1,2,3):
6 ‐> pdb.set_trace()
7 print i
[EOF]
(Pdb) s
‐‐Call‐‐
> c:\python27\lib\[Link](1250)set_trace()
‐> def set_trace():
(Pdb) s
> c:\python27\lib\[Link](1251)set_trace()
‐> Pdb().set_trace(sys._getframe().f_back)
(Pdb) s
‐‐Call‐‐
> c:\python27\lib\[Link](61)__init__()
‐> def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None):
(Pdb) r
‐‐Return‐‐
> c:\python27\lib\[Link](104)__init__()‐>None
‐> self.commands_bnum = None # The breakpoint number for which we are
(Pdb) r
‐‐Return‐‐
> c:\python27\lib\[Link](1251)set_trace()‐>None
‐> Pdb().set_trace(sys._getframe().f_back)
(Pdb) r
> <ipython‐input‐1‐0bad0586cb8f>(7)<module>()
‐> print i
(Pdb) c
2
> <ipython‐input‐1‐0bad0586cb8f>(6)<module>()
‐> pdb.set_trace()
(Pdb) c
3
[Link] 303/338
13/11/2016 python Basic and Intermediate Level Modules
Similarly, in [Link], you can observe the functionality of for loop and if condition, in runtime.
[Link] 304/338
13/11/2016 python Basic and Intermediate Level Modules
In [2]: #!/usr/bin/python
# [Link]
import pdb
for i in range(1,11):
if i == 5:
pdb.set_trace()
continue
print i
[Link] 305/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 306/338
13/11/2016 python Basic and Intermediate Level Modules
1
2
3
4
> <ipython‐input‐2‐f1378680eab5>(8)<module>()
‐> continue
(Pdb) l
3 import pdb
4
5 for i in range(1,11):
6 if i == 5:
7 pdb.set_trace()
8 ‐> continue
9 print i
[EOF]
(Pdb) b 5,6,9
Breakpoint 1 at <ipython‐input‐2‐f1378680eab5>:5
(Pdb) l
[EOF]
(Pdb) n
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
(Pdb) l
1 #!/usr/bin/python
2 # [Link]
3 import pdb
4
5 B‐> for i in range(1,11):
6 if i == 5:
7 pdb.set_trace()
8 continue
9 print i
[EOF]
(Pdb) d 5
*** Newest frame
(Pdb) l 1,9
1 #!/usr/bin/python
2 # [Link]
3 import pdb
4
5 B‐> for i in range(1,11):
6 if i == 5:
7 pdb.set_trace()
8 continue
9 print i
(Pdb) c
6
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
(Pdb) c
7
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
(Pdb) c
8
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
[Link] 307/338
13/11/2016 python Basic and Intermediate Level Modules
(Pdb) r
9
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
(Pdb) r
10
> <ipython‐input‐2‐f1378680eab5>(5)<module>()
‐> for i in range(1,11):
(Pdb) c
Now, let try to debug a script with some bugs.
Assignment : Try to find the dug, in this [Link] script.
#!/usr/bin/python
# [Link]
def eo(num):
''' fun for even odd numbers '''
if num % 2 == 0:
return 'even'
else:
return 'odd'
import pdb;pdb.set_trace()
print eo(2)
print eo(3)
print eo.func_doc
[Link] 308/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment 6: Analyze the script execution in this [Link]
#!/usr/bin/python
# [Link]
def eo(num):
''' fun for even odd numbers '''
if num % 2 == 0:
print 'even'
else:
print 'odd'
import pdb
pdb.set_trace()
for i in range(100):
print "The number1 is %s" %(i)
print "The number2 is %s" %(i)
print "The number3 is %s" %(i)
print "The number4 is %s" %(i)
print "The number5 is %s" %(i)
eo(i)
Assignment 7: In this [Link], Observe the call flow of function calls.
#!/usr/bin/python
# [Link]
import pdb
def fourth():
second()
print "this is my fourth function \n"
def third():
print "this is my third function \n"
def second():
third()
print "this is my second function \n"
def first():
fourth()
print "this is my first function \n"
pdb.set_trace()
first()
[Link] 309/338
13/11/2016 python Basic and Intermediate Level Modules
In conclusion, pdb debugging starts from the line where the pdb.set_trace() is placed. It can be more helpful,
in cases where using IDE or other GUI based debuggers is not possible. But, there are other debuggers such
as pydev, ... which have much more functionality than pdb.
re module
Regular expression (or regex) are used for pattern matching in the date. It is available in almost all
programming languages. Python has re module to deal with regular expressions.
In [3]: import re
In [4]: print dir(re)
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S',
'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__
all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__
version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_rep
l', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'co
py_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'searc
h', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
In [5]: regObject = [Link]("python") # creating a regular expression Object, r
egObject
In [6]: print regObject, type(regObject)
<_sre.SRE_Pattern object at 0x038802E0> <type '_sre.SRE_Pattern'>
[Link]()
[Link](string[, pos[, endpos]])
Matches zero or more characters at the beginning of the string
In [7]: [Link]("python Programming is Good") # The matched result is store
d in re type object
Out[7]: <_sre.SRE_Match at 0x38edb80>
In [8]: print [Link]("python Programming is Good")
<_sre.SRE_Match object at 0x038EDF00>
In [9]: print [Link]("python Programming is Good").group() # returns the matc
hes stored in object
python
[Link] 310/338
13/11/2016 python Basic and Intermediate Level Modules
In [10]: print [Link]("Programming python is Good")
None
In [11]: print [Link]("Programming python is Good").group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐11‐bba0226c0a4e> in <module>()
‐‐‐‐> 1 print [Link]("Programming python is Good").group()
AttributeError: 'NoneType' object has no attribute 'group'
Until and unless there are matches found, the group() attribute will not be present.
In [12]: print [Link](" python Programming is Good") # white‐space in startin
g
None
In [13]: print [Link]("Python Programming is Good") # capital 'P' encounter
ed
None
In [14]: print [Link]('pythoN') # results in None
None
In [15]: print [Link]('pythoN'.lower())
<_sre.SRE_Match object at 0x0396A1E0>
In [16]: #!/usr/bin/python
import os
checkString = 'python'
compiledCheckString = [Link](checkString) # re object will be created
targetString = 'python programming'
matchs = [Link](targetString) # match() will be initiate
d on the re object
print matchs
if matchs:
print "Now lets print the matches word"
print [Link]()
<_sre.SRE_Match object at 0x0396A250>
Now lets print the matches word
python
[Link] 311/338
13/11/2016 python Basic and Intermediate Level Modules
In [17]: #!/usr/bin/python
import os
checkString = 'python'
compiledCheckString = [Link](checkString) # re object will be created
targetString = 'Python'
matchs = [Link](targetString) # match() will be initiate
d on the re object
print matchs
if matchs:
print "Now lets print the matches word"
print [Link]()
else:
print "No matches found"
None
No matches found
Interview Question : What is the difference between [Link]() and [Link]()?
[Link]()
[Link](string[, pos[, endpos]])
Matches zero or more characters anywhere in the string
In [18]: regObject = [Link]("python")
In [19]: [Link]("python programming")
Out[19]: <_sre.SRE_Match at 0x396a250>
In [20]: [Link]("python programming")
Out[20]: <_sre.SRE_Match at 0x396a3d8>
In [21]: [Link]("programming python is good")
Out[21]: <_sre.SRE_Match at 0x396a448>
In [22]: print [Link]("programming python is good") # match tries to match in
the starting only
None
In [23]: [Link]("programming is good in python")
Out[23]: <_sre.SRE_Match at 0x396a528>
[Link] 312/338
13/11/2016 python Basic and Intermediate Level Modules
In [24]: regObject = [Link]("python is")
print [Link]("programming python is good")
print [Link]("programming python is good")
None
<_sre.SRE_Match object at 0x0396A1E0>
In [25]: print [Link]("is programming python in good ?")
None
Special Characters
^ (caret) Matches the start of the string
In [26]: string = "This is python class"
regObject = [Link]("^This")
In [27]: result = [Link](string)
In [28]: print [Link]()
This
Doing in other way
In [29]: [Link]('^This',string)
Out[29]: <_sre.SRE_Match at 0x396a800>
In [30]: [Link]('^This',string).group()
Out[30]: 'This'
In [31]: [Link]('^This',"This is python class").group()
Out[31]: 'This'
In [32]: [Link]('^This',"This is python class").group()
Out[32]: 'This'
In [33]: print [Link]('^This', "Yes!, This is python class")
None
$ matches the end of the string, or just before the newline at the end of the string
[Link] 313/338
13/11/2016 python Basic and Intermediate Level Modules
In [34]: #!/usr/bin/python
import re
string = 'foo foobar'
print string
regObject= [Link]('foobar$')
print "regObject = ",regObject
print "[Link](string) ", [Link](string) # [Link]() vs r
[Link]()
print "[Link](string) ", [Link](string)
foo foobar
regObject = <_sre.SRE_Pattern object at 0x038245E0>
[Link](string) None
[Link](string) <_sre.SRE_Match object at 0x0396A8A8>
In [35]: print [Link]('foobar$',string) # pattern match only in the starting of line
None
In [36]: print [Link]('foobar$',string)
<_sre.SRE_Match object at 0x0396A9C0>
In [37]: print [Link]('foobar$',string).group()
foobar
. (DOT)
matches any character, except the newline. if DOTALL flag is enabled, it matches any character
including newline
In [38]: #!usr/bin/python
string = 'This'
result = [Link]('....', string)
if result:
print [Link]()
This
In [39]: print [Link]('......', "Shoban").group()
Shoban
In [40]: print [Link]('....', "Shoban").group()
Shob
In [41]: print [Link]('', "Shoban").group()
[Link] 314/338
13/11/2016 python Basic and Intermediate Level Modules
In [42]: print [Link]('....$', "Shoban").group()
oban
In [43]: print [Link]('^....', "Shoban").group()
Shob
In [44]: print [Link]('^....$', "Shoban").group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐44‐42e3a946b9a3> in <module>()
‐‐‐‐> 1 print [Link]('^....$', "Shoban").group()
AttributeError: 'NoneType' object has no attribute 'group'
In [45]: print [Link]('^......$', "Shoban").group()
Shoban
* causes the RE to match 0 or more repetitions of the preceeding RE.
ex: 'ab*' ‐ matches for 'a', 'ab', and 'a' followed by any number of 'b''s
ab, abb, abbbb ...
In [46]: print string
This
In [47]: [Link]('.*',string).group()
Out[47]: 'This'
In [48]: [Link]('.*', "I am aspiring to be a promising Engineer").group()
Out[48]: 'I am aspiring to be a promising Engineer'
In [49]: [Link]('.a*', "I am aspiring to be a promising Engineer").group()
Out[49]: 'I'
In [50]: [Link]('a*', "I am aspiring to be a promising Engineer").group()
Out[50]: ''
In [51]: [Link]('. *', "I am aspiring to be a promising Engineer").group()
Out[51]: 'I '
[Link] 315/338
13/11/2016 python Basic and Intermediate Level Modules
In [52]: [Link]('.g*', "I am aspiring to be a promising Engineer").group() #'g' is
occuring 0 times
Out[52]: 'I'
In [55]: [Link]('.*', '').group() # trying to find in null string # resulted in
white‐space, not NONE
Out[55]: ''
In [56]: [Link]('', '').group() # trying to find in null string # resulted in
white‐space, not NONE
Out[56]: ''
causes the RE to match 1 or more repetitions of the preceeding RE
ex: ab+ > 'ab', 'abb', 'abbb', ...
In [57]: string = 'abbba'
[Link]('ab+a',string).group()
Out[57]: 'abbba'
In [58]: print [Link]('ab+a','aa')
None
In [59]: print [Link]('ab*a','aa123').group() # * tries to find 0 or more occurre
nces of 'b'
aa
In [60]: print [Link]('ab*c','aa123').group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐60‐b324747346bc> in <module>()
‐‐‐‐> 1 print [Link]('ab*c','aa123').group()
AttributeError: 'NoneType' object has no attribute 'group'
In [61]: print [Link]('ab*c','aca123').group()
ac
In [62]: print [Link]('ab*.*','aca123').group()
aca123
[Link] 316/338
13/11/2016 python Basic and Intermediate Level Modules
In [63]: [Link]('ab+a','abbba').group()
Out[63]: 'abbba'
In [64]: print [Link]('ab+a','aa')
None
? causes the RE to match 0 or 1 time of the preceeding RE
ex: ab? > 'a', 'ab'
In [65]: string = 'hello'
In [66]: print [Link](r'hello?', string).group()
hello
In [67]: print [Link](r'hello?', 'hell').group()
hell
In [68]: print [Link](r'hello?', 'hel')
None
In [69]: print [Link](r'hell?o?', 'hel').group()
hel
In [70]: print [Link]('hell?o', 'helll') # 'l' is repeating 3 times
None
In [71]: print [Link]('hell?o', 'helllo') # 'l' is repeating 3 times
None
In [72]: print [Link]('hell?o', 'helo').group()
helo
* ‐ 0 or more
+ ‐ 1 or more
? ‐ 0 or 1
[Link] 317/338
13/11/2016 python Basic and Intermediate Level Modules
Gready Search Patterns
*?, +?, ?? GREEDY SEARCH Patterns
In [73]: string = '<H1>title</H1>'
In [74]: print [Link](r'<.*>', string).group()
<H1>title</H1>
In [75]: print [Link](r'<H*?>', string).group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐75‐ad2e5d60ff7e> in <module>()
‐‐‐‐> 1 print [Link](r'<H*?>', string).group()
AttributeError: 'NoneType' object has no attribute 'group'
In [76]: print [Link](r'<H1*?>', string).group()
<H1>
In [77]: print [Link](r'<.*?>', string).group()
<H1>
{m}
specifies the exactly m copies of previous RE
ex: a{6} it maches six 'a' characters
In [78]: string = 'aaashique'
In [79]: [Link]('a{3}shique', string).group()
Out[79]: 'aaashique'
In [80]: [Link]('a{3}shique', 'aashique')
In [81]: print [Link]('aa{2}shique', 'aaashique').group()
aaashique
[Link] 318/338
13/11/2016 python Basic and Intermediate Level Modules
In [82]: [Link]('a{3}shique', 'aaaaaashique').group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐82‐9d05772446f1> in <module>()
‐‐‐‐> 1 [Link]('a{3}shique', 'aaaaaashique').group()
AttributeError: 'NoneType' object has no attribute 'group'
In [83]: [Link]('aaaa{3}shique', 'aaaaaashique').group()
Out[83]: 'aaaaaashique'
{m,n}
causes the resulting RE to match from m to n repititions of the preceding RE
ex: a{3,5} will match from 3 to 5 'a' characters
In [84]: string = 'aaashique'
In [85]: print [Link]('a{2,3}shique', string).group()
aaashique
In [86]: print [Link]('a{2,3}shique', 'aashique').group()
aashique
In [87]: print [Link]('aa{2,3}shique', 'aaaashique').group()
aaaashique
{m,n}? combined regex pattern
In [88]: print [Link]('a{1,2}?shique', 'aashique').group()
aashique
In [89]: [Link]('a{2,3}','aaaaaa').group()
Out[89]: 'aaa'
In [90]: [Link]('a{2,3}',string).group()
Out[90]: 'aaa'
In [91]: [Link]('a{2,3}?',string).group() # takes 2 occurrences, due to the presen
ce of '?'
Out[91]: 'aa'
[Link] 319/338
13/11/2016 python Basic and Intermediate Level Modules
\ either escapes the special characters (permittin you to match characters like '*', '?') or used to signal a
special sequence
In [92]: string = '<H*>test<H*>'
In [93]: [Link]('<H*>',string).group()
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
AttributeError Traceback (most recent call last)
<ipython‐input‐93‐b5f7ee7a2e36> in <module>()
‐‐‐‐> 1 [Link]('<H*>',string).group()
AttributeError: 'NoneType' object has no attribute 'group'
In [94]: [Link]('<H\*>',string).group()
Out[94]: '<H*>'
In [95]: string = '<H?>test<H?>'
In [96]: [Link]('<H\?>',string).group()
Out[96]: '<H?>'
[ ]
used to indicate a set of characters.
regular expression characters will lose their significance, within the [] (square) braces
ex:
[mnk] ‐ will match the characters 'm', 'n' and 'k'
[a‐z] ‐ will match all characters from 'a' to 'z'
[A‐Z] ‐ will match all characters from 'A' to 'Z'
[0‐9] ‐ will match all characters from 0 to 9
[a‐m] ‐ will match all characters from 'a' to 'm'
In [97]: print [Link]('h[eE]llo','hello').group()
hello
In [98]: print [Link]('h[eE]llo','hEllo').group()
hEllo
[Link] 320/338
13/11/2016 python Basic and Intermediate Level Modules
In [99]: print [Link]('h[eE]llo','heEllo')
None
In [100]: print [Link]('h[eE]llo','heello')
None
In [101]: print [Link]('h[eE]*llo','heello').group()
heello
In [102]: [Link]('[a‐z].*','hello').group()
Out[102]: 'hello'
In [103]: [Link]('[a‐z].*','hello123').group()
Out[103]: 'hello123'
In [104]: [Link]('[a‐z]','hello123').group()
Out[104]: 'h'
In [105]: print [Link]('[a‐z]$','hello123')
None
In [106]: [Link]('[0‐9]$','hello123').group()
Out[106]: '3'
Note special characters lose their special meaning inside sets.
To match a literal ']' inside a set, precede it with a backslash, or place it at the beginning of the set. For
example, both [()[]{}] and [] () {[]} will match a parenthesis
In [107]: string = '<h*>test<h*>'
In [108]: [Link]('<h[*]>',string).group() # esacaping *
Out[108]: '<h*>'
In [109]: [Link]('<h\*>',string).group() # esacaping *
Out[109]: '<h*>'
Interview Question : Write a regular expression, to match all email IDs?
[Link] 321/338
13/11/2016 python Basic and Intermediate Level Modules
Assignment : Identify the email IDs in the resume in the following paragraph.
Welcome to RegExr v2.1 by [Link], proudly hosted by Media Temple!
Edit the Expression & Text to see matches. Roll over matches or the expression for
details. Undo mistakes with ctrl‐z. Save Favorites & Share expressions with friend
s or the Community. Explore your results with Tools. A full Reference & Help is ava
ilable in the Library, or watch the video Tutorial.
python@[Link]
Sample text for testing:
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789 _+‐.,!@#$%^&*();\/|<>"'python@[Link]
12345 ‐98.7 3.141 .6180 9,000 +42
555.123.4567 +1‐(800)‐555‐2468
foo@[Link] [Link]@[Link]
[Link] [Link]
[Link]
[Link]
mediatepmple@[Link]
[Link]@[Link]
1%453&harini_new@[Link]
Popular Regular expression Generators :
[Link]
[Link]
[Link]
[Link]/[Link])
Popular Regular Expression Visualizers :
[Link]
[Link]
to ignore the case (upper and lower)
In [110]: regObject = [Link]('python', [Link]) #compiling of regex object le
ts us to reuse it
result = [Link]('PYTHON')
print result
if result:
print [Link]()
<_sre.SRE_Match object at 0x0396DD40>
PYTHON
[Link] 322/338
13/11/2016 python Basic and Intermediate Level Modules
In [111]: reg = [Link]('python', re.I) # Both re.I and [Link] work in same
way
result = [Link]('PyThOn')
print result
if result:
print [Link]()
<_sre.SRE_Match object at 0x0396DC28>
PyThOn
In [112]: print [Link]('python', 'PyThOn', re.I).group() # Alternative method
PyThOn
[Link]
special character match any character at all, includinh a newline
In [113]: string = 'Today is Friday.\n Tomarrow is morning'
print string
Today is Friday.
Tomarrow is morning
In [114]: reg = [Link]('.*')
print [Link](string).group() # Observe that only first line is matche
d
Today is Friday.
In [115]: reg = [Link]('.*', [Link])
print [Link](string).group() # Now, all the lines will be matched
Today is Friday.
Tomarrow is morning
In [116]: print [Link]('.*', string, [Link]).group() # ALTERNATIVELY
Today is Friday.
Tomarrow is morning
Grouping
[Link] 323/338
13/11/2016 python Basic and Intermediate Level Modules
\w ‐ presence of Alphabet
\W ‐ absence of Alphabet
\d ‐ presence of digit
\D ‐ absence of digit
\s ‐ presence of White‐space
\S ‐ absence of white‐space
In [117]: print [Link]('\w', 'udhay prakash').group()
u
In [118]: print [Link]('\w*', 'udhay prakash').group()
udhay
In [119]: print [Link]('(\w)', 'udhay prakash').group()
u
In [120]: print [Link]('(\w*)', 'udhay prakash').group()
udhay
In [121]: print [Link]('(\w*)', 'udhay prakash').group(0)
udhay
In [122]: print [Link]('(\w*) (\w*)', 'udhay prakash').group()
udhay prakash
In [123]: print [Link]('(\w*) (\w*)', 'udhay prakash').group(0) # group(0) is same a
s group()
udhay prakash
In [124]: print [Link]('(\w*) (\w*)', 'udhay prakash').group(1)
udhay
In [125]: print [Link]('(\w*) (\w*)', 'udhay prakash').group(2)
prakash
In [126]: print [Link]('(\w*)(\W)(\w*)', 'udhay prakash').group()
udhay prakash
In [127]: print [Link]('(\w*)(\W)(\w*)', 'udhay prakash').group(3)
prakash
[Link] 324/338
13/11/2016 python Basic and Intermediate Level Modules
In [128]: print [Link]('(\w*)(\s)(\w*)', 'udhay prakash').group()
udhay prakash
Perl based grouping pattern
(?P<name>)
In [129]: m = [Link](r"(?P<first_name>\w+) (?P<last_name>\w+)", "Barack Obama" )
In [130]: [Link]()
Out[130]: 'Barack Obama'
In [131]: [Link](0)
Out[131]: 'Barack Obama'
In [132]: [Link](2)
Out[132]: 'Obama'
In [133]: [Link]('first_name')
Out[133]: 'Barack'
In [134]: [Link]('last_name')
Out[134]: 'Obama'
In [135]: first_name # Observe that those identifiers can't be used outside
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
NameError Traceback (most recent call last)
<ipython‐input‐135‐c57da601b80c> in <module>()
‐‐‐‐> 1 first_name
NameError: name 'first_name' is not defined
In [136]: [Link](r'(..)+', 'alb2cs').group()
Out[136]: 'alb2cs'
NOTE: If a group matches multiple times, only the last match is accessible
In [137]: [Link](r'(..)+', 'alb2cs').group(0)
Out[137]: 'alb2cs'
[Link] 325/338
13/11/2016 python Basic and Intermediate Level Modules
In [138]: [Link](r'(..)+', 'alb2cs').group(1)
Out[138]: 'cs'
In [139]: [Link](r'(..)+', 'alb2cs').group(2)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
IndexError Traceback (most recent call last)
<ipython‐input‐139‐78100e216102> in <module>()
‐‐‐‐> 1 [Link](r'(..)+', 'alb2cs').group(2)
IndexError: no such group
Assignment : Try replacing match with search in the below expression, and reevaluate
[Link](r'(..)+', 'alb2cs').group(1)
ktg cl19
[Link] 326/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 327/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 328/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 329/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 330/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 331/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 332/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 333/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 334/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 335/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 336/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 337/338
13/11/2016 python Basic and Intermediate Level Modules
[Link] 338/338