1. PYTHON PROGRAMMING - OOPS CONCEPTS, CLASSES &
OBJECTS, OPERATOR OVERLOADING, FILE HANDLING,
EXCEPTION HANDLING AND REGULAR EXPRESSIONS
Ms. V.SAROJA
Assistant Professor
Department of Computer Science and Engineering
SRM Institute of Science and Technology, Chennai
2. Iterators
• Object that contains countable number of values
• Iterated or traversed through all the values
• Implements iterator which consists of iter() and
next()
• iter() used on lists, it returns
the list_iterator object.
• iter() is called on initialization of an iterator
• next() is used to access the elements from
the iterator one at a time.
3. Eg.
num=[5,2,9,7,3]
for i in num:
print(i)
ite=iter(num)
print(ite)
O/p:
5
2
9
7
3
<list_iterator object at 0x7fa1d80e8130>
9. Generators
• Create python iterators
• generator-function is defined like a normal
function, but whenever it needs to generate a
value, use yield keyword rather than return.
• If the body of a def contains yield, the function
automatically becomes a generator function
• Generator functions return a generator object.
• Generator objects are used either by calling the
next method on the generator object or using the
generator object in a “for in” loop
10. Eg.
def vowels():
yield ‘a’
yield ‘e’
yield ‘i’
yield ‘o’
yield ‘u’
(or)
def vowels():
yield ‘a’, ’e’, ’i’, ’o’, ’u’
for i in vowels():
print(i)
o/p:
a
e
i
o
u
(or)
('a', 'e‘, ‘i‘, ‘o’, ‘u’)
12. Eg.
def fib(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
x = fib(5)
print(type(x))
print(x.__next__())
print(x.__next__())
print(x.__next__())
print(x.__next__())
print(x.__next__())
(or)
for i in x:
print(i)
print("nUsing for in loop")
for i in fib(5):
print(i)
14. • Advantages of using generators
– Excellent memory efficiency
– Lazy evaluation
– yield() returns the value without affecting or
exiting the function
– return a sequence of data in iterator format where
we need to iterate over the sequence to use the
data as they won’t store the entire sequence in
the memory
15. Iterator Generator
Class is used to implement an iterator Function is used to implement a generator.
Local Variables aren’t used here. All the local variables before the yield function are stored.
Iterators are used mostly to iterate or convert other objects
to an iterator using iter()
function.
Generators are mostly used in loops to generate an iterator
by returning all the values in the loop without affecting the
iteration of the loop
Iterator uses iter() and next() functions Generator uses yield keyword
Every iterator is not a generator
Less memory efficient
Complex implementation
Every generator is an iterator
More memory efficient
Simple to code
16. Handling text files in python
• 2 types of files
– Text files
– Binary files
• Access modes manage the type of operations
possible in opened file. How file will be used once
it is opened
• also define location of file handle in file
• file handle – like cursor
• Define from where data to be read or written in
file
18. • Opening a File
fileobject=open(r“filename”, “accessmode”)
•r is placed before filename to prevent the characters in
filename string to be treated as special character.
•Eg. temp in the file name, then t is treated as the tab
character and error is raised of invalid address
•r makes the string raw, that is, it tells that the string is
without any special characters. The r can be ignored if the
file is in same directory and address is not being placed
19. Eg.
f1=open(r“fn1.txt”, “r”)
f2=open(r“fn2.txt”, “w+”)
f3=open(“fn3.txt”, “a”)
Closing a file
• close() function closes the file and frees the memory space
acquired by that file. It is used at the time when the file is no longer
needed or if it is to be opened in a different file mode.
File_object.close()
• Eg.
file1 = open("MyFile.txt","a")
file1.close()
20. • Reading from a file
There are three ways to read data from a text file.
• read() : Returns the read bytes in form of a string. Reads n bytes, if
no n specified, reads the entire file.
fo.read()
• readline() : Reads a line of the file and returns in form of a string.
For specified n, reads at most n bytes. However, does not reads
more than one line, even if n exceeds the length of the line.
fo.readline()
• readlines() : Reads all the lines and return them as each line a string
element in a list.
fo.readlines()
25. • Writing to a file
• If a file doesn’t exist, a new file is created and contents are written to file
• If a file already exists, contents are overwritten.
There are two ways to write data to a text file:
• write() : writes the string into newly created file or overwrite the existing
file contents.
fo.write(str)
• writelines() : used to write sequence datatypes(string, list, tuple etc.) in
an opened file. For a list of string elements, each string is inserted in the
text file.Used to insert multiple strings at a single time.
fo.writelines(L) for L = [str1, str2, str3]
fo.writelines(str1, str2, str3)
• For storing numeric values, convert it into string using str() or write in
quotes
26. with open('new.txt','w') as f:
f.write('hellon')
f.write('12345n')
l=['hin', 'hiiiin', 'heyyyn']
f.writelines(l)
o/p:
new.txt
----------
hello
12345
hi
hiiii
heyyy
27. Appending data to file
• In order to append a new line to the existing file, open the file in
append mode, by using either 'a' or 'a+' as the access mode
• Append Only (‘a’): Open the file for writing. The file is created if it
does not exist. The file handle is positioned at the end of the file.
The data being written will be inserted at the end, after the existing
data.
• Append and Read (‘a+’): Open the file for reading and writing. The
file is created if it does not exist. The file handle is positioned at the
end of the file. The data being written will be inserted at the end,
after the existing data.
Eg.
with open('new.txt',‘a') as f:
f.write('hellon')
28. Working with 2 files at same time:
f1='fl1.txt'
f2='fl2.txt'
with open(f1,'r') as reader, open(f2,'w') as writer:
str=reader.readlines()
print(str)
writer.writelines(reversed(str))
30. Using fileinput module to read contents of 2 files:
import fileinput as fi
with fi.input(files=['fl1.txt', 'fl2.txt']) as f:
for l in f:
print(l)
o/p:
hi
hello
sss
aaa
ccc
ccc
aaa
sss
hello
hi
31. Without using fileinput module to read contents of 2 files:
with open('fl1.txt', mode='w') as f:
f.write('In file1 - line 1: Hellon')
f.write('In file1 - line 2: Worldn')
with open('fl2.txt', mode='w') as f:
f.write('In file2 - line 1: Hain')
f.write('In file2 - line 2: Welcomen')
for fo in ['fl1.txt', 'fl2.txt']:
with open(fo, 'r') as f:
for l in f:
print(l)
32. o/p:
In file1 - line 1: Hello
In file1 - line 2: World
In file2 - line 1: Hai
In file2 - line 2: Welcome
>>>
33. Modules
• In Python, Modules are simply files with the “.py” extension
containing Python code that can be imported inside another Python
Program.
• Module to be the same as a code library or a file that contains a set
of functions that you want to include in your application.
• With the help of modules, we can organize related functions,
classes, or any code block in the same file. So, It is considered a best
practice while writing bigger codes for production-level projects in
Data Science is to split the large Python code blocks
into modules containing up to 300–400 lines of code.
• The module contains the following components:
– Definitions and implementation of classes,
– Variables, and
– Functions that can be used inside another program.
34. • Creation of python modules:
– To create a module, we have to save the code in a file with the file
extension “.py”
– Then, the name of the Python file becomes the name of the module
• use of Python Modules:
– use the import keyword, to add all methods from module
– use the from keyword, to get only a few or specific methods or
functions from a module.
– Use a function from a module use . Operator with syntax
• module_name.function_name
• The module not only contain functions but also contain variables of
all types such as arrays, dictionaries, objects, etc
35. • Eg.
mymodule.py
person = { "name": “AAA", "age": 19, "country":
"India" "education”: “IIT" }
import mymodule
a = mymodule.person["age"]
b = mymodule.person["education"]
c = mymodule.person["country"]
print(a)
36. • Rename a Python Module:
– To rename the module name, create an alias when
import a module, with the help of the as keyword
Eg.
import Mymodule as m
a =m.person["age"]
b =m.person["education"]
c =m.person["country"]
print(a)
37. Eg.
Mymodule.py
person = { "name": “AAA", "age": 19, "country": "India" "education”:
“IIT" }
def welcome(name):
print("Hello, " + name +“ welcome ")
from Mymodule import welcome
welcome(‘Kumar’)
from Mymodule import person
print (person["age"])
42. from fibo import fib1 as fibonacci
fibonacci(200)
o/p:
0
1
2
4
8
16
32
64
128
43. • Advantages of using modules
• Reusability
– Makes the code reusable
• Simplicity
– The module focuses on a small proportion of the
problem, rather than focusing on the entire problem
• Scoping
– A separate namespace is defined by a module that
helps to avoid collisions between identifiers.
45. • Frequently used buildin modules
– Math
– Statistics
• Some mathematical functions defined in math module
– Trigonometric functions – sin, cos, tan,
– Representation functions – ceil, floor, gcd, isinfinite, isinf, isnan, factorial,..
– Logarithmic functions - log, log10, ..
– Angle conversion functions – degrees(), radians()
• In addition, two mathematical constants- pi and e are also defined
in this module
• Some functions of the math module such as math.log(),
math.log10(), math.pow(). math.sqrt(), math.exp(), math.ceil(),
math.floor(), etc.
47. Statistics module:
• The statistics module provides functions to mathematical
statistics of numeric data.
• Some of the statistical functions are defined in this module
– Mean - returns the arithmetic mean of the numbers present in a
list.
– Median - returns the middle value of numeric data present in a
list.
– Mode - returns the most common data point present in the list.
– Standard Deviation - returns the standard deviation on a given
sample in the form of a list.
51. class Sample:
def __init__(self,a,b):
print('in init')
self.a=a
self.b=b
def method1(self):
print('hi')
print(self.a)
print(self.b)
s1=Sample(5,6)
s2=Sample(50,60)
print(type(s1))
Sample.method1(s1)
s2.method1()
O/p:
in init
in init
<class '__main__.Sample'>
hi
5
6
hi
50
60
52. • Every time object is created, new space will be
allocated to that object.
• Heap memory is used to store objects
• Size of object depends on the number of
variables and size of each variables in the class
• Constructor will allocate memory for objects
56. • Namespace is an area where we create and store
object/variable
• 2 types of namespace
– Class namespace
– Object/instance namespace
• 2 types of variables
– Class variables
– Instance variables
• 3 types of methods
– Class methods
– Instance methods
– Static methods
61. Class methods:
• A class method receives the class as an implicit first argument, just like an instance method
receives the instance
• A class method is a method that is bound to the class and not the object of the class.
• They have the access to the state of the class as it takes a class parameter that points to the
class and not the object instance.
• It can modify a class state that would apply across all the instances of the class. For example,
it can modify a class variable that will be applicable to all the instances.
• use @classmethod decorator in python to create a class method
Static methods:
• A static method does not receive an implicit first argument.
• A static method is also a method that is bound to the class and not the object of the class.
• A static method can’t access or modify the class state.
• static methods know nothing about the class state. They are utility-type methods that take
some parameters and work upon those parameters
• use @staticmethod decorator to create a static method in python.
62. OPERATOR OVERLOADING
• giving extended meaning beyond their
predefined operational meaning
• operator + is used to add two integers as well
as join two strings and merge two lists.
• It is achievable because ‘+’ operator is
overloaded by int class and str class.
• The same built-in operator or function shows
different behavior for objects of different
classes is known as Operator Overloading
69. Exception handling
• Exceptions or errors are raised when internal events occur which
changes the normal flow of program
• When syntax are correct, but program will give error result
• 3 types of Error
– Compile time error (syntax error)
– Logical error
– Runtime error
• Compile time errors are easy to find
• Logical errors contains bugs
– Easy to handle
– Testing team can handle bugs
• Runtime errors concerned with users
• Try and except used to catch and handle exceptions in python
• Statements raise/create exceptions kept inside try
70. • Eg.
a=2
b=0
print(a/b)
o/p:
Traceback (most recent call last):
File
"C:/Users/Admin/AppData/Local/Programs/Python/Pyt
hon37-32/eg.py", line 3, in <module>
print(a/b)
ZeroDivisionError: division by zero
75. a=2
b=3
c=int(input('Enter a number'))
print(c)
o/p:
Enter a number a
Traceback (most recent call last):
File
"C:/Users/Admin/AppData/Local/Programs/Python/Python
37-32/eg.py", line 3, in <module>
c=int(input('Enter a number'))
ValueError: invalid literal for int() with base 10: 'a'
78. a=2
b=3
try:
print(‘resource open’)
print(a/b)
c=int(input('Enter a number'))
print(c)
except Exception as e:
print('try to divide a number by 0', e)
finally:
print(‘resource closed’)
o/p:
resource open
0.6666666666666666
Enter a number a
try to divide a number by 0 invalid literal for int() with base 10: 'a'
resource closed
80. a=2
b=3
try:
print('resource open')
print(a/b)
c=int(input('Enter a number'))
print(c)
except ZeroDivisionError:
print('try to divide a number by 0')
except ValueError:
print('invalid datatype')
finally:
print('resource closed')
o/p:
resource open
0.6666666666666666
Enter a number a
invalid datatype
resource closed
81. Try with else clause
• Use else with try-except block which must be present after all except clauses
• If no exception is raised, else block will be executed
Eg;
def funct(a,b):
try:
c=((a+b)/(a-b))
except ZeroDivisionError:
print(‘divide by 0 error’)
else:
print(c)
funct(2.0,3.0)
funct(3.0,3.0)
o/p:
-5.0
divide by 0 error
82. Raising exception
• Programmer can create specific exception using raise
Eg
try:
raise NameError(‘hi there’)
except NameError:
print(‘name error occur’)
raise
o/p:
name error occur
Traceback (most recent call last):
File "C:/Users/Admin/AppData/Local/Programs/Python/Python37-32/eg.py",
line 2, in <module>
raise NameError('hi there')
NameError: hi there
85. User defined exception
• We can create custom exceptions by extending Exception
Eg
class VotingEligible(Exception):
def __init__(self,msg):
self.msg=msg
try:
age=12
if age<18:
raise VotingEligible("NOT ELIGIBLE")
except VotingEligible as e:
print(e.msg)
else:
print('Eligible: age >= 18')
finally:
print('end')
86. STRINGS AND REGULAR EXPRESSIONS
• A RegEx, or Regular Expression, is a sequence of characters that forms a search
pattern.
• RegEx can be used to check if a string contains the specified search pattern.
RegEx Module
• Python has a built-in package called re, which can be used to work with Regular
Expressions.
import re
• Once re module has imported, start using regular expressions
import re
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
print(x)
o/p:
<re.Match object; span=(0, 17), match='The rain in Spain'>
87. RegEx Functions
• The re module offers a set of functions that allows us
to search a string for a match:
• Function Description
• findall Returns a list containing all matches
• search Returns a Match object if there is a
match anywhere in the string
• split Returns a list where the string has been split
at each match
• sub Replaces one or many matches with a string
88. Meta characters
• Metacharacters are characters with a special meaning
• They are used in the functions of module re
MetaCharacters Description
Used to drop the special meaning of character following it
[] Represent a character class
^ Matches the beginning
$ Matches the end
. Matches any character except newline
| Means OR (Matches with any of the characters separated by it.
? Matches zero or one occurrence
* Any number of occurrences (including 0 occurrences)
+ One or more occurrences
{} Indicate the number of occurrences of a preceding regex to match.
() Enclose a group of Regex
89. Special Sequences
• A special sequence is a followed by one of the characters in the list below, and has a special
meaning:
Character Description
• A Returns a match if the specified characters are at the beginning of the string
• b Returns a match where the specified characters are at the beginning or at the end of a word
(the "r" in the beginning is making sure that the string is being treated as a "raw string“)
• B Returns a match where the specified characters are present, but NOT at the beginning (or at
the end) of a word
(the "r" in the beginning is making sure that the string is being treated as a "raw string“)
• d Returns a match where the string contains digits (numbers from 0-9)
• D Returns a match where the string DOES NOT contain digits
• s Returns a match where the string contains a white space character
• S Returns a match where the string DOES NOT contain a white space character
• w Returns a match where the string contains any word characters (characters from a to Z,
digits from 0-9, and the underscore _ character)
• W Returns a match where the string DOES NOT contain any word characters
• Z Returns a match if the specified characters are at the end of the string
90. re.findall()
• Return all non-overlapping matches of pattern in string, as a list of strings. The
string is scanned left-to-right, and matches are returned in the order found.
• Example: Finding all occurrences of a pattern
import re
string = ‘Hello my Number is 123456789 and my friend number is 987654321’
regex = 'd+‘
match = re.findall(regex, string)
print(match)
str=‘hello world’
x=re.findall(‘abc’, str)
print(x)
o/p:
['123456789', '987654321']
[ ]
91. search()
• The search() function searches the string for a match, and returns a Match object if
there is a match else None will be returned
• If there is more than one match, only the first occurrence of the match will be
returned
• Example: Search for the first white-space character in the string
import re
txt = “hello world“
st=“welcome”
x = re.search("s", txt)
y = re.search("s", st)
print("The first white-space character is located in position:", x.start())
print(y)
o/p:
The first white-space character is located in position: 5
None
92. split()
• The split() function returns a list where the string has been split at each match
• The number of occurrences can be controlled by specifying the maxsplit parameter
• Example: Split at each white-space character and also split at only first occurrence
import re
txt = ’hello welcome good morning’
x = re.split("s", txt)
y = re.split("s", txt, 1)
print(x)
print(y)
o/p:
['hello', 'welcome', 'good', 'morning']
['hello', 'welcome good morning']
93. sub()
• The sub() function replaces the matches with the text of your choice
• the number of replacements can be controlled by specifying
the count parameter
• Example: Replace every white-space character with the symbol & and
replace first 2 occurences with *
import re
txt = ’hello welcome good morning’
x = re.sub("s", “&", txt)
y = re.sub("s", “*", txt, 2)
print(x)
print(y)
o/p:
hello&welcome&good&morning
Hello*welcome*good morning
94. compile()
• Regular expressions are compiled into pattern objects,
which have methods for various operations such as
searching for pattern matches or performing string
substitutions.
Eg.
import re
p = re.compile('[a-e]')
print(p.findall(“hello welcome good morning"))
o/p:
[‘e’, ‘e’, ‘c’, ‘e’, ‘d’]
95. • re.escape()
• Returns string with all non-alphanumerics backslashed, this
is useful if you want to match an arbitrary literal string that
may have regular expression metacharacters in it.
Eg.
import re
print(re.escape("This is Awesome even 1 AM"))
print(re.escape("I Asked what is this [a-9], he said t ^WoW"))
o/p:
This is Awesome even 1 AM
I Asked what is this [a-9], he said ^WoW
96. Match Object
• A Match object contains all the information about the search and
the result and if there is no match found then None will be
returned.
• Some of the commonly used methods and attributes of the match
object.
Attributes:
re - returns the regular expression passed
string - returns the string passed
Methods:
• start() method returns the starting index of the matched substring
• end() method returns the ending index of the matched substring
• span() method returns a tuple containing the starting and the
ending index of the matched substring