SlideShare a Scribd company logo
1 of 96
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
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.
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>
Eg.
num=[5,2,9,7]
ite=iter(num)
print(ite.__next__())
print(ite.__next__())
print(next(ite))
print(next(ite))
print(ite.__next__())
O/P:
5
2
9
7
Traceback (most recent call last):
File "<string>", line 6, in <module>
StopIteration
class EvenNumbers:
def __iter__(self):
self.num = 0
return self
def __next__(self):
next_num = self.num
self.num += 2
return self.num
evens = EvenNumbers()
even_iter = iter(evens)
print(next(even_iter))
print(next(even_iter))
for i in even_iter:
print(i)
O/p:
2
4
6
8
10
12
14
…..
…..
…..
………..
(Infinite loop)
class EvenNumbers:
def __iter__(self):
self.num = 0
return self
def __next__(self):
if(self.num < 10):
next_num = self.num
self.num += 2
return self.num
else:
raise StopIteration
evens = EvenNumbers()
even_iter = iter(evens)
for n in even_iter:
print(n)
O/p:
2
4
6
8
10
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
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’)
Eg.
def PowerTwo(max):
n=1
while n<=max:
yield 2**n
n+=1
a=PowerTwo(5)
print(a)
for i in a:
print(i)
O/p:
<generator object PowerTwo at 0x022C07B0>
2
4
8
16
32
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)
O/p:
<class 'generator'>
0
1
1
2
3
Using for in loop
0
1
1
2
3
• 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
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
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
• Access modes
– Read only(r)
– Read and write(r+)
– Write only(w)
– Write and read(w+)
– Append only(a)
– Append and read(a+)
– Binary mode(‘b’)
– Default mode(‘t’)
• 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
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()
• 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()
Sample.txt
Hello
Welcome
Hi
Good morning
Eg.
f=open(“sample.txt","r")
content=f.read()
print(content)
O/p:
Hello
Welcome
Hi
Good morning
f=open("my.txt","r")
print(f.readline())
f=open("my.txt","r")
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
o/p:
Hello
Welcome
Hi
Good morning
f=open("my.txt","r")
print(f.readline(),end='')
print(f.readline(), end='')
o/p:
Hello
Welcome
f=open("my.txt","r")
print(f.readline(3),end='')
o/p:
Hel
f=open("my.txt","r")
print(f.readlines())
o/p:
['Hellon', 'Welcomen', 'Hin', 'Good morningn']
try:
f=open(“sample.txt","r")
content=f.read()
print(content)
finally:
f.close()
• Alternate way to open file using with
• No need to close file every time file opened
with open(“sample.txt","r") as f:
content=f.read()
print(content)
• 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
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
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')
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))
o/p:
fl1.txt
---------
hi
hello
sss
aaa
ccc
fl2.txt
---------
ccc
aaa
sss
hello
hi
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
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)
o/p:
In file1 - line 1: Hello
In file1 - line 2: World
In file2 - line 1: Hai
In file2 - line 2: Welcome
>>>
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.
• 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
• 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)
• 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)
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"])
fibo.py
def fib1(n):
a,b=0,1
while a<n:
print(a)
a=b
b=a+b
print()
def fib2(n):
r=[]
a,b=0,1
while a<n:
r.append(a)
a=b
b=a+b
return r
import fibo
fibo.fib1(100)
re=fibo.fib2(100)
for i in re:
print(i)
f=fibo.fib1
f(200)
o/p:
0
1
2
4
8
16
32
64
0
1
2
4
8
16
32
64
0
1
2
4
8
16
32
64
128
from fibo import fib1,fib2
(or)
from fibo import *
fib1(200)
o/p:
0
1
2
4
8
16
32
64
128
from fibo import fib1 as fibonacci
fibonacci(200)
o/p:
0
1
2
4
8
16
32
64
128
• 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.
>>> help('modules')
Please wait a moment while I gather a list of all available modules...
PIL bdb hyperparser rpc
__future__ binascii idle run
__main__ binhex idle_test runpy
_abc bisect idlelib runscript
_ast browser imaplib sched
_asyncio builtins imghdr scipy
_bisect bz2 imp scrolledlist
_blake2 cProfile importlib search
_bootlocale calendar inspect searchbase
_bz2 calltip io searchengine
_codecs calltip_w iomenu secrets
_codecs_cn cgi ipaddress select
_codecs_hk cgitb itertools selectors
_codecs_iso2022 chunk json setuptools
_codecs_jp cls keyword shelve
_codecs_kr cmath kiwisolver shlex
_codecs_tw cmd lab1 shutil
_collections code lab2 sidebar
_collections_abc codecontext lab2new signal
_compat_pickle codecs lib2to3 site
_compression codeop linecache six
_contextvars collections list smtpd
_csv colorizer locale smtplib
_ctypes colorsys logging sndhdr
_ctypes_test compileall lzma socket
_datetime concurrent macosx socketserver
_decimal config macpath sqlite3
_dummy_thread config_key mailbox squeezer
_elementtree configdialog mailcap sre_compile
_functools configparser main sre_constants
_hashlib constant mainmenu sre_parse
_heapq contextlib marshal ssl
_imp contextvars mat_addsub stackviewer
_io copy math stat
_json copyreg matplotlib statistics
_locale crypt matrix_add statusbar
_lsprof csv mimetypes str
_lzma ctypes mmap string
_markupbase curses modulefinder stringprep
_md5 cycler msilib struct
_msi dataclasses msvcrt subprocess
_multibytecodec datetime multicall sunau
_multiprocessing dateutil multiprocessing symbol
_opcode dbm netrc symtable
_operator debugger nntplib sys
_osx_support debugger_r nt sysconfig
_overlapped debugobj ntpath tabnanny
_pickle debugobj_r nturl2path tarfile
_py_abc decimal numbers telnetlib
• 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.
Eg.
>>> import math
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
>>> math.radians(30)
0.5235987755982988
>>> math.degrees(math.pi/6)
29.999999999999996
>>> math.sqrt(4)
2.0
>>> math.pow(2,3)
8.0
>>> math.ceil(1.414)
2
>>> math.floor(1.414)
1
>>> math.log(2)
0.6931471805599453
>>> math.log10(2)
0.3010299956639812
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.
Eg.
>>> import statistics
>>> statistics.mean([2,5,6,9])
5.5
>>> statistics.median([1,2,3,7,8,9])
5.0
>>> statistics.median([1,2,3,8,9])
3.0
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6])
2
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153
OOPS
• Python supports both functional programming
and object oriented programming
• Every object has attributes(properties) and
behaviour (methods,actions)
• OOPS concepts
– Object
– Class
– Encapsulation
– Abstraction
– Polymorphism
– Inheritance
Eg
class Sample:
def method1(self):
print('hi')
s1=Sample()
s2=Sample()
print(type(s1))
Sample.method1(s1)
s2.method1()
O/p:
<class '__main__.Sample'>
hi
hi
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
• 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
class Sample:
pass
s1=Sample()
s2=Sample()
print(id(s1))
print(id(s2))
o/p:
38599952
38600176
class Sample:
def __init__(self, name, age):
self.name=name
self.age=age
def update(self, age):
self.age=age
def compare(self, s1):
if self.age == s1.age:
return True
else:
return False
s1=Sample(‘John’,25)
s2=Sample(‘Joe’,28)
print(s1.name)
print(s2.name)
print(s1.age)
print(s2.age)
s1.age=30
print(s1.age)
print(s2.age)
Sample.update(s2,30)
if s1.compare(s2):
print('same')
else:
print('not same‘)
o/p
John
Joe
25
28
30
28
same
• 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
class Sample:
type='human'
def __init__(self):
self.name='John'
self.age=28
def update(self):
self.age=30
def compare(self, s1):
if self.age == s1.age:
return True
else:
return False
s1=Sample()
s2=Sample()
print(s1.name, s1.age, s1.type)
print(s2.name, s2.age, s2.type)
s1.age=30
Sample.update(s2)
Sample.type='animal'
print(s1.name, s1.age, s1.type)
print(s2.name, s2.age, s2.type)
o/p:
John 28 human
John 28 human
John 30 animal
John 30 animal
class Sample:
type='human'
school='secondary'
def __init__(self):
self.name='John'
self.age=28
@classmethod
def getinfo(cls):
print(cls.type, cls.school)
@staticmethod
def info():
print('common for all')
s1=Sample()
s2=Sample()
print(s1.name, s1.age, s1.type)
print(s2.name, s2.age, s2.type)
Sample.getinfo()
Sample.info()
o/p:
John 28 human
John 28 human
human secondary
common for all
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.
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
Eg.
a=2
b=3
print(a+b)
s1=‘aaa’
s2=‘bbb’
print(s1+s2)
print(3 * 4)
print(‘hello’*4)
l1=[2,4.5, ‘hi’]
l2=[3,6.8, ‘hello’]
print(l1+l2)
class A:
def __init__(self, a,st):
self.a=a
self.st=st
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
ob1 = A(2,'Hello')
ob2 = A(3,'World')
if(ob1.__gt__(ob2)):
print('ob1 is greater than ob2')
else:
print('ob2 is greater than ob1')
if(ob1.st.__gt__(ob1.st)):
print('s1 is greater than s2')
else:
print('s2 is greater than s1')
o/p:
ob2 is greater than ob1
s2 is greater than s1
• Python magic methods or special functions for operator overloading
• Binary operators:
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
• Comparison Operators :
< __LT__(SELF, OTHER)
> __GT__(SELF, OTHER)
<=__LE__(SELF, OTHER)
>=__GE__(SELF, OTHER)
==__EQ__(SELF, OTHER)
!= __NE__(SELF, OTHER)
• Unary Operators :
– __NEG__(SELF, OTHER)
+ __POS__(SELF, OTHER)
~ __INVERT__(SELF, OTHER)
• Assignment Operators :
-= __ISUB__(SELF, OTHER)
+= __IADD__(SELF, OTHER)
*= __IMUL__(SELF, OTHER)
/= __IDIV__(SELF, OTHER)
//= __IFLOORDIV__(SELF, OTHER)
%=__IMOD__(SELF, OTHER)
**= __IPOW__(SELF, OTHER)
>>= __IRSHIFT__(SELF, OTHER)
<<= __ILSHIFT__(SELF, OTHER)
&=__IAND__(SELF, OTHER)
|= __IOR__(SELF, OTHER)
^= __IXOR__(SELF, OTHER)
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
• 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
a=2
b=0
try:
print(a/b)
except Exception:
print(“try to divide a number by 0”)
o/p:
try to divide a number by 0
a=2
b=0
try:
print(a/b)
except Exception as e:
print(“try to divide a number by 0”, e)
o/p:
try to divide a number by 0 division by zero
finally
• Always executed after try-except block
• Whether exception raised or not, finally block always
executed.
Eg:
try:
k=5/0
print(k)
except ZeroDivisionError:
print(“error”)
finally:
print(‘always executed’)
a=2
b=0
try:
print(‘resource open’)
print(a/b)
except Exception as e:
print('try to divide a number by 0', e)
finally:
print(‘resource closed’)
o/p:
resource open
try to divide a number by 0 division by zero
resource closed
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'
l=[1,2,3,4,5]
try:
print('second item=', l[1])
print('sixth item=', l[5])
except Exception as e:
print('error=', e)
o/p:
second item= 2
error= list index out of range
a=2
b=0
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
try to divide a number by 0 division by zero
resource closed
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
Catching specific exceptions
try:
#stmts
except IndexError:
#stmts
except ValueError:
#stmts
except ZeroDivisionError:
#stmts
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
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
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
Declaring multiple exceptions with
except clause
• try block throws multiple exceptions
• declare multiple exceptions with except clause
try:
#stmts
except(<excep1>, <excep2>, <excep3>,……. <excep n>)
#stmts
else:
#stmts
Eg.
try:
a=10/0
except(ArithmeticError, IOError):
print(‘arithmetic exception’)
else:
print(‘success’)
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')
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'>
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
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
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
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']
[ ]
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
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']
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
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’]
• 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
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

