II B.
Tech I- Semester Python Programming (2022-23)
LECTURE NOTES
ON
PYTHON PROGRAMMING
UNIT III PART-I & II
(AI & ML)
II B. Tech I semester [2022-23]
R18 Regulation
Dr. N.Venkateswaran,
Associate Professor,
Department of Computer Science & Engineering
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 1
II B.Tech I- Semester Python Programming (2022-23)
PYTHON PROGRAMMING –UNIT-III
Python Regular Expressions
The regular expressions can be defined as the sequence of characters which are used to search for a pattern in a
string. The module re provides the support to use regex in the python program. The re module throws an
exception if there is some error while using the regular expression.
The re module must be imported to use the regex functionalities in python.
import re
Regex Functions
The following regex functions are used in the python.
SN Function Description
This method matches the regex pattern in the string with the optional flag. It returns true if a
1 Match
match is found in the string otherwise it returns false.
2 Search This method returns the match object if there is a match found in the string.
3 Findall It returns a list that contains all the matches of a pattern in the string.
4 Split Returns a list in which the string has been split in each match.
5 Sub Replace one or many matches in the string.
Forming a regular expression
A regular expression can be formed by using the mix of meta-characters, special sequences, and sets.
Meta-Characters
Metacharacter is a character with the specified meaning.
Metacharacter Description Example
[] It represents the set of characters. "[a-z]"
\ It represents the special sequence. "\r"
. It signals that any character is present at some specific place. "Ja.v."
^ It represents the pattern present at the beginning of the string. "^Java"
$ It represents the pattern present at the end of the string. "point"
* It represents zero or more occurrences of a pattern in the string. "hello*"
+ It represents one or more occurrences of a pattern in the string. "hello+"
{} The specified number of occurrences of a pattern the string. "java{2}"
| It represents either this or that character is present. "java|point"
() Capture and group
Special Sequences
Special sequences are the sequences containing \ followed by one of the characters.
Character Description
\A It returns a match if the specified characters are present at the beginning of the string.
\b It returns a match if the specified characters are present at the beginning or the end of the string.
It returns a match if the specified characters are present at the beginning of the string but not at the
\B
end.
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 2
II B.Tech I- Semester Python Programming (2022-23)
\d It returns a match if the string contains digits [0-9].
\D It returns a match if the string doesn't contain the digits [0-9].
\s It returns a match if the string contains any white space character.
\S It returns a match if the string doesn't contain any white space character.
\w It returns a match if the string contains any word characters.
\W It returns a match if the string doesn't contain any word.
\Z Returns a match if the specified characters are at the end of the string.
Sets
A set is a group of characters given inside a pair of square brackets. It represents the special meaning.
SN Set Description
1 [arn] Returns a match if the string contains any of the specified characters in the set.
2 [a-n] Returns a match if the string contains any of the characters between a to n.
3 [^arn] Returns a match if the string contains the characters except a, r, and n.
4 [0123] Returns a match if the string contains any of the specified digits.
5 [0-9] Returns a match if the string contains any digit between 0 and 9.
6 [0-5][0-9] Returns a match if the string contains any digit between 00 and 59.
10 [a-zA-Z] Returns a match if the string contains any alphabet (lower-case or upper-case).
The findall() function
This method returns a list containing a list of all matches of a pattern within the string. It returns the patterns in
the order they are found. If there are no matches, then an empty list is returned.
Consider the following example.
Example
import re
str = "How are you. How is everything"
matches = re.findall("How", str)
print(matches)
print(matches)
Output:
['How', 'How']
['How', 'How']
The match object
The match object contains the information about the search and the output. If there is no match found, the None
object is returned.
Example
import re
str = "How are you. How is everything"
matches = re.search("How", str)
print(type(matches))
print(matches) #matches is the search object
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 3
II B.Tech I- Semester Python Programming (2022-23)
Output:
<class '_sre.SRE_Match'>
<_sre.SRE_Match object; span=(0, 3), match='How'>
The Match object methods
There are the following methods associated with the Match object.
1. span(): It returns the tuple containing the starting and end position of the match.
2. string(): It returns a string passed into the function.
3. group(): The part of the string is returned where the match is found.
Example
import re
str = "How are you. How is everything"
matches = re.search("How", str)
print(matches.span())
print(matches.group())
print(matches.string)
Output:
(0, 3)
How
How are you. How is everything
re.search(pattern, string):
It is similar to match() but it doesn‟t restrict us to find matches at the beginning of the string only. Unlike
previous method, here searching for pattern „Analytics‟ will return a match.
Example:
result = re.search('Analytics', 'AV Analytics Vidhya AV')
print result.group()
Output:
Analytics
Here you can see that, search() method is able to find a pattern from any position of the string but it only returns
the first occurrence of the search pattern.
re.findall (pattern, string):
It helps to get a list of all matching patterns. It has no constraints of searching from start or end. If we will use
method findall to search „AV‟ in given string it will return both occurrence of AV. While searching a string, I
would recommend you to use re.findall() always, it can work like re.search() and re.match() both.
Example:
result = re.findall('AV', 'AV Analytics Vidhya AV')
print result
Output:
['AV', 'AV']
re.split(pattern, string, [maxsplit=0]):
This method helps to split string by the occurrences of given pattern.
Example:
result=re.split('y','Analytics')
print(result)
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 4
II B.Tech I- Semester Python Programming (2022-23)
Output:
['Anal', 'tics']
Above, we have split the string “Analytics” by “y”. Method split() has another argument “maxsplit“. It has
default value of zero. In this case it does the maximum splits that can be done, but if we give value to maxsplit,
it will split the string. Let‟s look at the example below:
Example:
result=re.split('i','Analytics Vidhya')
print(result)
Output:
['Analyt', 'cs V', 'dhya'] #It has performed all the splits that can be done by pattern "i".
Example:
result=re.split('i','Analytics Vidhya',maxsplit=1)
print(result)
Output:
['Analyt', 'cs Vidhya']
Here, you can notice that we have fixed the maxsplit to 1. And the result is, it has only two values whereas first
example has three values.
re.sub(pattern, replacement, Target string):
It helps to search a pattern and replace with a new sub string. If the pattern is not found, string is returned
unchanged.
Example:
result=re.sub(r'India','the World','AV is largest Analytics community of India')
print(result)
Output:
'AV is largest Analytics community of the World'
Python RegEx Examples
1. ^a...s$
The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending with
s.
A pattern defined using RegEx can be used to match against a string.
Expression String Matched?
ads No match
alias Match
^a...s$ abyss Match
Alias No match
An abacus No match
import re
pattern = '^a...s$'
test_string = 'abyss'
result = re.match(pattern, test_string)
if result:
print("Search successful.")
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 5
II B.Tech I- Semester Python Programming (2022-23)
else:
print("Search unsuccessful.")
Here, we used re.match() function to search pattern within the test_string. The method returns a match object if
the search is successful. If not, it returns None.
There are other several functions defined in the re module to work with RegEx. Before we explore that, let's
learn about regular expressions themselves.
Specify Pattern Using RegEx
To specify regular expressions, meta characters are used. In the above example, ^ and $ are metacharacters.
Meta Characters
Metacharacters are characters that are interpreted in a special way by a RegEx engine. Here's a list of
metacharacters:
[] . ^ $ * + ? {} () \ |
[] - Square brackets
Square brackets specifies a set of characters you wish to match.
Expression String Matched?
a 1 match
ac 2 matches
[abc]
Hey Jude No match
abc de ca 5 matches
Here, [abc] will match if the string you are trying to match contains any of the a, b or c.
You can also specify a range of characters using - inside square brackets.
[a-e] is the same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
You can complement (invert) the character set by using caret ^ symbol at the start of a square-bracket.
[^abc] means any character except a or b or c.
[^0-9] means any non-digit character.
. - Period
A period matches any single character (except newline '\n').
Expression String Matched?
a No match
ac 1 match
..
acd 1 match
acde 2 matches (contains 4 characters)
^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 6
II B.Tech I- Semester Python Programming (2022-23)
Expression String Matched?
a 1 match
^a abc 1 match
bac No match
abc 1 match
^ab
acb No match (starts with a but not followed by b)
$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.
Expression String Matched?
a 1 match
a$ formula 1 match
cab No match
* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.
Expression String Matched?
mn 1 match
man 1 match
ma*n maaan 1 match
main No match (a is not followed by n)
woman 1 match
+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.
Expression String Matched?
mn No match (no a character)
man 1 match
ma+n maaan 1 match
main No match (a is not followed by n)
woman 1 match
? - 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
ma?n maaan No match (more than one a character)
main No match (a is not followed by n)
woman 1 match
{} - Braces
Consider this code: {n,m}. This means at least n, and at most m repetitions of the pattern left to it.
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 7
II B.Tech I- Semester Python Programming (2022-23)
Expression String Matched?
abc dat No match
abc daat 1 match (at daat)
a{2,3}
aabc daaat 2 matches (at aabc and daaat)
aabc daaaat 2 matches (at aabc and daaaat)
Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits but not more than 4 digits
Expression String Matched?
ab123csde 1 match (match at ab123csde)
[0-9]{2,4} 12 and 345673 2 matches (at 12 and 345673)
1 and 2 No match
| - Alternation
Vertical bar | is used for alternation (or operator).
Expression String Matched?
cde No match
a|b ade 1 match (match at ade)
acdbea 3 matches (at acdbea)
Here, a|b match any string that contains either a or b
() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that matches either a or b
or c followed by xz
Expression String Matched?
ab xz No match
(a|b|c)xz abxz 1 match (match at abxz)
axz cabxz 2 matches (at axzbc cabxz)
\ - Backslash
Backlash \ is used to escape various characters including all metacharacters. For example,
\$a match if a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a special way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This makes sure the
character is not treated in a special way.
Special Sequences
Special sequences make commonly used patterns easier to write. Here's a list of special sequences:
\A - Matches if the specified characters are at the start of a string.
Expression String Matched?
the sun Match
\Athe
In the sun No match
\b - Matches if the specified characters are at the beginning or end of a word.
Expression String Matched?
\bfoo football Match
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 8
II B.Tech I- Semester Python Programming (2022-23)
Expression String Matched?
a football Match
afootball No match
the foo Match
foo\b the afoo test Match
the afootest No match
\B - Opposite of \b. Matches if the specified characters are not at the beginning or end of a word.
Expression String Matched?
football No match
\Bfoo a football No match
afootball Match
the foo No match
foo\B the afoo test No match
the afootest Match
\d - Matches any decimal digit. Equivalent to [0-9]
Expression String Matched?
12abc3 3 matches (at 12abc3)
\d
Python No match
\D - Matches any non-decimal digit. Equivalent to [^0-9]
Expression String Matched?
1ab34"50 3 matches (at 1ab34"50)
\D
1345 No match
\s - Matches where a string contains any whitespace character. Equivalent to [ \t\n\r\f\v].
Expression String Matched?
Python RegEx 1 match
\s
PythonRegEx No match
\S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\v].
Expression String Matched?
ab 2 matches (at a b)
\S
No match
\w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_]. By the way,
underscore _ is also considered an alphanumeric character.
Expression String Matched?
12&": ;c 3 matches (at 12&": ;c)
\w
%"> ! No match
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 9
II B.Tech I- Semester Python Programming (2022-23)
\W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_]
Expression String Matched?
1a2%c 1 match (at 1a2%c)
\W
Python No match
\Z - Matches if the specified characters are at the end of a string.
Expression String Matched?
I like Python 1 match
\ZPython I like Python No match
Python is fun. No match
UNIT-III PART-II
Multithreading Programming
Time is the most essential factor in life. The world of programming provides various tricks and techniques that
significantly help you reduce the time consumption, thereby increasing performance. One such approach is
Multithreading in Python, which is one of the most important concepts.
What is Multitasking in Python?
Multitasking, in general, is the capability of performing multiple tasks simultaneously. In technical terms,
multitasking refers to the ability of an operating system to perform different tasks at the same time. For
instance, you are downloading something on your PC as well as listening to songs and concurrently playing a
game, etc. All these tasks are performed by the same OS an in sync. This is nothing but multitasking which not
just helps you save time but also increases productivity.
There are two types of multitasking in an OS:
Process-based
Thread-based
you will be learning about Thread-based multitasking or Multithreading.
What is a thread?
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 10
II B.Tech I- Semester Python Programming (2022-23)
A thread is basically an independent flow of execution. A single process can consist of multiple threads. Each
thread in a program performs a particular task. For Example, when you are playing a game say FIFA on your
PC, the game as a whole is a single process, but it consists of several threads responsible for playing the music,
taking input from the user, running the opponent synchronously, etc. All these are separate threads responsible
for carrying out these different tasks in the same program.
Every process has one thread that is always running. This is the Main thread. This main thread actually creates
the child thread objects. The child thread is also initiated by the main thread.
When to use Multithreading in Python?
Multithreading is very useful for saving time and improving performance, but it cannot be applied everywhere.
In the previous FIFA example, the music thread is independent of the thread that takes your input and the thread
that takes your input is independent of the thread that runs your opponent. These threads run independently
because they are not inter-dependent.
Therefore, multithreading can be used only when the dependency between individual threads does not exist.
How to achieve Multithreading in Python?
Multithreading in Python can be achieved by importing the threading module.
import threading
from threading import *
How to create threads in Python?
Threads in Python can be created in three ways:
1. Without creating a class
2. By extending Thread class
3. Without extending Thread class
Without creating a class
Multithreading in Python can be accomplished without creating a class as well. Here is an example to
demonstrate the same:
Example:
from threading import *
print(current_thread().getName())
def mt():
print("Child Thread")
child=Thread(target=mt)
child.start()
print("Executing thread name :",current_thread().getName())
Output:
MainThread
Child Thread
Executing thread name : MainThread
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 11
II B.Tech I- Semester Python Programming (2022-23)
The above output shows that the first thread that is present is, the Main thread. This main thread then creates a
child thread that is executing the function and then the final print statement is executed again by the main
thread.
Now let us move ahead and see how to do Multithreading in python by extending the Thread class.
By extending the Thread class:
When a child class is created by extending the Thread class, the child class represents that a new thread is
executing some task. When extending the Thread class, the child class can override only two methods i.e. the
__init__() method and the run() method. No other method can be overridden other than these two methods.
Here is an example of how to extend the Thread class to create a thread:
Example:
import threading
import time
class mythread(threading.Thread):
def run(self):
for x in range(7):
print("Hi from child")
a = mythread()
a.start()
a.join()
print("Bye from",current_thread().getName())
Output:
Hi from child
Hi from child
Hi from child
Hi from child
Hi from child
Hi from child
Hi from child
Bye from MainThread
The above example shows that class myclass is inheriting the Thread class and the child class i.e myclass is
overriding the run method. By default, the first parameter of any class function needs to be self which is the
pointer to the current object. The output shows that the child thread executes the run() method and the main
thread waits for the childs execution to complete. This is because of the join() function, which makes the main
thread wait for the child to finish.
This method of creating threads is the most preferred method because its the standard method. But in case you
want to create threads without inheriting or extending the Thread class, you can do it in the following manner.
Without Extending Thread class
To create a thread without extending the Thread class, you can do as follows:
Example:
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 12
II B.Tech I- Semester Python Programming (2022-23)
from threading import *
class ex:
def myfunc(self): #self necessary as first parameter in a class func
for x in range(7):
print("Child")
myobj=ex()
thread1=Thread(target=myobj.myfunc)
thread1.start()
thread1.join()
print("done")
Output:
Child
Child
Child
Child
Child
Child
Child
done
The child thread executes myfunc after which the main thread executes the last print statement.
Advantages of using threading
Multithreading has many advantages some of which are as follows:
Better utilization of resources
Simplifies the code
Allows concurrent and parallel occurrence of various tasks
Reduces the time consumption or response time, thereby, increasing the performance.
Here is an example to check how long it takes for a code to execute with and without multithreading in python:
Example:
import time
def sqr(n):
for x in n:
time.sleep(1)
x%2
def cube(n):
for x in n:
time.sleep(1)
x%3
n=[1,2,3,4,5,6,7,8]
s=time.time()
sqr(n)
cube(n)
e=time.time()
print(e-s)
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 13
II B.Tech I- Semester Python Programming (2022-23)
Output:
16.042309284210205
The above is the output time taken to execute the program without using threads. Now let us use threads and see
what happens to the same program:
Example:
import threading
from threading import *
import time
def sqr(n):
for x in n:
time.sleep(1)
print('Remainder after dividing by 2',x%2)
def cube(n):
for x in n:
time.sleep(1)
print('Remainder after dividing by 3',x%3)
n=[1,2,3,4,5,6,7,8]
start=time.time()
t1=Thread(target=sqr,args=(n,))
t2=Thread(target=cube,args=(n,))
t1.start()
time.sleep(1)
t2.start()
t1.join()
t2.join()
end=time.time()
print(end-start)
Output: 9.040220737457275
The above output clearly shows that the time taken when we use threads is much less compared to the time
taken for the same program to execute without using threads.
Prepared by Dr. N.Venkateswaran, Associate Professor, CSE Dept, JITS Page 14