Python Complete Unit 3
Python Complete Unit 3
UNIT-3
Regular Expressions:
Multithreaded programming:
Regular Expressions
A Regular Expressions (RegEx) is a special sequence of characters that
uses a search pattern to find a string or set of strings. It can detect the
presence or absence of a text by matching it with a particular pattern, and
also can split a pattern into one or more sub-patterns. Python provides a re
module that supports the use of regex in Python. Its primary function is to
offer a search, where it takes a regular expression and a string. Here, it
either returns the first match or else none.
RegEx Module
Python has a built-in package called re, which can be used to work with
Regular Expressions.
import re
RegEx in Python
When you have imported the re module, you can start using regular
expressions:
Example-1:
Search the string to see if it starts with "My"and ends with "nagendra":
import re
x = re.search("^My.*nagendra$",a)
if x:
else:
print("No match")
For complete python subject tutorials visit : ns lectures youtube channel
output:
Example-2:
import re
s = 'my name is nagendra'
print(re.search('nagendra', s))
output:
Function Description
split Returns a list where the string has been split at each
match
Example-1:
import re
x = re.match("my", a)
print(x)
Output:
Example-2:
import re
x = re.match("The", a)
print(x)
Output:
None
The search() function searches the string for a match, and returns a Match
object if there is a match.
If there is more than one match, only the first occurrence of the match will
be returned:
Match Object
Example-1:
import re
print(x)
Output:
Example-2:
import re
x = re.search("24", a)
print(x)
Output:
Example-1:
import re
x = re.findall("My", a)
print(x)
Output:
['my', 'my']
Example-1:
import re
x = re.split("nagendra", a)
print(x)
Output:
Example-1:
import re
x = re.sub("nagendra","rahul", a)
print(x)
Output:
Meta Characters
To understand the RE analogy, Meta Characters are useful, important, and will be
used in functions of module re. Below is the list of meta characters.
[ ] (Square Brackets)
Square Brackets ([]) represent a character class consisting of a set of
characters that we wish to match. For example, the character class [abc]
will match any single a, b, or c.
We can also specify a range of characters using – inside the square
brackets. For example,
[0-3] is sample as [0123]
[a-c] is same as [abc]
We can also invert the character class using the caret(^) symbol. For
example,
[^0-3] means any number except 0, 1, 2, or 3
[^a-c] means any character except a, b, or c
example-1:
import re
For complete python subject tutorials visit : ns lectures youtube channel
example-2:
import re
a= "my name is nagendra, my age is 25"
print(re.findall("[0-9]”, a))
output:
['2', '5']
^ (Caret)
Caret (^) symbol matches the beginning of the string i.e. checks whether
the string starts with the given character(s) or not. For example –
^B will check if the string starts with g such as Btech, Ball, BOX etc.
^BTECH will check if the string starts with BTECH such as BTECH
HYDERABAD, BTECH AIML, BTECH CSE etc.
example-1:
import re
a = 'Btech'
result = re.match(‘^B’, a)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
For complete python subject tutorials visit : ns lectures youtube channel
output:
Search successful.
example-2:
import re
a = 'Btech hyderabad'
result = re.match(‘^Btech’, a)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
output:
Search successful.
$ (Dollar)
Dollar($) symbol matches the end of the string i.e checks whether the string
ends with the given character(s) or not. For example –
s$ will check for the string that ends with a such as geeks, ends, s,
etc.
ks$ will check for the string that ends with ks such as geeks,
geeksforgeeks, ks, etc.
example-1:
import re
a = 'Btech'
result = re.search(‘h$’, a)
For complete python subject tutorials visit : ns lectures youtube channel
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
output:
Search successful.
example-2:
import re
a = 'Btech hyderabad'
result = re.search(‘hyderabad$’, a)
if result:
print("Search successful.")
else:
print("Search unsuccessful.")
output:
Search successful.
. (Dot)
Dot(.) symbol matches only a single character except for the newline
character (\n). For example –
a.b will check for the string that contains any character at the place of
the dot such as acb, adb, arb, a1b, etc...
For complete python subject tutorials visit : ns lectures youtube channel
example-2:
import re
a= "hello hyderabad"
x = re.findall("hyd.....d", a)
print(x)
output:
['hyderabad']
| (Or)
Or symbol works as the or operator meaning it checks whether the pattern
before or after the or symbol is present in the string or not. For example –
btech|mtech will match any string that contains btech or mtech.
Example-1:
import re
a= "i am from btech and i am from mtech "
For complete python subject tutorials visit : ns lectures youtube channel
x = re.findall("btech|mtech", a)
print(x)
output:
['btech', 'mtech']
Example-2:
import re
a= "i am nagendra and i am from BTECH"
x = re.findall(" BTECH | MTECH ", a)
print(x)
output:
['btech']
? (Question Mark)
The question mark symbol ? matches zero or one occurrence of the
pattern left to it.
Expression String Matched?
mn 1 match
man 1 match
woman 1 match
example-1:
For complete python subject tutorials visit : ns lectures youtube channel
import re
a= "i am a man"
x = re.findall("ma?n", a)
print(x)
output:
['man']
example-2:
import re
a= "i am a mn"
x = re.findall("ma?n", a)
print(x)
output:
['mn']
example-3:
import re
a= "i am a maaaan"
x = re.findall("ma?n", a)
print(x)
output:
[ ] ( output is empty because a repeated more than once. The question
mark symbol ? matches zero or one occurrence of the pattern left to it.)
* (Star)
The star symbol * matches zero or more occurrences of the pattern left to
it.
For complete python subject tutorials visit : ns lectures youtube channel
mn 1 match
man 1 match
woman 1 match
example-1:
import re
a= "i am a maaan"
x = re.findall("ma*n", a)
print(x)
output:
['maaan']
example-2:
import re
a= "i am a mn"
x = re.findall("ma*n", a)
print(x)
output:
['mn']
+ (Plus)
The plus symbol + matches one or more occurrences of the pattern left to it.
For complete python subject tutorials visit : ns lectures youtube channel
man 1 match
maaan 1 match
woman 1 match
example-1:
import re
a= "i am a maaan"
x = re.findall("ma+n", a)
print(x)
output:
['maaan']
example-2:
import re
a= "i am a mn"
x = re.findall("ma+n", a)
print(x)
output:
For complete python subject tutorials visit : ns lectures youtube channel
{ } (Braces)
Consider this code: {n,m}. This means at least n, and at most m repetitions
of the pattern left to it.
Example-1:
import re
a= "my name is nagendra, my age is 25"
x = re.findall("my{1,3}", a)
print(x)
output:
['my', 'my']
from above pattern my{1,3} mean --if “my” is present at least once and
maximum three time then it will print “my”
from above example “my” is present twice, so it will print my twice
( ) -Group
Group symbol is used to group sub-patterns.
Special
Description
Sequence
It is the opposite of the \b i.e. the string should not start or end
\B
with the given regex.
Matches any decimal digit, this is equivalent to the set class [0-
\d
9]
Program:
import re
a="my name is nagendra, my age is 25"
b=re.findall("\Amy",a)
c=re.findall("\w",a)
d=re.findall("\W",a)
e=re.findall("\d",a)
f=re.findall("\D",a)
g=re.findall("\s",a)
h=re.findall("\S",a)
i=re.findall(r"\bmy", a)
i=re.findall(r"\bna", a)
j=re.findall(r"ra\b",a)
print("output of \A is ",b)
print("output of \w is ",c)
print("output of \W is ",d)
print("output of \d is ",e)
print("output of \D is ",f)
print("output of \s is ",g)
print("output of \S is ",h)
print("output of \b is ",i)
print("output of \b is ",j)
For complete python subject tutorials visit : ns lectures youtube channel
output:
output of \A is ['my']
output of \w is ['m', 'y', 'n', 'a', 'm', 'e', 'i', 's', 'n', 'a', 'g', 'e', 'n', 'd', 'r', 'a',
'm', 'y', 'a', 'g', 'e', 'i', 's', '2', '5']
output of \D is ['m', 'y', '', 'n', 'a', 'm', 'e', '', 'i', 's', '', 'n', 'a', 'g', 'e', 'n', 'd',
'r', 'a', ',', '', 'm', 'y', '', 'a', 'g', 'e', '', 'i', 's', '']
output of \S is ['m', 'y', 'n', 'a', 'm', 'e', 'i', 's', 'n', 'a', 'g', 'e', 'n', 'd', 'r', 'a',
',', 'm', 'y', 'a', 'g', 'e', 'i', 's', '2', '5']
output of \b is ['na’]
output of \b is ['ra’]
BRarnple
wite o python poqxarn to Veroove all white spau:ro a hing
rm a
ienpovt se
ame S nagendYo
amy
he.Su("|s+ a
priot ()
output mynamelsnaqencra
(omma t h #
to yeplace au Spas, doty
wte a
pytomprocvo
import e is 05.
isnagendra
my aqe
a my ame
is 95, my no o is as"
y age
ve.datCd+",)
or in b:
print ()
Oput
ython proqvam to emo Vouwele on qan test
por e
a a n a &neg endra,
veSoauEoo" ",
printC)
utput s nand ny
nand'm
Python progvo o pyint worestars wHh
Pyth
icport e
.
ve.nde a)
Print()
name
ogndra,,'
word that Stors wth na
Ptkopregrern
import e
is 2S
narnes nagendra, a
b |bna
print (1
oukput
nam nasendr
Stat wth na
Pythm proqromö Ppriot
rint wods that
Yelax expresior
Ond end th sa Using
-import se
ynaMa is n a g e n d r a
a g e n d a
nda0( wt a
printl)
ukpu-nogendra'
in eat le
point yol
nunber
o
Python prog
Yayu
13 Shiva
Nagendra
nport
a bpen (4e.t
ine
b ye. matth
("14ay
13
pnbgroupl)
For complete python subject tutorials visit : ns lectures youtube channel
Multithreading
Python Multithreading:
Multithreading is a threading technique in Python programming to run
multiple threads concurrently by rapidly switching between threads with
a CPU help (called context switching). Besides, it allows sharing of its
data space with the main threads inside a process that share
information and communication with other threads easier than
individual processes. Multithreading aims to perform multiple tasks
simultaneously, which increases performance, speed and improves the
rendering of the application.
Benefits of Multithreading in Python
A thread is the smallest unit of a program or process executed independently
or scheduled by the Operating System. In the computer system, an Operating
System achieves multitasking by dividing the process into threads. A thread is
a lightweight process that ensures the execution of the process separately on
the system. In Python 3, when multiple processors are running on a program,
each processor runs simultaneously to execute its tasks separately.
Thread modules
A thread is an entity within a process that can be scheduled for execution.
Also, it is the smallest unit of processing that can be performed in an OS
(Operating System). In simple words, a thread is a sequence of such
instructions within a program that can be executed independently of other
codes.
It is started with Python 3, designated as obsolete, and can only be
accessed with _thread that supports backward compatibility.
Syntax:
thread.start_new_thread ( function_name, args[, kwargs] )
example:
import _thread
import time
def name(n):
time.sleep(0.5)
print("my name is:",n)
def country(m):
time.sleep(0.5)
print("my country is :",m)
r=time.time()
_thread.start_new_thread(name,("nagendrasai",))
_thread.start_new_thread(country,("india",))
print("time taken to execute two functions:",time.time()-r)
output:
my name is nagendrasai
my country is india
time taken to execute two functions:00:0045555
For complete python subject tutorials visit : ns lectures youtube channel
Types of Threads
There are two types of threads, which are:
1. User Level Thread
As the name suggests, the user-level threads are only managed by users,
and the kernel does not have its information.
These are faster, easy to create and manage.
The kernel takes all these threads as a single process and handles them
as one process only. The user-level threads are implemented by user-
level libraries, not by the system calls.
2. Kernel-Level Thread
The kernel-level threads are handled by the Operating system and managed
by its kernel. These threads are slower than user-level threads because
context information is managed by the kernel. To create and implement a
kernel-level thread, we need to make a system call.
Features of Thread
Threads share data, memory, resources, files, etc., with their peer threads
within a process.
One system call is capable of creating more than one thread.
Each thread has its own stack and register.
Threads can directly communicate with each other as they share the same
address space.
Threads need to be synchronized in order to avoid unexpected scenarios.
Threading Modules
The threading module included with Python 2.4 provides much more
powerful, high-level support for threads than the thread module .To use
multithreading, we need to import the threading module in Python Program.
The threading module exposes all the methods of the thread module and
provides some additional methods −
threading.activeCount() − Returns the number of thread objects that are
active.
threading.currentThread() − Returns the number of thread objects
in the caller's thread control.
threading.enumerate() − Returns a list of all thread objects that are
currently active.
In addition to the methods, the threading module has the Thread class that
For complete python subject tutorials visit : ns lectures youtube channel
implements threading. The methods provided by the Thread class are as follows
−
run() − The run() method is the entry point for a thread.
start() − The start() method starts a thread by calling the run method.
join() − The join() waits for threads to terminate.
isAlive() − The isAlive() method checks whether a thread is still executing.
getName() − The getName() method returns the name of a thread.
setName() − The setName() method sets the name of a thread.
Apart from the functions specified above, the threading module also provides
many classes whose objects are very useful in creating and managing threads.
Object Description
The above table gives a brief introduction of various object types in python
threading module. We will discuss about all these objects in details in the next few
tutorials.
Follow the given below steps to implement the threading module in Python
Multithreading:
1. Import the threading module:
It contains the target function, argument, and kwargs as the parameter in the
Thread() class.
Target: It defines the fun4. Join method:ction name that is executed by
the thread.
Args: It defines the arguments that are passed to the target function
name.
3. Start a new thread:
To start a thread in Python multithreading, call the thread class's object. The
start() method can be called once for each thread object; otherwise, it throws an
exception error.
4. Join method:
It is a join() method used in the thread class to halt the main thread's
execution and waits till the complete execution of the thread object. When the
thread object is completed, it starts the execution of the main thread in
Python.
For complete python subject tutorials visit : ns lectures youtube channel
1. python programming for multi threading in which one thread executes and
prints the square of numbers and other thread executes and prints the cube of
numbers from 1 to 5
(OR)
PYTHON PROGRAM TO ACHIEVE PARALLELISM BY THREADING
MODULE
(OR)
PROGRAM FOR INTER PROCESS COMMUNICATION
(OR)
PYTHON PROGRAM FOR MULTI-THREADING
PROGRAM:
import threading
import time
def square(n):
print("square of a number:")
for i in n:
time.sleep(0.2)
print("square is :",i*i)
def cube(n):
print("cube of a number:")
for i in n:
time.sleep(0.2)
print("cube is :",i*i*i)
r=time.time()
l=[1,2,3,4,5]
t1=threading.Thread(target=square,args=(l,))
t2=threading.Thread(target=cube,args=(l,))
t1.start()
t2.start()
t1.join()
t2.join()
print("time taken to execute two functions:",time.time()-r)output:
square of a number:
cube of a number:
square is : 1
cube is : 1
cube is : 8
square is : 4
cube is : 27
square is : 9
For complete python subject tutorials visit : ns lectures youtube channel
cube is : 64
square is : 16
cube is : 125
square is : 25
time taken to execute two
functions: 1.0623559951782227
2. python program to create two threads. one thread will display the numbers
from 1 to 10 in normal order and other thread will display the numbers in
reverse order
import threading
import time
def normalorder(n):
print("normal order:")
for i in range(1,n+1):
time.sleep(0.2)
print(i)
def reverseorder(n):
print("reverse order:")
for i in range(n,0,-1):
time.sleep(0.2)
print(i)
r=time.time()
t1=threading.Thread(target=normalorder,args=(10,))
t2=threading.Thread(target=reverseorder,args=(10,))
t1.start()
t1.join()
t2.start()
t2.join()
print("time taken to execute two functions:",time.time()-r)
output:
normal order:
1
2
3
4
5
6
7
8
9
For complete python subject tutorials visit : ns lectures youtube channel
10
reverse order:
10
9
8
7
6
5
4
3
2
1
time taken to execute two functions: 4.010312557220459
In a scenario where you have multiple threads, what can happen is that both the
thread might try to acquire the memory at the same time, and as a result of which
they would overwrite the data in the memory. Hence, arises a need to have a
mechanism that could help prevent this phenomenon.
Some popular interpreters that have GIL are CPython and Ruby MRI. As most of
you would know that Python is an interpreted language, it has various
distributions like CPython, Jython, IronPython. Out of these, GIL is supported only
in CPython, and it is also the most widely used implementation of Python.
CPython has been developed in both C and Python language primarily to support
and work with applications that have a lot of C language underneath the hood.
The impact of the GIL isn’t visible to developers who execute single-threaded
programs.Since the GIL allows only one thread to execute at a time even in a multi-
threaded architecture with more than one CPU core, the GIL has gained a reputation
as an “infamous” feature of Python.
Python Global Interpreter Lock (GIL) is a type of process lock which is used
by python whenever it deals with processes. Generally, Python only uses
only one thread to execute the set of written statements. This means that in
python only one thread will be executed at a time. The performance of the
single-threaded process and the multi-threaded process will be the same in
python and this is because of GIL in python. We can not achieve
multithreading in python because we have global interpreter lock which
restricts the threads and works as a single thread.
For complete python subject tutorials visit : ns lectures youtube channel
There are two states of a lock i.e locked and unlocked. A lock is a class in
the threading module whose object generated in the unlocked state and has
two primary methods i.e acquire() and release(). When the acquire() method is
called, it locks the execution of the Lock and blocks it’s execution until the
release() method is called in some other thread which sets it to unlock state.
Locks help us in efficiently accessing a shared resource in a program in order
to prevent corruption of data, it follows mutual exclusion as only one thread
can access a particular resource at a time.
For complete python subject tutorials visit : ns lectures youtube channel
An instance of the RLock can be created and then acquired by threads before
accessing a critical section, and released after the critical section.
For example:
lock = RLock()
lock.acquire()
# ...
lock.release()
For complete python subject tutorials visit : ns lectures youtube channel
Subprocess module
The subprocess module present in Python(both 2.x and 3.x) is used to run new
applications or programs through Python code by creating new processes. It
also helps to obtain the input/output/error pipes as well as the exit codes of
various commands.
To execute different programs using Python two functions of the subprocess
module are used:
1.subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None,
shell=False)
Parameters:
args=The command to be executed. Several commands can be passed as a
String by separated by “;”.
stdin=Value of standard input stream to be passed as (os.pipe()).
stdout=Value of output obtained from standard output stream.
stderr=Value of error obtained(if any) from standard error stream.
shell=Boolean parameter.If True the commands get executed through a new
shell environment.
Return-Value:
The function returns the return code of the command.If the return code is zero,
the function simply returns(command executed successfully) otherwise
CalledProcessError is being raised.
2.subprocess.check_output(args, *, stdin=None, stderr=None, shell=False,
universal_newlines=False)
Parameters:
args=The command to be executed.Several commands can be passed as a
For complete python subject tutorials visit : ns lectures youtube channel
c-program:
#include<stdio.h>
int main()
{
printf("Hello my name is nagendrasai, this is c program ");
return 0;
}
import subprocess
import os
def excute():
s = subprocess.check_call("gcc HelloWorld.c -o out1;./out1", shell = True)
if _ _ file _ _ = _ _ main _ _:
execute()
Output:
Hello my name is nagendrasai, this is c program
Multiprocessing module
For complete python subject tutorials visit : ns lectures youtube channel