More Related Content

What's hot

PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...Maulik Borsaniya
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| FundamentalsMohd Sajjad
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way PresentationAmira ElSharkawy
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming LanguageTahani Al-Manie
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPTShivam Gupta
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming LanguageDipankar Achinta
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019Samir Mohanty
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming LanguageMansiSuthar3
 

What's hot (20)

Python
PythonPython
Python
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Seminar PPT
Python Seminar PPTPython Seminar PPT
Python Seminar PPT
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Python Presentation
Python PresentationPython Presentation
Python Presentation
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to Python programming Language
Introduction to Python programming LanguageIntroduction to Python programming Language
Introduction to Python programming Language
 
Python made easy
Python made easy Python made easy
Python made easy
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Python Basics
Python Basics Python Basics
Python Basics
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python Basics
Python BasicsPython Basics
Python Basics
 

Similar to Python programming

File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleRohitKurdiya1
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4Prerna Sharma
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in pythonLifna C.S
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C ProgrammingRavindraSalunke3
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
Bt0067 c programming and data structures2
Bt0067 c programming and data structures2Bt0067 c programming and data structures2
Bt0067 c programming and data structures2Techglyphs
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Filesprimeteacher32
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 

Similar to Python programming (20)

File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
Unit V.pptx
Unit V.pptxUnit V.pptx
Unit V.pptx
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
File_Management_in_C
File_Management_in_CFile_Management_in_C
File_Management_in_C
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
Programming in C
Programming in CProgramming in C
Programming in C
 
