Python Quick Refresher.
This section is Quick Refresher for Python,
If you’re already familiar with Python programming
This will section will help to recall all the core concepts of Python.
If you’re new to Python please check out Python 101.
This section covers briefly about fallowing.
TABLE OF CONTENTS
- Why Python ?
- Python Installation
- First Program
- Running code from a file
- Importing and Modules
- Comments in Python
- Indentation is important
- Keywords
- Most common built-in functions
- Variables and Assignments
- Operators In Python
- Sequences
- Strings
- List and Tuple
- if-elif-else
- Loops
- List Comprehensions
- Generator Expressions
- Hashing Types
- Dictionaries
- Sets and Frozensets
- File Handling
- Errors and Exception handling
- Functions in Python
- Magic or dunder methods
- Objects and Classes
- Modules and Packages
- os module
- shutil module
- os.path module
- sys module
- Generators
- Decorators
Intro python syntax
Why Python
Great documentation and community
Very clean syntax
Readable code
Open source
Multi-platform – Windows Unix/Linux Mac
Interactive – built in interpreter no pre compiling
Code can be grouped in modules and packages
Object Oriented Programming paradigm
Rapid development
Multiple programming paradigm – OOP, procedural, functional
Exception handling – like in C++ , Java
In built Memory management
Batteries loaded……
Python can be Used for
Web development , Networking applications, Scripting
Software testing, GUI development, Scientific applications
Popular applications built on python
youtube, openstack, google, Quara, dropbox,
reddit, bitly,SurveyMonkey, esri, esa, blender
Python Implementation
Cpython -> Written C
Jython -> Java
IronPython -> .NET
pypy -> RPython
Batteries included……
Python called as “Batteries Included” language,
As it comes with built in standard Python library,
With approximately 190 plain (“top-level”) modules,
like,
math, sys, os, struct, re, random, gzip,collections
contextli, socket, select, urllib, ftplib, rfc822,
bsddb, compiler, ctypes, curses, email,
encodings, unittest etc…
GO TO TOC
Installation
installers are available for most operating systems
@ www.python.org/download
Linux:
Most of the Linux distribution come with default Python installation.
You can also get Python Installed with below command.
On shell type
sudo apt-get install python3
Windows:
down load and run the latest installer
Python 3.6.4 – as on 2018-01-13 or latest
Download Windows x86-64 MSI installer
Set environment variable
My Computer > Properties > Advanced System Settings > Environment Variables >
Python comes with Python shell and IDLE IDE
Hello World Program
The classic same old – Hello World program.
Ex:1
Open Linux shell type Python3 or
On Windows from start menu open Python3 command program
and type the below lines.
>>>print("Hello World") >>>print ("Hello" + " Guido van Rossum ")
GO TO TOC
Running code from a file.
Open a first.py and write code
On Unix like machine, the first line
of your Python file has to point to,
the Python interpreter location.
It is is usually one of the below.
#!/user/bin/python #path to python interpreter OR #!/user/user/bin/python OR #!/user/bin/env python print("Hello, World!")
save the file, give execute permission
chmod +x first.py
and run by using
python3 fist.py or ./first.py
on windows open IDLE,
open a new file , write below code , save it and run.
print("Hello, World!")
Everything in python is an object
GO TO TOC
Importing and modules
module(s) -collection self contained Python code/files
import – allows you to use outside code in your application.
Most often you might import modules which gives,
features like networking,web-services,file handling, etc..
reload() – Reload is a built-in function,
which reloads update version of module
How to check what modules available
help(‘modules’)
importing a module
Style1: import whole module all elements
import os os.getcwd() #dot notation
Style2: import individual element from module
from os import getcwd getcwd() #notice os. is not needed
Common syntax in python examples
Python interpreter path
#!/user/bin/env python
Comments in Python
Single and Block commenting
# This is single line comment
Block comment
# this is first line of block comment # this is second line of block comment # this is third line of block comment # this is fourth line of block comment
Strings representation.
Stings are represented in quotes, single, double, triple quotes.
'I am a single quote string ' "I am a double quote string" ''' I am a triple single quote string''' """ I am a triple double quote string """
Triple quotes are generally used to present multi line strings,
And also commonly used for docstring representation.
""" First line of multi-line string. Second line of multi-line string. Third line of multi-line string. Fourth line of multi-line string. """" or ''' First line of multi-line string. Second line of multi-line string. Third line of multi-line string. Fourth line of multi-line string. '''
GO TO TOC
Indentation is important
In Python indentation is important.
Python identifies program logic through indentation.
def function(): #indented code block #indented code block #indented code block
Ex 1:
def main(): print("This is the syntx.py file") if __name__ == "__main__": ''' this line allows to run method in order we want ''' main()
“if __name__ == “__main__”:” is used to run Python file as standalone module,
When your module is imported in another module it is not executed.
Keywords
Like in other programming languages, Python also has a list of
restricted words, which you can not use as identifier.
Keyword list varies from Python version to version.
To get keyword list in current installation.
import keyword for keyword in keyword.kwlist: print(keyword)
You can also check if a word is keyword or not
keyword.iskeyword('if') # you can also check if name is kewyword keyword.iskeyword('Alex')
GO TO TOC
>Most common buit-in functions
Informational built-in functions
dir() – lists out the objects attributes in current namespace.
help() – shows help text of objects, topics.
type() – returns the type object type.
isinstance()– to check object type
cmp() – To compare 2 objects returns
-1 – if the object-1 less than object-2
0 – if the objects are equal
1 – if the object-1 is greater than object-2
Operational built-in functions
int() -Integer type constructor, converts a number or string to integer,
returns 0 if no arguments.
str() -String type constructor, string representation of object
range(end)
range(start,end,step) – Returns an object that produces a sequence of numbers,
including start, excluding end, with a step if provided.
input() – Prompt for user input, reads a string from standard input.
len() – Returns the number of items in container.
Avoid these names as identifiers in your program,
otherwise you’re overriding, Standard library functions.
GO TO TOC
Variables and assignments
Unlike in other languages, Python doesn’t have primitive datatypes.
Python is dynamic type language, no need of variables declaration
Variables and Data types
A variable is reference to memory location where a value is stored.
The value of the stored location may change.
variable names must start with a letter or underscore.
And may contain numbers and letters and underscore there after.
name, _name, or __name, name9,
full_name, last_name, name_, pass_word, password9
are all valid variable names.
Examples:
name = "Alex" _name = "Alex" __name = "Mark" name_ = "Julie" name__ = "Anne" pass_word = "as8#99A" last_name = "Shepherd"
Variable assignment examples :
a = 10 #int a = 'string' #string a = 10.10 #float #flexible assignment a = b = c = 10 # a = 10, b = 10, c = 10 a, b, c = 10, 11, 12 # a = 10, b = 11, c = 12
Standard Types
more information on standard type.
Most common standard types
numbers – includes int , long, floating -float , complex, Boolean
sequences – Sequences can be indexed,
Examples: range(), list, tuple, str, unicode, bytearray, buffer,
hashing type – dict() is the only available Mapping type, dict() maps key to value.
operators and built-in functions
numeric -built-ins
Factory pattern
int()
float()
complex()
Operational pattern
abs()
pow()
round()
devmod()
chr()
GO TO TOC
Relational operators
Operator for object comparison.
Object value comparison
== -Returns True of if objects are equal, otherwise False
!= -Returns True of if objects are not equal, otherwise False
< -Returns True of if object-1 is less than object-2, otherwise False
> -Returns True of if object-1 is greater than object-2, otherwise False
<= -Returns True of if object-1 is less than or equal to object-2, otherwise False
>= -Returns True of if object-1 is greater than or equal to object-2, otherwise False
Object identity comparison
is
is not
a = 10 b = 10 c = 20 print(a is b) print(c is not b)
Boolen types
not
and
or
Examples:
a = 10 b = 10 c = 20 if a == 10 and b == 20: print(a,b) if a > b or b < c: print(a,c) if a > 0 and b is not 20: print(a,b)
Arithmetic operators
+ => Addition
– => Subtraction
* => Multiplication
/ => Division
// => Absolute division Ex. 1.0/2 , 1.0//2
% => Modulo a%b
** => Power a ** b
short form – Augmented assignment
In Python we don’t have ++ or — operator
+= => a += b same as a = a + b
-= => a -= b same as a = a – b
*= => a *= b same as a = a * b
/= => a /= b same as a = a / b
bit operators
Bit operators work only with integer types.
& => and operator
| => or operator
^ => xor operator
<< => left shift operator
>> => right shift operator
~ => tilde operator
GO TO TOC
Sequences
strings(str, unicode)
lists (list)
tuples (tuple)
Sequences are array-like types, also called as collections or containers.
List and Tuple sequences are heterogeneous types,
they can hold objects of different types.
Elements of sequences are accessed by indexing,
the first element stats at 0th index, and last element is at len(seq) -1
Slicing operation on sequence
Slicing works same way for all sequences.
Below are some examples of string slicing.
Slicing syntax:
[begin:end]
lang[2:5] # 'tho' lang[3:] #'hon' lang[:2] #'Py' lang[-4:-1] #'tho' lang[:] #'Python'
Extended slice syntax.
[begin:end:step]
lang[1:-1:2] # 'yh' #reverses the string lang[::-1] # 'nohtyP'
Sequence operators
Membership checking operators
in , not in Ex 1: 't' in 'python' # evaluate to True 'python' not in 'complicated programming languages' # evaluate to True
Concatenation operator
+
Ex 2:
'python' + ' ' + 'is'+ ' ' +'cool' # 'python is cool'
Repetition /Duplication
Ex 1: ' - ' * 30 output: ' - - - - - - - - - - ' Ex 2: 'abc' * 3 output: 'abcabcabc' Ex 3: [1,3,5] * 4 output: [1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5]
Sequence built-ins functions
Factory Pattern
str() => String constructor type, returns a immutable string.
list() => List constructor type, returns a list.
tuple() => Tuple constructor type. returns a tuple.
bytes() => byte array constructor type.
bytearray() => bytearray constructor type, constructs a mutable bytearry.
Operational Pattern
len() => number of items in sequence
max() => maximum of sequence items
min() => minimum of sequence items
GO TO TOC
STRINGS
A string is a immutable sequence contains array of characters.
'This is a string ' # single quote "This is also a string" # double quote #multi line string ''' this is a multi line ... string which continues on ... 3 lines ''' output: ' this is a multiline\nstring which countinues on \n3 lines '
Raw string
Raw strings are represented with prefix ‘r’ or ‘R’ to strings.
With raw string what you see is what you get(WYSIWYG)
file_path = r"C:\Users\PC-USER" print(file_path) output: C:\Users\PC-USER #without 'r' prefix you need provide escape sequence character. file_path_wo = "C:\\Users\\PC-USER" print(file_path_wo) output: C:\Users\PC-USER
Getting help on str object
help(str)
Duplication operator on string
s = "Just Learn Python" s*2
string formatting
“Name: %s, age: %d” % (‘abc’,’27’)
“Name: %s, age: %d” % (‘abc’,int(’27’))
“Name:{0}, age:{1}”.format(“abc”,27)
Basic string methods
split() #split on delimiter
join() #join list with delimiter
find() #substring search
strip() #remove WS
llower() #lowercase
replace() #replace
ljust() #left justification
isdigit() #check digits in string
Find attributes of string object with dir()
s = "Just Learn Python" print(dir(s))
Ex:1
s = 'Just learn Python, Python is cool. ' print(s) # returns -1 if not present s.find('is not') output: -1 # returns the start index s.find('is') output: 26 # returns the highest index #if substr present s.rfind('o') output: 31 # pad exra spaces on both side s.center(50) output: ' Just learn Python, Python is cool. ' # we can multiple methods at a time # this is called method chaining. #trims the white space on both side #lstrip() trims on leftside #rstrip() times on rightside s.center(50).strip() output: 'Just learn Python, Python is cool.' # returns a list of words split by delimeter #default delimeter is white space s.split() output: ['Just', 'learn', 'Python,', 'Python', 'is', 'cool.'] # join is opposite of split # joins list of words with a space ' '.join(s.split()) output: 'Just learn Python, Python is cool.' # joins list of words with a colon ':'.join(s.split()) #opposite of split 'Just:learn:Python,:Python:is:cool.' # returns a string in Title Case s.title() output: 'Just Learn Python, Python Is Cool. ' # returns a string in UPPERCASE s.upper() output: 'JUST LEARN PYTHON, PYTHON IS COOL. ' #returns a string in lowercase s.lower() output: 'just learn python, python is cool. ' # swaps the case s.title().swapcase() output: 'jUST lEARN pYTHON, pYTHON iS cOOL. ' #returns True if string only contains alphabets s.isalpha() output: Fasle #returns True as Just is only alphabets 'Just'.isalpha() output: True #returns True if string contains only digits '012342'.isdigit() output: True #returns True if string contains #both alphabets and digits '012342abcdd'.isalnum() # Validate if string ends with period s.endswith('.') output: True #replaces the givern str with new str s.replace('.', '?') ouput: 'Just learn Python, Python is cool? '
Types of strings
r’string’ – prefix ‘r’ represents raw string ignore escape sequences
u’string’ – prefix ‘u’ represents a Unicode string
b’string’ – prefix ‘b’ represents a byte string
GO TO TOC
LISTS & TUPLE
LISTS
List is an array like data structure enclosed in [ ],
Perhaps most used data structure in Python, Lists are mutable
Most common operations on lists
are
1.Choose elements that fit criteria
2.Apply functions to each element
append() add object at the end
insert() – add object anywhere
remove() – delete an object, if object not present
does nothing.
pop() – delete object
reverse() – reverse in-place , changes original list
sort() – sort in-place, changes original list
reversed() – copy of reversed
sorted() – copy of sorted
extend() – merger another list
count() – number of occurrences
index() – first index of a value
Examples:
prog=['core', 'programming' , 12, 2018] prog.append('published') prog.insert(1,'Python') 12 in prog 'fund' not in prog prog.pop(3) prog[2:4]=['Programming', '2015'] #replaces 2:4 with new values prog.remove('core')
TUPLE
Tuples are like a lists but are immutable type.
Usually defined with ( ) but ( ) are not mandatory
Only while defining tuple ( ) are not mandatory.
pos1 = (24,50) pos2 = 50, 70 #any sequence separated by comma is tuple
But when passing tuple as argument to functions () is required
Like other sequences, tuples are index able, slice able.
And are heterogeneous type, can hold different types of objects.
Ex 2:
t1 = (1,'mango',2,'mango',3,'mango',4,'mango') print(t1) output: (1, 'mango', 2, 'mango', 3, 'mango', 4) Ex 1: index of element 4, index starts from zero t1.index(4) output: 6 Ex 2: count number times element in tuple t1.count('mango') output: 4 Ex 3: Accessing element of a tuple t1[2] output: 2 Ex 4: Get the first index t1.index('mango') output: 1
Assignment examples with slicing
a,t,lst = "0123456789"[:],('one','two','three')[:],[1,2,3,4,5][:] a t lst a,t,lst = "0123456789"[:3],('one','two','three')[:3],[1,2,3,4,5][2:] a t lst
GO TO TOC
Conditionals
In Python we have only one conditional statement ,
if-elif-else, there is no switch-case like in Java , C/C++.
Syntax : if <condition>: statements elif <condition>: statements else: statements
Ex 1:
a,b = 0,1 if a < b: print ('a({}) is less than b ({})'.format(a,b)) else: print ('a({}) is not less than b ({})'.format(a,b))
Ternary operation
Ex 2: similar to ternary operation
C-style:
(a < b ? "True":"False") a = 10 b = 20 print("True" if a < b else "False") output: True
Ex 3:
value = 40 if value > 50: print (" Greater than 50") elif value < 20: print ("less than 20") else: print("Between 20 and 50") output: Between 20 and 50
GO TO TOC
Loops
In Python, there are only two loop statements are available.
1. While loop
2. for loop
while loop
syntax:
while <condition>: statements
Ex 1:
Generate Fibonacci series up to 50.
a,b = 0,1 while b < 50: print(b) a,b = b, a+b print("done") #look at the indentation
Ex 2:
Syntax:
while True: #infinite loop #code block break; a = 0 while True: #infinite loop if a < 10: print(a) else: break a += 1
for loop
for loop is mostly used to iterate through an iterable object.
It can be used either by index or iterate style. see example below.
iterable– is any data structure that supports element traversal
ex: stings, lists, tuples iterators,
lines of file, keys of dict, generator sets etc.
range() – built in function, returns an object,
which generates sequence of numbers.
Syntax :
range(start,end,step)
usage of for with range.
Ex 1:
for num in range(10): print (num) for num in range(5,25): print (num) for num in range(5,25,2): print (num)
enumerate() – built in function
which return an enumerate object, yields pairs,
containing a count and a value yielded by iterable object.
Ex 2:
music =('rock','pop','alternative','folk') # without enumerator for i in range(len(music)): print ('music : %d: %s' %(i+1, music[i])) # with enumerator for i, style in enumerate(music,1): print ("music %d : %s " %(i, style))
Ex 2:
Using for to read lines of a file
fd = open('lines.txt') for line in fd.readlines(): print(line)
break – Break the loop from current execution
continue – skip the current execution cycle
pass – do nothing
Example :
if a <b : pass #nothing to be done
GO TO TOC
LIST Comprehensions
Construct lists using logic for loop logic.
Syntax:
[expr for var in interable]
Example :
range(10) [i for i in range(10)] [i*2 for i in range(10)] def fun(num): return num*4*5/2+5*2 [fun(i) for i in range(10)] [i for i in range(10) if i % 2 == 0]
Ex 2:
path = 'C:\bla bla' #file path fd = open(path, 'r') lines = [line.strip() for line in fd]
GO TO TOC
LIST Generator Expressions
List Comprehensions are not memory efficient
Alternative to list comprehensions are generator expressions
generate successive values,
values can be accessed using next() built in function.
Syntax:
(expr for var in interable)
GO TO TOC
HASHING Types
dic() – dictionaries,only mapping type
set() – mutable sets
frozoneset() – immutable sets
Common properties of hashed collection/container objects
Hash type object are accessed by iteration
heterogeneous in nature, can hold different types objects.
Items are un-ordered by definition.
GO TO TOC
DICTIONARIES
Python’s only mapping type, dict map keys to values.
A dict is mutable, you can add, delete and modify its elements.
It is also a heterogeneous type, capable of holding different
types of objects.
Defining a dictionary
Dictionary is defined by using {} or dict() factory function.
Creating a dictionary by using {} d ={'title':'Python programming'} Creating a dictionary by using dict() built-in constructor function. d1 = dict(title = 'Python programming')
Buit-in methods of Dictionary
get() – get value with key
update() – merge another dict
setdefault() – set default values to keys
pop() – remove and return value
items() – all keys and values
clear() – clear dict, remove all elements
fromkeys() – make new dect with as keys from iterable.
general dict operations
len() – number of key-value pairs
if k in d: – in or not in
for k in d: – loop through dict
d[‘key’] – indexing, access the value by key
GO TO TOC
SETS and FROZENSETS
SET is a finite collection of objects.
set() – set is mutable collection of unique elements
frozenset() – frozenset is immutable collection of unique elements.
sets and frozenset are un-ordered and heterogeneous.
sets and frozensets are similar to Mathematical sets.
They set operations like in Maths, see examples below.
a =[1,2,3,4] b =(9,5,2,7,5,3) print(a,b) c = set(a) d = set(b) print(c) print(d) c.intersection(d) # same as c & d c.union(d) # same as c|d c.add(8) # add element c - d
GO TO TOC
Files and Input/Output
standard files
stdin
stdout
stderr
Python automatically opens these standard files for each process.
The standard files are part of sys module.
You can check by importing sys module
sys.stdin
sys.stdout
sys.stderr
input() – Reads stream from current sys.stdin
print() – Prints the output to current sys.stdout
File Hanlding
A file is also an object, open() built-in function is used
to open a file.
open() – open file returns to reference file object, usually
referred as file handler or file descriptor.
Syntax:
file_handle = open(file_name, access_mode)
You can open a file in variety of modes.
Possible access_modes are ‘r’, ‘U’, ‘w’, ‘a’
‘U’ – Specific to python, treats all line terminators as new line.
Other modes :
‘+’ #read and write
‘b’ # binary
‘t’ # text mode
We can also access modes in combination, like ‘rb’, ‘rt’, ‘wb’,’wt’ etc…
Most common file object methods
close() – close a file
read(n) – read n bytes from file, or
entire file as a string if n is not specified.
readline() – read single line as string from file
readlines() – read all lines as list
write() – write a string to file
writelines() – write list of string to file,
note there is no writeline()
seek() – move current cursor position within file
tell() – tell the current cursor position in the file
flush() – flush output buffer of file
Ex 1:
fn = 'sample.txt' # open file in read mode fn = open(fn,'r') all = fn.readlines() f.close() for line all : print (line)
Persistent storage
files are persistent data , but at times its a overhead to
very frequent data handling with files.
Python provides few other facilities to work with persistent data.
Other forms of persistent data
pickled files – save Python objects
DBM files – abstraction of dict strings
shelve files – shelve is the combination of above two.
RDBMS – read database, SQLite, MySQL, PostGreSQL etc
GO TO TOC
Error and Exception handling
exception
An exception is an occurrence of an ‘unexpected event’
general a error situation , but not always ctrl + C, SysExit, StopIteration,
exception handling
Exception handling is to detect an exception at runt-time and take some action.
catch
Catch is to detect an exception and look for a handler.
handler
Handle code for action to be taken when an exception is caught.
throw
Throw is to tell the Python interpreter to raise an exception.
Syntax:
type 1:
try: actual code except: handler code
type 2:
try: actual code except: handler code else: code when no error
type 3:
try: actual code except: handler code else: code when no error finally: code run at all time
type 4:
Handling specific exception. try: actual code except ErrorName [as reason]: handler code
reason is an identifier which refers to the exception.
usage:
except specific exception as reason, and print it in a user friendly format.
except SomeError reason : print ("Error occured %s" % str(reason)
Ex 1:
try: fd = open('file.txt','r') except IOError as e: print ('File Open error: ' % str(e)) return False
Some Common Exceptions
Refer to exception module in Python documentation for more
details on exceptions.
Exception /BaseException – Root classes for most /all exception
KeyError – No such key
ValueError – Invalid argument given
ZeroDivisionError – Division by zero
AssertionError – Failure to assert statement
SyntaxError – Python syntax error
TypeError – Invalid operation for this type
AttributeError – No such attribute
IndexError – No such index
IOError – input/output operation failure
UnboundLocalError – Attempted to read local var before assigning
KeyboardInterrupt – interrupt from keyboard ex: ^C
Exception are very helpful in debugging a error scenario.
We can also extend the use of exceptions information
to postmortem analysis.
Post-mortem analysis using sys and traceback modules
We can also get more information about error scenarios,
with the help of sys and traceback modules.
Sys Module
sys.exc_info()
Return information about the most recent exception.
sys.exc_info() returns three values,
1. exception class,
2. exception reason,
3. traceback object
traceobject module
provides standard interface to extract, format and print,
stack traces of Python programs.
The third value traceobject can be further, used to extract,
format and display stack traces.
GO TO TOC
Functions in Python
Function are defined using def keyword,
function promote code re-usability.
Syntax:
def function_name([parmeters,]): statements
Ex 1:
simple program to add two numbers
def add(num1, num2): return num1 + num2 add(10,40)
if your function doesn’t return anything, Python returns default None
How Python treats return Values of a function
return 0 objects => None # if nothing is returned python returns None
return 1 object => object #python returns 1 object
more than 1 objects => tuple #tuple as a collection of objects
is it call by reference or call by value in Python>
Python doesn’t fallow that model directly
it’s actually depends on passed arguments mutability
if the object is mutable, object can be changed in the function (reference)
if the object is not mutable then its call by value
variable scope
global – can be accessed from anywhere
local – can be accessed only in block where defined
nonlocal – can be accessed in enclosing blocks(inner functions clousers)
x = 2 # global variable def fun(): # fun is global object y = 30 # local variable print("I'm global x:", x ) print( "I'm local, can't access:",y) # throws error
Namespace and Scope
A namespace contains a bindings of names to object,
there may be more than 1 variable with the same name
scope is derived from namespace.
there are many namespaces
one for function, class, instance, modules etc.
evaluation is done from inner to outer
ex: scope evaluation order local ->nonlocal->global, buit-ins
Variable arguments
use * and ** in function declaration
* #tuple
** #dict
While using varargs order of position and keyword arguments is important.
def fun(pos1, pos2 ='abc', *args,**kwargs): print("pos arg 1 :", pos1) print("pos arg 2 :", pos2) for arg in args: print( "extra args :", arg) for k ,v in kwargs.items(): print("extra kw arg : %s =%s" %(k,v)) #calling: fun(42, 'xyz', 'one more', bar='cheers') output: pos arg 1 : 42 pos arg 2 : xyz extra args : one more extra kw arg : bar =cheers fun('change', num=3.3234, tmp ='state') output: pos arg 1 : change pos arg 2 : abc extra kw arg : num =3.3234 extra kw arg : tmp =state
GO TO TOC
Magic or dunder Methods
In Python special methods starts and ends with __ (double underscore) __
They are special, because you don’t have call them explicitly,
Python automatically invokes them for you.
__int__(self, …) – initializer method invoked during object creation.
__iter__(self) – makes object iterable object.
__eq__(self,other) – defines behaviour of object equality (==)
__ne__(self,other) – defines behaviour of object non equality (!=)
__lt__(self,other) – defines behaviour less than (<) __gt__(self,other) – defines behaviour greater than (>)
__le__(self,other) – defines behaviour less or equal (<=) __ge__(self,other) – defines behaviour greater or equal (>=)
__add__(self ,other) – defines objects addition (+)
__sub__(self,other) – defines behaviour object subtraction (-)
__int__(self,other) – defines behaviour conversion int (int()).
Private attributes
attributes which begin with __ are called private attributes.
These are usually used by internally Python itself.
Objects and Classes
Object oriented programming generally abbreviated as OOP.
OOP is one programming paradigm, Perhaps widely used and most popular.
Most of us are familiar with,
structural #conditional statements, loops, etc
procedural programming #procedures/functions etc
Three pilars of OOP
Encapsulation
Inheritance
Polymorphism
Goal of oop is to break tasks into smaller objects. ‘
OOP promotes collaboration among development team.
Objects in OOP are no different than real world objects.
OOP also promotes interaction among different objects.
OOP integrates logic and data together in an object.
New objects can be created from existing ones(inheritance),
Thus sharing the implementation.
OOP terminologies
classes #container, object blueprints (abstraction)
#used to create real objects
instance #a constructed object of the class
functions/ #part of class (methods) callable attibutes
methods
subclass #relation ship with main class, derived class
encapsulation #hide data, data and logic integrated
namespace #scope of related attributes
attributes/ #variables and class methods
members
type #class of the object
derivation #derive from main class
polymorphism #different forms of a objects
#function overloading.
Even though Python is a pure object oriented programming language,
but it doesn’t enforce user to write object oriented code.
Multiple paradigm can coexists in Python program.
Classes
Class is like a container or a blueprint.
Class name starts with a capital letter, that’s convention.
A class is created with a class keyword fallowed by class name,
Syntax:
class MyClass: class-definition or class MyClass(): class-definition new style: class MyClass(BaseClasses,): class-definition.
A class becomes alive, when an object is created.
It provides a separate class level namespace for its elements.
It binds the state and behavior into an object.
Through inheritance class creates relation between,
parent and child classes.
class attributes
Class has attributes, some are callable attributes.
Callable attributes are called as methods.
Methods are used for implementation of behavior of the object.
While identifiers are used as state holds the data.
in Python we can have three types of methods.
Instance methods, class methods, static methods.
Instance methods work on instance attributes, and the first argument
is reference to that object its called upon, ‘self’ word is used
as usual convention, ‘self’ is similar to ‘this’ in Java, C++.
Instance methods are bound to the particular instance.
class methods are not bound to instance, they can be called
before an instance is created.
Two things we can do with a class
1. create object
2. use to derive subclass
Python does’t have constructors or destructor,
it has initializers and terminators, __init__ and __del __
special methods.
Simple example a class
Ex 1:
class MyClass(object): pass obj_1 = MyClass() print(obj_1) obj_2 = MyClass() print(obj_2)
setters and getters
Using set() and get() setters and getter() instance methods,
to access and modify instance attributes.
Ex 2:
class MyClass(object): def set(self, value): self.val = value def get(self, value): return self.val
Ex 3:
A simple address book entry class example.
class AddrBookEntry(object): "This address book entry class " #it's doc string version = 0.1 # class attribute # special instance method #initializes object during creation #self is like "this" in C++/Java def __init__(self,name,phone): self.name = name self.phone = phone def updatePhone(self, new_phone): self.phone = new_phone
Instances
Instance is one unit of an object.
They are created by calling class as function.
Classes are also objects, they can be called or passed as an object,
due to this they are called as first class objects.
obj = MyClass()
class come to life through objects, Python creates an object,
by invoking a special method __new__() and calls __init__()
to initialize the object attributes , then returns object.
In Python there is no “new” keyword in Python like in C++ / Java.
Ex 1:
class Duck(object): def __init__(self,value): print( "Inside __inti__ method ") self._v = value def quack(self): print ("Quack Quack Quack like a duck !" ) def walk(self): print("Walk like a duck !") # Create an instance of a Duck donald = Duck(1010) # calling quack () method donald.quack() # calling walk() method donald.walk() output: Inside __inti__ method Quack Quack Quack like a duck ! Walk like a duck !
Ex 2:
class AddrBookEntry(object): "This address book entry class " #it's doc string version = 0.1 # class attribute # special instance method #initializes object during creation #self is like "this" in C++/Java def __init__(self,name,phone): self.name = name self.phone = phone def update_phone(self, new_phone): self.phone = new_phone def display(self): print ("display : Name : ", self.name) print ("display : Phone : ", self.phone) print( ' -' * 20) print( "user1\n".title()) # creating an instance user1 = AddrBookEntry("user1", "x000-1111") # Acdessing instance attributes print( "name :" , user1.name) print( "phone :" ,user1.phone) print( ' -' * 20) print ("user2\n".title()) # creating another instace user2 = AddrBookEntry("user2", "x000-2222") # accesing user2 attributes print( "name :" , user2.name) print( "phone :" ,user2.phone) print( ' -' * 20) print( "addign instance attribute on the go \n".title()) # Adding place attribute on the go user1.place = "USA" print ("user1 :place :" , user1.place) # user2 doesnt have place attribute # thows AttributeError #print ("user2 :place :" , user2.place ) print( ' -' * 20) print ("calling instace methods\n".title()) user1.display() user2.display() print (' -' * 20) print( "modifying instance attribute version \n".title()) # accessing class attribute version print( "user1 version :" , user1.version) # user1 version is modified user1.version = 0.2 print( "user1 version :" , user1.version ) # user2 version will remain the same print( "user2 version :" ,user2.version) output: - - - - - - - - - - - - - - - - - - - - User1 name : user1 phone : x000-1111 - - - - - - - - - - - - - - - - - - - - User2 name : user2 phone : x000-2222 - - - - - - - - - - - - - - - - - - - - Addign Instance Attribute On The Go user1 :place : USA - - - - - - - - - - - - - - - - - - - - Calling Instace Methods display : Name : user1 display : Phone : x000-1111 display : Name : user2 display : Phone : x000-2222 - - - - - - - - - - - - - - - - - - - - Modifying Instance Attribute Version user1 version : 0.1 user1 version : 0.2 user2 version : 0.1
Notice user1 and user2 versions, they are different,
Even though both are from same class, to avoid modifying,
instance attributes accidentally, setter and getter methods are recommended.
inheritance
subclasses
A class which inherits the base class.
Base class attributes don’t change unless overridden in subclass.
Python supports multiple inheritance, that is a class can inherit from
more than one base class.
MRO – Method resolution order, is used to resolve attribute inheritance.
Method definitions are resolved by MRO, which is from left to right,
order of inheritance.
Extending duck example, Duck class inherits Animal class.
class Animal(object): def talk(self): print("I'm talking !") def walk(self): print ("I'm walking !") def clothes(self): print ("And I've nice clothes !") class Duck(Animal): def __init__(self,value): self._v = value def quack(self): print ("Quack Quack Quacking !" ) def walk(self): print("Walk like a duck !") donald = Duck(1010) donald.quack() donald.walk() donald.talk() donald.clothes()
Ex 2:
Initializing base class inside derived class.
class EmplAddrBookEntry(AddrBookEntry): 'Employer Address Book' def __init__(self, name ,phone,id,blood_group): AddrBookEntry.__init__(self,name,phone) self.id = id self.blood_group = blood_group def display(self): AddrBookEntry.display(self) print('id : ', self.id) print('blood group : ', self.blood_group) user1 = EmplAddrBookEntry("James Tylor", "4320-345" ,1001,'AB+ve') user1.display()
built-in functions used with classes and objects
isinstance() #whether or not object of a perticular class
issubclass() #is this class subclass of a perticular class
has(), get(),set(),del() attributes
dir()
In Python all class members are in public, there is no private,
or protected or friend access specifiers.
GO TO TOC
Modules and packages
os and os.path and shutil module
os Module
Most common used os module methods
getcwd()
listdir()
chdir()
walk()
exists()
isfile()
isdir()
Example:
List all the names available in os module
import os for line in dir(os): print(line) os.getcwd()
getcwd()
from os import getcwd #current working dir getcwd() org_dir = getcwd()
form os import listdir listdir('.') #list dir of given path , path can be relative or absolute
Ex 1:
from os import listdir from os import getcwd org_dir = getcwd() files =listdir(org_dir) for file in files: print(file)
chdir()
chdir()
form os import chdir #change dir chdir('dirname') #takes
walk()
# recursively explore directory contents
from os import walk #walk returns list of tuples tree = walk('.') for dir,subdir,files in tree: print ("%s %s %s" %(dir,subdir,files)) tree = walk(getcwd(), topdown = False) #optional parameter
exists()
from os import exists
isfile()
from os import isfile
isdir()
from os import isdir #important while deleting files #need to avoid dir deletion
Example :
def check(path): if isfile(path): print ("It's a file") elif isdir(path): print ("It's a directory") else: print( "It doen't exists") path = 'pathname' check(path)
access()
from os import access from os import F_OK, R_OK,W_OK,X_OK # F_OK , does the path exists ? access(filename, F_OK|R_OK|X_OK)
stat()
form os import stat info = stat('gener.py') info
join()
to create os independent path (linux ‘/’ , windows ‘\’)
join picks the file separtor based on current operating systems
from os.path import join path = join(base,user,datadir)
normpath()
norpath not only converts path seperators ,
it removes dulplicated path separators,
removes “.” current dir short hand,
tries to reslove “..” patent dir shor-hand
from os.path import normpath
how to check os or os.path
‘normpath’ in dir(os.path) #returns True or False
Below are some other methods related os module.
mkdir() #makde dir
makedirs() #make a dir and any intermediate dir
rmdir() #remove an empty dir
removedirs() #move all empty dirs
remov() #remove a file
rename() #rename a file
renames() #rename a file , creatin intermediate dir
GO TO TOC
Methods of shutil module
Like os, shutil also provide handy methods to operate
with file system, some of the methods listed below.
rmtree() #remove a dir and all its contents
move() #move a file or a dir
copytree() #copy dir and all its contents using copy2
copyfile() #copy a file’s contents
copy() #copy file preserving file permissions
copy2() #copy a with file properties
GO TO TOC
Explore more of os.path module
dirname() #get path up to the final dir/file in the path
basename() #get final dir/file in the path
split() #split path into dir and filename
splitext() #split path to get file extension
splitdrive() #split path to get a drive name
isabs() #is a path absolute?
abspath() #convert path to absolute
GO TO TOC
sys module
list attributes of sys module
import sys for line in dir(sys): print (line) sys.version #python version sys.copyright #copyright sys.executable #python.exe path where is your interpreter sys.builtin_module_names for bm in dir(sys.builtin_module_names): print( bm) sys.modules #lists all modules sys.modules.has_key("time") #check module key exists sys.modules.has_key("gtk") sys.platform() #what is the platform sys.exit() #exit code sys.stderr.write("Something wrong") sys.stderr.flush() #flush stderr buffer argument handling sys.argv #argument list
import sys def argcheck(): if (len(sys.argv < 3)) : sys.stderr.write("E: usage " + sys.argv[0] + "<filename><word>" sys.stderr.flush() exit(2) else: filename = sys.argv[1] needle = sys.argv[2] counter = 0 file_handle = open(filename) for line in file_handle.readlines(): words = line.split(" ") for word in words: if(word == words[-1]): word = word[:-1] #remove \n which is at the end if word == needle: counter += 1 print "needle count :", counter
GO TO TOC
Packages and Modules
Packages are modules that contain other modules.
Packages are generally implemented as directories containing special __init__.py
__init__.py is executed when the packages is imported
packages can contain sub packages which again contians __init__.py
Modules
Modules are generally files, check the type of sys module.
import sys type(sys)
how python imports module
it looks the file structure recursively in sys.path
sys.path is list of directories
Basic package structure
path_entry/ #must be in sys.path my_package/ #package root __init__ #package init , makes package as module
unload imported images
if 'myModule' in sys.modules: del sys.modules["myModule"]
If your module imports it’s own submodules, you may need to delete those too.
Something like
[del(sys.modules[mod] for mod in sys.modules.keys() if mod.startswith(‘myModule.’)]
GO TO TOC
Generators
Generator generates and iterable object
def num_range(): for n in range(20): print(n) num_range() output: 0,1,2,3,4,5,6,7,8,9,10 .....19
range(start,end) – generates numbers from start to end -1.
end is not inclusive.
Now lets create range object which generates number including the end.
Ex 1: our range which is 0 ~ 25 inclusive_range()
class inclusive_range: def __init__(self,*args): #list to emulate range(start,end,step) numargs = len(args) if numargs < 1: raise TypeError("requires at least one argument") elif numargs == 1: self.stop = args[0] self.start = 0 self.step = 1 elif numargs == 2: (self.start, self.stop) = args self.step = 1 elif numargs == 3: self.start, self.stop,self.step = args else: raise TypeError("Expected atmost 3 arguments got {}".format(numargs)) def __iter__(self): #makes object iterable i = self.start while i <= self.stop: yield i # this makes the method generator i += self.step #if return is used instead of yield function will return the value and start at the beginning #while yield , the execution starts from where it has returned value print( " _" *20) print (" single argument ") for r in inclusive_range(25): print( r) print( " _" *20 ) print (" two arguments ") for r in inclusive_range(5,25): print( r) print (" _" *20 ) print( " three arguments ") for r in inclusive_range(1,25,2): print (r) print( " _" *20 ) print (" no argument ") for r in inclusive_range(): print( r) print( " _" *20 ) print (" more than 3 arguments ") for r in inclusive_range(1,25,2,4): print( r)
GO TO TOC
Introduction to decorators
Decorators wrapper functions, they acts like syntactic sugar.
what we can do with decorators
to existing functions
add functionality
modify its behaviour
perform setup/tear down
diagnostics (timing etc)
#!/user/bin env python def divide(x,y): return x/y print( divide(10,2)) #print (divide(10,0)) # raises ZeroDivisionError: exception #one way to handle the exception is try-except block def divide(x,y): try: return x/y except Exception as e: print ("Exception raised : ", str(e)) return "N/A" print( "Simple divison") print (divide(10,0)) output: 5.0 Simple divide Exception raised : division by zero N/A
#imagine if try-except block is used in some 200 places,
if we want to make some changes the all the places need to be changed
#handling exception in elegant way def handle_exception(func): def inner(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: print ("Exception raised : ", str(e)) return "N/A" return inner def divide1(x,y): return x/y # divide without decorator notation value = handle_exception(divide1) print( value(8,2)) print( value(8,0)) #short hand of handle_exception print (" using decorator") # @handle_exception is equivalent to handle_exception(divide2) @handle_exception def divide2(x,y): return x/y print( divide2(8,2)) print( divide2(8,0))
GO TO TOC