File mangement
File mangementFile mangement
File mangement
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
files.pptx
files.pptxfiles.pptx
files.pptx
 
Unit-4 PPTs.pptx
Unit-4 PPTs.pptxUnit-4 PPTs.pptx
Unit-4 PPTs.pptx
 
Bt0067 c programming and data structures2
Bt0067 c programming and data structures2Bt0067 c programming and data structures2
Bt0067 c programming and data structures2
 
Python File functions
Python File functionsPython File functions
Python File functions
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
working with files
working with filesworking with files
working with files
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Handout#01
Handout#01Handout#01
Handout#01
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 

Python programming

  • 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>
  • 5. class EvenNumbers: def __iter__(self): self.num = 0 return self def __next__(self): next_num = self.num self.num += 2 return self.num evens = EvenNumbers() even_iter = iter(evens) print(next(even_iter)) print(next(even_iter)) for i in even_iter: print(i)
  • 7. class EvenNumbers: def __iter__(self): self.num = 0 return self def __next__(self): if(self.num < 10): next_num = self.num self.num += 2 return self.num else: raise StopIteration evens = EvenNumbers() even_iter = iter(evens) for n in even_iter: print(n)
  • 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’)
  • 11. Eg. def PowerTwo(max): n=1 while n<=max: yield 2**n n+=1 a=PowerTwo(5) print(a) for i in a: print(i) O/p: <generator object PowerTwo at 0x022C07B0> 2 4 8 16 32
  • 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
  • 17. • Access modes – Read only(r) – Read and write(r+) – Write only(w) – Write and read(w+) – Append only(a) – Append and read(a+) – Binary mode(‘b’) – Default mode(‘t’)
  • 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()
  • 24. try: f=open(“sample.txt","r") content=f.read() print(content) finally: f.close() • Alternate way to open file using with • No need to close file every time file opened with open(“sample.txt","r") as f: content=f.read() print(content)
  • 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"])
  • 38. fibo.py def fib1(n): a,b=0,1 while a<n: print(a) a=b b=a+b print() def fib2(n): r=[] a,b=0,1 while a<n: r.append(a) a=b b=a+b return r
  • 39. import fibo fibo.fib1(100) re=fibo.fib2(100) for i in re: print(i) f=fibo.fib1 f(200)
  • 41. from fibo import fib1,fib2 (or) from fibo import * fib1(200) o/p: 0 1 2 4 8 16 32 64 128
  • 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.
  • 44. >>> help('modules') Please wait a moment while I gather a list of all available modules... PIL bdb hyperparser rpc __future__ binascii idle run __main__ binhex idle_test runpy _abc bisect idlelib runscript _ast browser imaplib sched _asyncio builtins imghdr scipy _bisect bz2 imp scrolledlist _blake2 cProfile importlib search _bootlocale calendar inspect searchbase _bz2 calltip io searchengine _codecs calltip_w iomenu secrets _codecs_cn cgi ipaddress select _codecs_hk cgitb itertools selectors _codecs_iso2022 chunk json setuptools _codecs_jp cls keyword shelve _codecs_kr cmath kiwisolver shlex _codecs_tw cmd lab1 shutil _collections code lab2 sidebar _collections_abc codecontext lab2new signal _compat_pickle codecs lib2to3 site _compression codeop linecache six _contextvars collections list smtpd _csv colorizer locale smtplib _ctypes colorsys logging sndhdr _ctypes_test compileall lzma socket _datetime concurrent macosx socketserver _decimal config macpath sqlite3 _dummy_thread config_key mailbox squeezer _elementtree configdialog mailcap sre_compile _functools configparser main sre_constants _hashlib constant mainmenu sre_parse _heapq contextlib marshal ssl _imp contextvars mat_addsub stackviewer _io copy math stat _json copyreg matplotlib statistics _locale crypt matrix_add statusbar _lsprof csv mimetypes str _lzma ctypes mmap string _markupbase curses modulefinder stringprep _md5 cycler msilib struct _msi dataclasses msvcrt subprocess _multibytecodec datetime multicall sunau _multiprocessing dateutil multiprocessing symbol _opcode dbm netrc symtable _operator debugger nntplib sys _osx_support debugger_r nt sysconfig _overlapped debugobj ntpath tabnanny _pickle debugobj_r nturl2path tarfile _py_abc decimal numbers telnetlib
  • 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.
  • 46. Eg. >>> import math >>> math.pi 3.141592653589793 >>> math.e 2.718281828459045 >>> math.radians(30) 0.5235987755982988 >>> math.degrees(math.pi/6) 29.999999999999996 >>> math.sqrt(4) 2.0 >>> math.pow(2,3) 8.0 >>> math.ceil(1.414) 2 >>> math.floor(1.414) 1 >>> math.log(2) 0.6931471805599453 >>> math.log10(2) 0.3010299956639812
  • 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.
  • 48. Eg. >>> import statistics >>> statistics.mean([2,5,6,9]) 5.5 >>> statistics.median([1,2,3,7,8,9]) 5.0 >>> statistics.median([1,2,3,8,9]) 3.0 >>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6]) 2 >>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5]) 1.3693063937629153
  • 49. OOPS • Python supports both functional programming and object oriented programming • Every object has attributes(properties) and behaviour (methods,actions) • OOPS concepts – Object – Class – Encapsulation – Abstraction – Polymorphism – Inheritance
  • 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
  • 54. class Sample: def __init__(self, name, age): self.name=name self.age=age def update(self, age): self.age=age def compare(self, s1): if self.age == s1.age: return True else: return False s1=Sample(‘John’,25) s2=Sample(‘Joe’,28) print(s1.name) print(s2.name) print(s1.age) print(s2.age) s1.age=30 print(s1.age) print(s2.age) Sample.update(s2,30) if s1.compare(s2): print('same') else: print('not same‘)
  • 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
  • 57. class Sample: type='human' def __init__(self): self.name='John' self.age=28 def update(self): self.age=30 def compare(self, s1): if self.age == s1.age: return True else: return False s1=Sample() s2=Sample() print(s1.name, s1.age, s1.type) print(s2.name, s2.age, s2.type) s1.age=30 Sample.update(s2) Sample.type='animal' print(s1.name, s1.age, s1.type) print(s2.name, s2.age, s2.type)
  • 58. o/p: John 28 human John 28 human John 30 animal John 30 animal
  • 59. class Sample: type='human' school='secondary' def __init__(self): self.name='John' self.age=28 @classmethod def getinfo(cls): print(cls.type, cls.school) @staticmethod def info(): print('common for all') s1=Sample() s2=Sample() print(s1.name, s1.age, s1.type) print(s2.name, s2.age, s2.type) Sample.getinfo() Sample.info()
  • 60. o/p: John 28 human John 28 human human secondary common for all
  • 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
  • 64. class A: def __init__(self, a,st): self.a=a self.st=st def __gt__(self, other): if(self.a>other.a): return True else: return False ob1 = A(2,'Hello') ob2 = A(3,'World') if(ob1.__gt__(ob2)): print('ob1 is greater than ob2') else: print('ob2 is greater than ob1') if(ob1.st.__gt__(ob1.st)): print('s1 is greater than s2') else: print('s2 is greater than s1')
  • 65. o/p: ob2 is greater than ob1 s2 is greater than s1
  • 66. • Python magic methods or special functions for operator overloading • Binary operators: + __add__(self, other) – __sub__(self, other) * __mul__(self, other) / __truediv__(self, other) // __floordiv__(self, other) % __mod__(self, other) ** __pow__(self, other) >> __rshift__(self, other) << __lshift__(self, other) & __and__(self, other) | __or__(self, other) ^ __xor__(self, other)
  • 67. • Comparison Operators : < __LT__(SELF, OTHER) > __GT__(SELF, OTHER) <=__LE__(SELF, OTHER) >=__GE__(SELF, OTHER) ==__EQ__(SELF, OTHER) != __NE__(SELF, OTHER) • Unary Operators : – __NEG__(SELF, OTHER) + __POS__(SELF, OTHER) ~ __INVERT__(SELF, OTHER)
  • 68. • Assignment Operators : -= __ISUB__(SELF, OTHER) += __IADD__(SELF, OTHER) *= __IMUL__(SELF, OTHER) /= __IDIV__(SELF, OTHER) //= __IFLOORDIV__(SELF, OTHER) %=__IMOD__(SELF, OTHER) **= __IPOW__(SELF, OTHER) >>= __IRSHIFT__(SELF, OTHER) <<= __ILSHIFT__(SELF, OTHER) &=__IAND__(SELF, OTHER) |= __IOR__(SELF, OTHER) ^= __IXOR__(SELF, OTHER)
  • 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
  • 71. a=2 b=0 try: print(a/b) except Exception: print(“try to divide a number by 0”) o/p: try to divide a number by 0
  • 72. a=2 b=0 try: print(a/b) except Exception as e: print(“try to divide a number by 0”, e) o/p: try to divide a number by 0 division by zero
  • 73. finally • Always executed after try-except block • Whether exception raised or not, finally block always executed. Eg: try: k=5/0 print(k) except ZeroDivisionError: print(“error”) finally: print(‘always executed’)
  • 74. a=2 b=0 try: print(‘resource open’) print(a/b) except Exception as e: print('try to divide a number by 0', e) finally: print(‘resource closed’) o/p: resource open try to divide a number by 0 division by zero resource closed
  • 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'
  • 76. l=[1,2,3,4,5] try: print('second item=', l[1]) print('sixth item=', l[5]) except Exception as e: print('error=', e) o/p: second item= 2 error= list index out of range
  • 77. a=2 b=0 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 try to divide a number by 0 division by zero resource closed
  • 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
  • 79. Catching specific exceptions try: #stmts except IndexError: #stmts except ValueError: #stmts except ZeroDivisionError: #stmts
  • 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
  • 83. Declaring multiple exceptions with except clause • try block throws multiple exceptions • declare multiple exceptions with except clause try: #stmts except(<excep1>, <excep2>, <excep3>,……. <excep n>) #stmts else: #stmts
  • 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