0% found this document useful (0 votes)
2 views141 pages

Unit-2 Python Programming

The document outlines the syllabus for the Python Programming course (10211CS213) offered in the Winter Semester of the 2022-23 academic year, detailing prerequisites, course outcomes, and a comprehensive syllabus divided into five units. Each unit covers essential topics such as Python basics, file manipulation, exception handling, data analysis, and UI/game design, with case studies provided for practical application. The document also includes references to textbooks and online resources for further learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views141 pages

Unit-2 Python Programming

The document outlines the syllabus for the Python Programming course (10211CS213) offered in the Winter Semester of the 2022-23 academic year, detailing prerequisites, course outcomes, and a comprehensive syllabus divided into five units. Each unit covers essential topics such as Python basics, file manipulation, exception handling, data analysis, and UI/game design, with case studies provided for practical application. The document also includes references to textbooks and online resources for further learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

SCHOOL OF COMPUTING

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Academic Year 2022-23 : Winter Semester


10211CS213 / PYTHON PROGRAMMING
Pre-requisites

Sl. No Course Code Course Name

1 1150CS201 Problem Solving using C

2/27/2023 Ms.S. Shanmuga Priya, Assistant Professor, Department of CSE. Slot : S1 1


Course Outcomes
CO Level of learning
Course Outcomes
Nos. domain
Experiment with data types, operators, statements
CO1 K2
and functions in python.
Construct the file handling, string handling and
CO2 K3
regular expression functions in python
Use the Exception handling to handle errors and
CO3 K3
multithreading mechanism for parallel execution .
Demonstrate the use of python libraries for data
CO4 K3
analysis.
Apply python modules for Graphical User
CO5 K3
Interface and game design

2/27/2023 Ms.S. Shanmuga Priya, Assistant Professor, Department of CSE. Slot : S1 2


Correlation of COs with POs and PSOs

PO PO PO PO PO PO PSO PSO PSO


Cos PO1 PO2 PO3 PO4 PO5 PO6
7 8 9 10 11 12 1 2 3

CO1 2 2 2 2
2
CO2 2 2
2
CO3 2 2 2
2 3 2
CO4 2 2 3 3 3
2
CO5 2 3 3

1– High; 2 – Medium; 3 - Low

2/27/2023 Ms.S. Shanmuga Priya, Assistant Professor, Department of CSE. Slot : S1 3


Syllabus Content
Unit I Introduction 6
Python Basics:variables-data types-operators and expressions-control
statements-comments in the program-python collections (List, Tuple, Set,
Dictionary)-modules-packages and composition-python functions-build-in
functions-Lambda functions-python iterator and generator.
Case Study: Shuffling a Deck of Card
Unit II File Manipulation, String Handling and Regular
Expression 6
Manipulating files and directories, os and sys modules; text files:
reading/writing text and binary files; creating and reading a formatted file
(csv or tab-separated); String manipulations: indexing, slicing a string-
string operations-number system-Regular expressions- match and search
functions; modifiers and patterns-python Decorators.
Case Studies: Creating a Hash File (or a message digest of a file) and
pattern recognition

2/27/2023 4
Syllabus Content
Unit – III Exception Handling and Multi-Threading in python
6
Exception handling: try, except and finally block, handling multiple
exceptions, raise an exception, User defined exception- python
multithreading- thread and threading module-Synchronizing Threads in
Python.
Case Study: Development of student performance evaluation report

Unit IV Data Analysis using Python libraries 6


NumPy: Introduction, NdArray object, Data Types, Array Attributes,
Indexing and Slicing, Array manipulation, mathematical functions,
Matplotlib; Pandas: Introduction to pandas data structures-series-Data
Frame-Panel-basic functions-descriptive statistics function-iterating data
frames-statistical functions-aggregations-visualization- plotting graphs
using plotly Library.
Case Study: Sales Forecasting
2/27/2023 5
Syllabus Content
Unit V UI and Game design using Python libraries 6
Tkinter module: introduction, widgets, standard attributes, Geometry
management, Tkinter Event Handling; Database connectivity with MySQL.
PyGame Module - PyGame Concepts- Basic Game Design-Sprites-Sprite
Groups-Custom Events-Collision Detection-Sprite Images-Game Speed-
Sound Effects.
Case Studies: Angry bird game and UI design

Text Books:
• Guido van Rossum, Learning Python: Crash Course Tutorial, The Python
development team publishers, 2020. [Unit-1,2,3]
• Bharti Motwani, Data Analytics using Python, paperback edition, Wiley
Publishing Ltd, 2020. [Unit-4]
• James R. Parker, Game Development Using Python, 2nd edition, Mercury
Learning and Information publishers,, 2021. [Unit-5]

2/27/2023 6
Syllabus Content
Reference Books:
• David M.Baezly , Python Essential Reference, 5th edition, Addison-
Wesley Professional;, 2021.
• John Shovic, Alan Simpson, Python for dummies. John Wiley & Sons,
2020.
Online References:
• Programming for Everybody (Getting Started with Python), Accessed on
02, April 2021 [Online] Available: https://www.coursera.org/learn/python
• Learn Python, Accessed on 02, April 2021 [Online] Available:
https://www.udemy.com/course/python-the-complete-python-developer-
course
• Introduction to Python Programming, Accessed on 02, April 2021 [Online]
Available: https://www.edx.org/professional-certificate/introduction-to-
python-programming
• The Python Standard Library, Accessed on 02, April 2021 [Online]
Available: https://docs.python.org/3/library
2/27/2023 7
Manipulating files & directories
Directories
 All files are contained within various directories.
 The os module has several methods that help you create, remove,
and change directories.
mkdir() – creates directories in current working directory.
chdir() – changes the current working directory.
rmdir() – deletes the directory. To ensure deletion all its contents should
be removed.
getcwd() – displays the current working directory.
Example code

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 8


os module
Link = https://docs.python.org/3/library/os.html
Some os library functions
os.name os.getegid() os.close(fd) os.ftruncate(fd, lengt
h)
os.ctermid() os.geteuid() os.device_encoding( os.lseek(fd, pos, ho
fd) w)
os.environ os.getlogin() os.dup(fd) os.pipe()
os.chdir(path) os.getppid() os.fchmod(fd, mode) os.get_terminal_size
(fd=STDOUT_FILEN
O)
os.getcwd() os.putenv(key, value os.fchown(fd, uid, gi os.get_handle_inheri
) d) table(handle)
os.fsencode(filenam os.umask(mask) os.fdatasync(fd) os.mkdir()
e)
os.fspath(path) os.uname() os.fpathconf(fd, nam os.rmdir()
e)
os.getenv(key, defau os.fdopen(fd, *args, os.fsync(fd) os.chroot(path)
lt=None) **kwargs)
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 9
Working with os module

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 10


sys module
 System-specific parameters and functions that provides access to
some variables used or maintained by the interpreter and to
functions that interact strongly with the interpreter.
 It is always available.
 3 file objects called standard input(stdin), standard output(stdout)
and standard error(stderr) are created when we start a python shell.
 These are accessible via the sys module.
sys.argv - retrieves user argument when your module is executable.
sys.path - tells you where Python is searching for modules on your
system.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 11


sys module
Link = https://docs.python.org/3/library/sys.html
Some of the system specific parameters and functions
sys.byteorder sys.displayhook(val sys.exc_value sys.float_info
ue)
sys.builtin_module_ sys.dont_write_byte sys.exc_traceback sys.float_repr_style
names code
sys.call_tracing(fun sys.excepthook(type sys.exec_prefix sys.getcheckinterval
c, args) , value, traceback) ()
sys.copyright sys.__displayhook_ sys.executable sys.getdefaultencodi
_ ng()
sys._clear_type_cac sys.exc_info() sys.exit([arg]) sys.getdlopenflags()
he()
sys._current_frames sys.exc_clear() sys.exitfunc sys.getfilesystemen
() coding()
sys.dllhandle sys.exc_type sys.flags sys.getrefcount(obje
ct)
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 12
Working with sys module

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 13


Working with sys module

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 14


Random module
• The random module is a built-in module to generate the pseudo-
random variables.

• It can be used perform some action randomly such as to get a


random number, selecting a random elements from a list, shuffle
elements randomly, etc.

Generate Random Floats


• The random.random() method returns a random float number
between 0.0 to 1.0. The function doesn't need any arguments.
>>> import random
>>> random.random()
0.645173684807533
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 15
Generate Random Integers
• The random.randint() method returns a random integer between the
specified integers.
>>> import random
>>> random.randint(1, 100) #95
>>> random.randint(1, 100) #49

• Generate Random Numbers within Range


• The random.randrange() method returns a randomly selected
element from the range created by the start, stop and step arguments.
The value of start is 0 by default. Similarly, the value of step is 1 by
default.
• >>> random.randrange(1, 10) #2
• >>> random.randrange(1, 10, 2) #5
• >>> random.randrange(0, 101, 10) # 80
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 16
Statistics Module
• The statistics module provides functions to mathematical statistics
of numeric data. The following popular statistical functions are
defined in this module.
Mean
The mean() method calculates the arithmetic mean of the numbers in a
list.
>>> import statistics
>>> statistics.mean([2,5,6,9]) #5.5
Median
The median() method returns the middle value of numeric data in a list.
>>> import statistics
>>>statistics.median([1,2,3,8,9]) #3
>>> statistics.median([1,2,3,7,8,9]) #5.0
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 17
Mode
• The mode() method returns the most common data point in the list.
>>> import statistics
>>> statistics.mode([2,5,3,2,8,3,9,4,2,5,6])
2
Standard Deviation
The stdev() method calculates the standard deviation on a given sample
in the form of a list.
>>> import statistics
>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5])
1.3693063937629153

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 18


Math Module
• Some of the most popular mathematical functions are defined in the
math module. These include trigonometric functions, representation
functions, logarithmic functions, angle conversion functions, etc. In
addition, two mathematical constants are also defined in this
module.

• Pi is a well-known mathematical constant, which is defined as the


ratio of the circumference to the diameter of a circle and its value is
3.141592653589793.
>>> import math
>>>math.pi
3.141592653589793

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 19


Math Module
• Another well-known mathematical constant defined in the math
module is e. It is called Euler's number and it is a base of the natural
logarithm. Its value is 2.718281828459045.
>>> import math
>>> math.e #2.718281828459045

>>> import math


>>> math.radians(30)
0.5235987755982988
>>> math.degrees(math.pi/6)
29.999999999999996

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 20


Math Module
>>> import math
>>> math.sin(0.5235987755982988)
0.49999999999999994
>>> math.cos(0.5235987755982988)
0.8660254037844387
>>> math.tan(0.5235987755982988)
0.5773502691896257

>>> import math


>>>math.log(10)
2.302585092994046
>>>math.log10(10)
1.0
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 21
Files
• Files are named locations on disk to store related information. They
are used to permanently store data in a non-volatile memory (e.g.
hard disk).
• Since Random Access Memory (RAM) is volatile (which loses its
data when the computer is turned off), we use files for future use of
the data by permanently storing them.
• When we want to read from or write to a file, we need to open it
first. When we are done, it needs to be closed so that the resources
that are tied with the file are freed.
• Hence, in Python, a file operation takes place in the following order:
– Open a file
– Read or write (perform operation)
– Close the file
Text Files and Binary Files

Types Of File in Python


• There are two types of files in Python and each of them are
explained below in detail with examples for your easy
understanding. They are:

• Binary file
• Text file
Binary files in Python
• All binary files follow a specific format. We can open some binary
files in the normal text editor but we can’t read the content present
inside the file. That’s because all the binary files will be encoded in
the binary format, which can be understood only by a computer or
machine.
• For handling such binary files we need a specific type of software to
open it.
• For Example, You need Microsoft word software to open .doc
binary files. Likewise, you need a pdf reader software to open .pdf
binary files and you need a photo editor software to read the image
files and so on.
Binary files in Python (cont…1)
• Most of the files that we see in our computer system are called
binary files.
• Example:
• Document files: .pdf, .doc, .xls etc.
• Image files: .png, .jpg, .gif, .bmp etc.
• Video files: .mp4, .3gp, .mkv, .avi etc.
• Audio files: .mp3, .wav, .mka, .aac etc.
• Database files: .mdb, .accde, .frm, .sqlite etc.
• Archive files: .zip, .rar, .iso, .7z etc.
• Executable files: .exe, .dll, .class etc.
Text files in Python
• A text file is usually considered as sequence of lines. Line is a
sequence of characters (ASCII), stored on permanent storage media.
Although default character coding in python is ASCII but supports
Unicode as well.
• in text file, each line is terminated by a special character, known as
End of Line (EOL). From strings we know that \n is newline
character.
• at the lowest level, text file is collection of bytes. Text files are stored
in human readable form.
• they can also be created using any text editor.
Text files in Python
• Text files don’t have any specific encoding and it can be opened in
normal text editor itself.
• Example:
• Web standards: html, XML, CSS, JSON etc.
• Source code: c, app, js, py, java etc.
• Documents: txt, tex, RTF etc.
• Tabular data: csv, tsv etc.
• Configuration: ini, cfg, reg etc.
Operations in Files
 Named locations on disk to store related information.
 They are used to permanently store data in a non-volatile memory.
 When we want to read from or write to a file, we need to open it
first.
 When we are done, it needs to be closed so that the resources that are
tied with the file are freed.
Hence, in Python, a file operation takes place in the following order:
- A.Opening a file.
- B.Reading or writing (perform operation).
- C.Closing the file.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 28


A.Opening a file
 A built-in open() function to open a file.
 This function returns a file object, also called a handle, as it is used
to read or modify the file accordingly.
 We can specify the mode while opening a file. In mode, we specify
whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file.
 Can also specify if we want to open the file in text mode or binary
mode.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 29


Opening or Creating a New File in
Python
• The method open() is used to open an existing file or creating a new
file. If the complete directory is not given then the file will be created
in the directory in which the python file is stored. The syntax for using
open() method is given below.
– Syntax:
– file_object = open( file_name, “Access Mode”, Buffering )
• The open method returns file object which can be stored in the name
file_object (file-handle).
• File name is a unique name in a directory. The open() function will
create the file with the specified name if it is not already exists
otherwise it will open the already existing file.
Opening Files in Python (cont…1)
• The access mode
it is the string which tells in what mode the file should be opened
for operations. There are three different access modes are available
in python.
• Reading: Reading mode is crated only for reading the file. The
pointer will be at the beginning of the file.
• Writing: Writing mode is used for overwriting the
information on existing file.
• Append: Append mode is same as the writing mode. Instead of
over writing the information this mode append the information
at the end.
• Below is the list of representation of various access modes in
python.
Opening Files in Python (cont…2)

Access modes in Text Files


• ‘r' – Read Mode: Read mode is used only to read data from the
file.
• ‘w' – Write Mode: This mode is used when you want to write data
into the file or modify it. Remember write mode overwrites the data
present in the file.
• ‘a' – Append Mode: Append mode is used to append data to the
file. Remember data will be appended at the end of the file
pointer.
• ‘r+' – Read or Write Mode: This mode is used when we want to
write or read the data from the same file.
• ‘a+' – Append or Read Mode: This mode is used when we want
to read data from the file or append the data into the same file.
Opening Files in Python (cont…3)

Access modes in Binary Files


• ‘wb’ – Open a file for write only mode in the binary format.
• ‘rb’ – Open a file for the read-only mode in the binary format.
• ‘ab’ – Open a file for appending only mode in the binary format.
• ‘rb+’ – Open a file for read and write only mode in the binary
format.
• ‘ab+’ – Open a file for appending and read-only mode in the
binary format.
Opening Files in Python (cont…4)

What is Buffering ?
• Buffering is the process of storing a chunk of a file in a temporary
memory until the file loads completely. In python there are different
values can be given. If the buffering is set to 0
, then the buffering is off. The buffering will be set to 1 when we
need to buffer the file.
Opening Files in Python (cont…5)

Examples Opening a file:


# open file in current directory
• f = open("test.txt“, “r”)

# specifying full path


• f = open(r“D:\temp\data.txt“, “r”)
– –raw string
• f = open(“D:\\temp\\data.txt“, “r”)
– -absolute path
B.Reading a file
 The default is reading in text mode. In this mode, we get strings
when reading from the file.
 Binary mode returns bytes and this is the mode to be used when
dealing with non-text files like images or executable files.

 The default encoding is platform dependent. In windows, it is


cp1252 but utf-8 in Linux.
when working with files in text mode, it is highly recommended to
specify the encoding type.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 36


Reading Information in the File

• In order to read a file in python, we must open the file in read mode.
• There are three ways in which we can read the files in python.
– read([n])
– readline([n])
– readlines() – all lines returned to a list
• Here, n is the number of bytes to be read.
Reading Information in the File
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read(5))

Output:
Hello

•Here we are opening the file test.txt in a read-only mode and are
reading only the first 5 characters of the file using the
my_file.read(5) method.
Reading Information in the File

Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read())
Output:
Hello World Hello
Python Good
Morning
Here we have not provided any argument inside the read()
function. Hence it will read all the content present inside the
file.
Reading Information in the File

Example 3:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline(2))

Output:
He

This function returns the first 2 characters of the next line.


Reading Information in the File

Example 4:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readline())

Output:
Hello World

Using this function we can read the content of the file on a


line by line basis.
Reading Information in the File

Example 5:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.readlines())

Output:
*‘Hello World\n’, ‘Hello Python\n’, ‘Good Morning\n’, ‘How are
You’+

Here we are reading all the lines present inside the text file
including the newline characters.
Reading Information in the File
Reading a specific line from a File
line_number = 4
fo = open(“C:/Documents/Python/test.txt”, ’r’)
currentline = 1
for line in fo:
if(currentline == line_number): print(line)
break
currentline = currentline +1
Output:
How are You
In the above example, we are trying to read only the 4th line
from the ‘test.txt’ file using a “for loop”.
Reading Information in the File

Reading the entire file at once


filename = “C:/Documents/Python/test.txt” filehandle =
open(filename, ‘r’)
filedata = filehandle.read()
print(filedata)

Output: Hello
World Hello
Python
Good Morning
How are You
B.Writing a file
 In order to write into a file in Python, we need to open it in write w,
append a or exclusive creation x mode.
 Need to be careful with the w mode, as it will overwrite into the file
if it already exists. Due to this, all the previous data are erased.
 Writing a string or sequence of bytes (for binary files) is done using
the write() method. This method returns the number of characters
written to the file.

 This program will create a new file named test.txt in the current
directory if it does not exist. If it does exist, it is overwritten.
 We must include the newline characters ourselves to distinguish the
different lines.
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 45
Write to a Python File

• In order to write data into a file, we must open the file in write
mode.
• We need to be very careful while writing data into the file as it
overwrites the content present inside the file that you are writing,
and all the previous data will be erased.
• We have two methods for writing data into a file as shown below.

– write(string)
– writelines(list)
• Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’
file.
Write to a Python File
Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.write(“Hello World\n”)
my_file.write(“Hello Python”)

•The first line will be ‘Hello World’ and as we have mentioned \n character, the
cursor will move to the next line of the file and then write ‘Hello Python’.
•Remember if we don’t mention \n character, then the data will be written
continuously in the text file like ‘Hello WorldHelloPython’

Example 3:
fruits = *“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”+
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.writelines(fruits)

The above code writes a list of data into the ‘test.txt’ file simultaneously.
Append in a Python File

To append data into a file we must open the file in


‘a+’ mode so that we will have access to both
the append as well as write modes.

Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“Strawberry”)

The above code appends the string ‘Strawberry’ at the end of the ‘test.txt’ file

Example 2:
my_file = open(“C:/Documents/Python/test.txt”, “a+”)
my_file.write (“\nGuava”)

The above code appends the string ‘Apple’ at the end of


the ‘test.txt’ file in a new line.
flush()function

• When we write any data to file, python hold everything in buffer


(temporary memory) and pushes it onto actual file later. If you want to
force Python to write the content of buffer onto storage, you can use
flush() function.
• Python automatically flushes the files when closing them i.e. it will be
implicitly called by the close(), BUT if you want to flush before closing
anyfile youcanuseflush()
flush()function
Nothing is
Example: working of flush()
in the file
Without flush() temp.txt

When you run the above code, program will


stopped at “Press any key”, for time being
don’t press any key and go to folder where file
“temp.txt” is created an open it to see what is in
the file till now

NOW PRESS ANY KEY….


Now content is stored,
because of close() function
contents are flushed and
pushed in file
flush()function
Example: working of flush() All contents
before flush()
With flush() are present in
file

When you run the above code, program will


stopped at “Press any key”, for time being don’t
press any key and go to folder where file
“temp.txt” is created an open it to see what is in
the file till now
NOW PRESS ANY KEY….
Rest of the content is
written because of close(),
contents are flushed and
pushed in file.
Removing whitespaces after reading
from f ile

• read() and readline() reads data from file and return it in the
form of string and readlines() returns data in the form of list.
• All these read function also read leading and trailing
whitespaces, new line characters. If you want to remove these
characters you can use functions
– strip() : removes the given character from both ends.
– lstrip(): removes given character from left end
– rstrip(): removes given character from right end
Removing w hitespaces after readin g
f rom file
• Example: strip(), lstrip(), rstrip()
Fi l e P o i n t e r

• Every file maintains a file pointer which tells the current position in
the file where reading and writing operation will take.
• When we perform any read/write operation two things happens:
– The operation at the current position of file pointer
– File pointer advances by the specified number of bytes.
Fi l e Po i nte r
myfile = open(“ipl.txt”,”r”)

File pointer will be by default at first position i.e. first character

ch = myfile.read(1
ch will store first character i.e. first character is
consumed, and file pointer will move to next character
C.Closing the file
 When we are done with performing operations on the file, we need
to properly close the file.
 Closing a file will free up the resources that were tied with the file.
 This is done using the close() method available in Python.
 Python has a garbage collector to clean up unreferenced objects but
we must not rely on it to close the file.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 56


Closing Files in Python

• After processing the content in a file, the file must be saved and
closed. To do this we can use another method close() for closing the
file. This is an important method to be remembered while handling
files in python.
• Syntax: file_object.close()

string = "This is a String in Python" my_file =


open(my_file_name.txt,"w+",1) my_file.write(string)
my_file.close()
print(my_file.closed)
File Handling Methods

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 58


Open & Write Operation in a file
Program

Output

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 59


Open & Read Operation in a file
Program

Output

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 60


Append Operation in a file
Program

Output

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 61


Reading Numbers in a text file
Program

Input File Output

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 62


Binary file oper a tions

• If we want to write a structure such as list or dictionary to a file and


read it subsequently we need to use the Python module pickle.
Pickling is the process of converting structure to a byte stream before
writing to a file and while reading the content of file a reverse
process called Unpickling is used to convert the byte stream back to
the original format.
Binary file oper a tions

• First we need to import the module called


pickle.
• This module provides 2 main functions:
– dump() : to write the object in file which is loaded in binary
mode
• Syntax : dump(object_to_write, filehandle)
– load() : dumped data can be read from file using load() i.e. it
is used to read object from pickle file.
• Syntax: object = load(filehandle)
Binary file oper a tions

• Example dump()
:

See the content is some kind of


encrypted format, and it is not in
complete readable form
Binary file oper a tions

• Example: load()
Binary file oper a tions
The four major operations performed using a binary file are—
• 1. Inserting/Appending a record in a binary file
• 2. Reading records from a binary file
• 3. Searching a record in a binary file
• 4. Updating a record in a binary file
Binary file oper a tions

• Inserting/Appending a record in a binary file


• Inserting or adding (appending) a record into a binary file
requires importing pickle module into a program followed by
dump() method to write onto the file.
Binary file oper a tions
• Inserting/Appending a record in a binary file
Binary file oper a tions
• Reading a record from a binary file

• deals with reading the contents from binary file student


using load() method of pickle module. It is used to read the
object from the opened file. The syntax for this is given by
the statement—
– object = pickle.load(file)
Binary file oper a tions
• Reading a record from a binary file
Binary file oper a tions

• Searching a record in a binary file

• Searching the binary file ( "student" )is carried out on


the basis of the roll number entered by the user. The
file is opened in the read-binary mode and gets stored
in the file object, f. load() method is used to read the
object from the opened file. A variable ‘found’ is used
which will tell the status of the search operation being
successful or unsuccessful. Each record from the file is
read and the content of the field, roll no, is compared
with the roll number to be searched. Upon the search
being successful, appropriate message is displayed to
the user.
Binary file oper a tions
• Searching a record in a binary file
Binary file oper a tions

• Updating a record in a binary file


• Updating a record in the file requires roll number (search field) to be
fetched from the user whose name (Record) is to be updated

• Once the record is found, the file pointer is moved to the beginning
of the file using seek(0) statement, and then the changed values are
written to the file and the record is updated. seek() method is used for
random access to the file.
B i n a r y f i l e o p e rat i o n s cont…5g
• Updating a record in a binary file
B i n a r y f i l e o p e rat i o n s cont…6

• RANDOM ACCESS IN FILES USING TELL() AND


SEEK()

• Till now, in all our programs we laid stress on the


sequential processing of data in a text and
binary file.

• But files in Python allow random access of the


data as well using built-in methods seek() and
tell().
B i n a r y f i l e o p e rat i o n s cont…6a
• RANDOM ACCESS IN FILES USING TELL() AND SEEK()

• seek()—seek() function is used to change the position of the file


handle (file pointer) to a given specific position. File pointer is
like a cursor, which defines from where the data has to be read
or written in the file.
• Python file method seek() sets the file’s current position at the
offset. This argument is optional and defaults to 0, which means
absolute file positioning. Other values are: 1, which signifies seek
is relative (may change) to the current position, and 2, which
means seek is relative to the end of file. There is no return value.
• The reference point is defined by the “from_what” argument. It
can have any of the three values:
• 0: sets the reference point at the beginning of the file, which is by
default.
• 1: sets the reference point at the current file position.
• 2: sets the reference point at the end of the file.
Binary file operations cont…6a_1
• RANDOM ACCESS IN FILES USING TELL() AND SEEK()

• seek() can be done in two ways:


– Absolute Positioning
– Relative Positioning
• Absolute referencing using seek() gives the file
number on which the file pointer has to position
itself. The syntax for seek() is—
– f.seek(file_location) #where f is the file pointer
• For example, f.seek(20) will give the position or file
number where the file pointer has been placed. This
statement shall move the file pointer to 20th byte in
the file no matter where you are.
Binary file operations cont…6a_2
• RANDOM ACCESS IN FILES USING TELL() AND SEEK()

• Relative referencing/positioning has two arguments,


offset and the position from which it has to traverse. The
syntax for relative referencing is:
– f.seek(offset, from_what) #where f is file pointer, For example,
– f.seek(–10,1) from current position, move 10 bytes backward
– f.seek(10,1) from current position, move 10 bytes forward
– f.seek(–20,1) from current position, move 20 bytes backward
– f.seek(10,0) from beginning of file, move 10 bytes forward
B i n a r y f i l e o p e rat i o n s cont…6b
• RANDOM ACCESS IN FILES USING TELL() AND SEEK()

• tell()—tell() returns the current position of the file


read/write pointer within the file. Its syntax is:
• f.tell() #where f is file pointer

• When we open a file in reading/writing mode, the file


pointer rests at 0th byte.
• When we open a file in append mode, the file pointer
rests at the last byte.
• This is illustrated in the practical implementation that
follows:
B i n a r y f i l e o p e rat i o n s cont…7
• RANDOM ACCESS IN FILES USING TELL() AND SEEK()
Creating and Reading a formatted file
 As a data scientist, you handle a lot of data daily.
 This data could be from multiple sources like from databases, from
Excel to flat files, from public websites like kaggle.
 Not just sources it could be in any file format like .csv, .txt, .parquet,
etc.
 Before you start making sense of the data, you will need to know the
basic three things: how to open, read and write data into flat files so
that you can then perform analyses on them.
Flat Files vs. Non-Flat Files
 Flat files are data files that contain records with no structured
relationships between the records, and there's also no structure for
indexing like you typically find it in relational databases.
e.g.TSV, CSV
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 82
Flat vs Non-flat Files
 A non-flat file is a file where an index is assigned to every record.
 The exact location of the record can be known using the index of that
record.
 You would normally need some applications like a database
management system to read this type of file. e.g. XML.
Flat Files
Comma Separated Values (CSV) files, which contain data values that
are separated by ',' e.g.Name,Address,Email.
 Delimited files, which contain data values with a user-specified
delimiter. This can be a \t tab or a symbol (#,&,||)
e.g.Name||Address||Email.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 83


Writing a CSV File
writer()
 This function in csv module returns a writer object that converts data
into a delimited string and stores in a file object.
 Needs a file object with write permission as a parameter.
 Every row written in the file issues a newline character.
 To prevent additional space between lines, newline parameter is set
to ‘ ’.
The writer class has following methods
 writerow() - writes items in an iterable (list, tuple or string),
separating them by comma character.
 writerows() - takes a list of iterables as parameter and writes each
item as a comma separated line of items in the file.
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 84
Writing a CSV File

 Instead of iterating over the list to write each row individually, we


can use writerows() method.

2/27/2023 Mr.A.Arul Prasath, AP/CSE, Vel Tech 85


Reading a CSV File
reader()
 This function returns a reader object which returns an iterator of
lines in the csv file.

 Built-in next() function is useful to display all lines in a csv file.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 86


dialect()
 The csv module also defines a dialect class.
 Dialect is set of standards used to implement CSV protocol.
 The list of dialects available can be obtained by:
>>> csv.list_dialects() function.
 The Dialect class is a container class relied on primarily for its
attributes, which are used to define the parameters for a specific
reader or writer instance.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 87


Writing dictionary in a CSV File
DictWriter()
 This function returns a DictWriter object. It is similar to writer
object, but the rows are mapped to dictionary object.
 Needs a file object with write permission and a list of keys used in
dictionary as fieldnames parameter.
 Fieldnames used to write first line in the file as header.
writeheader()
 Writes list of keys in dictionary as a comma separated line as first
line in the file.

2/27/2023 Mr.A.Arul Prasath, AP/CSE, Vel Tech 88


Reading dictionary from a CSV File
DictReader()
 This function returns a DictReader object from the underlying CSV
file.
 As in case of reader object, this one is also an iterator, using which
contents of the file are retrieved.
 The class provides fieldnames attribute, returning the dictionary
keys used as header of file.
 Use loop over the DictReader object to fetch individual dictionary
objects.
 The results will be an OrderedDict.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 89


OrderedDict to Dictionary & TSV File
 To convert OrderedDict object to normal dictionary, we have to first
import OrderedDict from collections module.

TSV read & write on CSV Files

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 90


String Manipulations
 A string is a list of characters in order.
 A character is anything you can type on the keyboard in one
keystroke, like a letter, a number, or a backslash.
 Strings can have spaces: e.g."hello world".
 An empty string is a string that has 0 characters.
 Python strings are immutable.
 Python recognize as strings everything that is delimited by quotation
marks (” ” or ‘ ‘).
 To manipulate strings, we can use some of Pythons built-in methods.
Creation Output
word = "Hello World“ Hello World
print(word)
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 91
len() & find()
Accessing - Use [ ] to access characters in a string.
Str = "Hello World" Output
letter=Str[0] H
print(letter)
Length – computes number of characters in a string including space.
Output
len(Str) 11
Find – finds index specified character or substring
print(Str.count(‘l’)) 3
print(Str.find(‘H’)) 0
print(Str.index(‘World’)) 6
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 92
count() & slicing
count()
 Counts for specified letter in string
s =“Hello World"
>>> print s.count(‘l')
3
Slicing
 Use [ : ] to get set of letters from a string.
 Keep in mind that python, as many other languages, starts to count
from 0.
word[start:end] # items start through end -1.
word[start:] # items start through the rest of the list.
word[:end] # items from the beginning through end -1.
word[:] # a copy of the whole list.
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 93
Slicing in string
word = "Hello World“

print word[0] #get one char of the word


print word[0:1] #get one char of the word (same as above)
print word[0:3] #get the first three char
print word[:3] #get the first three char
print word[-3:] #get the last three char
print word[3:] #get all but the three first char
print word[:-3] #get all but the three last character
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 94
split(), startswith() & endswith()
split() – splits string on whitespaces.
word = "Hello World"
>>> word.split(' ')
['Hello', 'World']
startswith() – Checks whether the given string starts with specified
character(s)
word.startswith("H")
True
endswith() - Checks whether the given string ends with specified
character(s)
>>> word.endswith("d")
True
>>> word.endswith("w")
False
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 95
Repeat Strings, replace() & casechange
Can use certain mathematical operators(*) on strings.
>>> print("." * 10) # prints ten dots
..........
replace() – replaces specified character(s) with another.
>>> word.replace("Hello", "Goodbye")
'Goodbye World‘
Case change for strings
upper() – converts the given string to upper case.
lower() – converts the given string to lower case.
title() - converts the given string to title case.
capitalize() - converts the first letter of given string to upper case.
swapcase() – converts lowercase strings to uppercase and vice-versa.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 96


Case change & reversed()
string=‘Hello World’
>>> print (string.lower())
hello world
>>> print (string.title())
Hello World
>>> print (string.capitalize())
Hello world
>>> print (string.swapcase())
hELLO wORLD
reversed() – reverses the given string.
>>> print (' '.join(reversed(string)))
dlroW olleH

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 97


strip()
 Python strings have the strip(), lstrip(), rstrip() methods for removing
any character from both ends of a string.
- strip() #removes from both ends
- lstrip() #removes leading characters (Left-strip)
- rstrip() #removes trailing characters (Right-strip)
 If the characters to be removed are not specified then white-space
will be removed.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 98


concatenation() & join()
Concatenation – combines two different strings as one.
 To concatenate strings in Python use the “+” operator.
"Hello " + "World“

join() – adds specified value inside the string


>>> print (":".join(word)) # #add a : between every char
H:e:l:l:o: :W:o:r:l:d

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 99


Testing functions on Strings
 A string in Python can be tested for boolean value.

word = "Hello World"


word.isalnum() #check if all char are alphanumeric
word.isalpha() #check if all char in the string are alphabetic
word.isdigit() #test if string contains digits
word.istitle() #test if string contains title words
word.isupper() #test if string contains upper case
word.islower() #test if string contains lower case
word.isspace() #test if string contains spaces
word.endswith('d') #test if string endswith a d
word.startswith('H') #test if string startswith H

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 100


7.Strings & Number System
 An integer can be stored using different types. Two possible Python
data types for representing an integer are:1.str and 2.int.
 s="110" - python understands that you want to store the integer 110
as a string.
 a=110 - follows by default decimal number system
 There are several other number systems, such as binary and
hexadecimal, which use different bases to represent an integer.
 All integer prefixes are in the form 0?, in which you replace ? with a
character that refers to the number system:
- b: binary (base 2)
- o: octal (base 8)
- d: decimal (base 10)
- x: hexadecimal (baseMs.S.
2/27/2023
16) Shanmuga Priya, AP/CSE, Vel Tech 101
Representation of Integers using String & Int

 int assumes the literal integer to be decimal.

 The string representation of an integer is more flexible because a


string holds arbitrary text data

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 102


Converting a python string to an int
 To convert a decimal integer represented as a string, you have to just
pass the string to int(), which returns a decimal integer.
 By default, int() assumes that the string argument represents a
decimal integer.

 If, however, you pass a hexadecimal string to int(), then you’ll see a
ValueError.
 The error message says that the string is not a valid decimal integer.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 103


Errors in Conversion

 The difference between two types of failed results of passing a string


to int().
 Syntax Error: A Value Error will occur when int() doesn’t know
how to parse the string using the provided base (10 by default).
 Logical Error: int() does know how to parse the string, but not the
way you expected.
 You meant for the result to be 210, which is the decimal
representation of the binary string.
 Unfortunately, because you didn’t specify that behavior, int()
assumed that the string was a decimal integer.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 104


Number system conversion
 One good safeguard for this behavior is to always define your string
representations using explicit bases.
 When you pass a string to int(), you can specify the number system
that you’re using to represent the integer.
 The way to specify the number system is to use base.
 Now, int() understands you are passing a hexadecimal string and
expecting a decimal integer.

 The argument that you pass to base is not limited to 2, 8, 10, and 16.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 105


Converting a python int into string
 You can convert a Python int to a string using str().
 By default, str() behaves like int() in that it results in a decimal
representation.

 str() is smart enough to interpret the binary literal and convert it to a


decimal string.

 If you want a string to represent an integer in another number


system, then you use a formatted string, such as an f-string (in
Python 3.6+), and an option that specifies the base

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 106


Regular Expression
 A Regular Expression (RegEx) is a sequence of characters that
defines a search pattern. For example,

^a...s$
 The above code defines a RegEx pattern. The pattern is: any five
letter string starting with a and ending with s.

Expression String Matched?


abs No match
alias Match
^a...s$ abyss Match
Alias No match
An abacus No match

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 107


Regular Expression Example

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 108


Specify Pattern Using RegEx
• To specify regular expressions, metacharacters are used
• 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.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 109


Specify Pattern Using RegEx
• 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].

• 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.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 110


Specify Pattern Using RegEx
• 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].

• 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.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 111


Specify Pattern Using RegEx
. – Period
• A period matches any single character (except newline '\n').

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 112


Specify Pattern Using RegEx
^ - Caret
• The caret symbol ^ is used to check if a string starts with a
certain character.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 113


Specify Pattern Using RegEx
$ - Dollar
• The dollar symbol $ is used to check if a string ends with a
certain character.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 114


Specify Pattern Using RegEx
*- Star
• The star symbol * matches zero or more occurrences of the
pattern left to it.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 115


Specify Pattern Using RegEx
+ - Plus
• The plus symbol + matches one or more occurrences of the
pattern left to it.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 116


Specify Pattern Using RegEx
? - Question Mark
• The question mark symbol ? matches zero or one occurrence
of the pattern left to it.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 117


Specify Pattern Using RegEx
{} - Braces
• Consider this code: {n,m}. This means at least n, and at
most m repetitions of the pattern left to it.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 118


Specify Pattern Using RegEx
| - Alternation
• Vertical bar | is used for alternation (or operator).

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 119


Specify Pattern Using RegEx
() - 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

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 120


Specify Pattern Using RegEx
\ - 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.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 121


Specify Pattern Using RegEx
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.
• \b - Matches if the specified characters are at the beginning or
end of a word.
• \B - Opposite of \b. Matches if the specified characters
are not at the beginning or end of a word
• \d - Matches any decimal digit. Equivalent to [0-9]
• \D - Matches any non-decimal digit. Equivalent to [^0-9]

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 122


Specify Pattern Using RegEx
\s - Matches where a string contains any whitespace character.
Equivalent to [ \t\n\r\f\v].

\S - Matches where a string contains any non-whitespace


character. Equivalent to [^ \t\n\r\f\v].
\w - Matches any alphanumeric character (digits and alphabets).
Equivalent to [a-zA-Z0-9_]. By the way, underscore _ is also
considered an alphanumeric character.
\W - Matches any non-alphanumeric character. Equivalent
to [^a-zA-Z0-9_]
\Z - Matches if the specified characters are at the end of a string.

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 123


Special Sequences
Example:

import re
txt = "The rain in Spain"
#Check if the string starts with "The":
x = re.findall("\AThe", txt)
print(x)
if x:
print("Yes, there is a match!")
else:
print("No match")

OUTPUT:
['The']
Yes, there is a match!

2/27/2023 124
Special Sequences
Example:
txt = "The rain in Spain"

x = re.findall(r"\bain", txt) # [] No match


x = re.findall(r"ain\b", txt) #['ain', 'ain'] Yes, there is at least one match!
x = re.findall(r"ain\B", txt) # [] No match
x = re.findall(r"\Bain", txt) #['ain', 'ain'] Yes, there is at least one match!
x = re.findall("\d", txt) # [] No match
x = re.findall("\D", txt) #['T', 'h', 'e', ' ', 'r', 'a', 'i', 'n', ' ', 'i', 'n', ' ', 'S', 'p', 'a', 'i', 'n']
#Yes, there is at least one match!
x = re.findall("\s", txt) # [' ', ' ', ' '] Yes, there is at least one match!
x = re.findall("\S", txt) #['T', 'h', 'e', 'r', 'a', 'i', 'n', 'i', 'n', 'S', 'p', 'a', 'i', 'n']
#Yes, there is at least one match!
x = re.findall("\w", txt) # ['T', 'h', 'e', 'r', 'a', 'i', 'n', 'i', 'n', 'S', 'p', 'a', 'i', 'n']
#Yes, there is at least one match!
x = re.findall("\W", txt # [' ', ' ', ' '] Yes, there is at least one match!
x = re.findall("\Z", txt) #['Spain'] Yes, there is a match!

2/27/2023 125
Sets
A set is a set of characters inside a pair of square brackets [] with a special meaning:
Set Description
[arn] Returns a match where one of the specified characters (a, r, or n) is
present
[a-n] Returns a match for any lower case character, alphabetically
between a and n
[^arn] Returns a match for any character EXCEPT a, r, and n

[0123] Returns a match where any of the specified digits (0, 1, 2, or 3) are present

[0-9] Returns a match for any digit between 0 and 9

[0-5][0-9] Returns a match for any two-digit numbers from 00 and 59

[a-zA-Z] Returns a match for any character alphabetically between a and z, lower
case OR upper case
[+] In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a
match for any + character in the string
2/27/2023 126
Sets
Example:
txt = "The rain in Spain"

x = re.findall("[arn]", txt) # ['r', 'a', 'n', 'n', 'a', 'n']

x = re.findall("[^arn]", txt) # ['T', 'h', 'e', ' ', 'i', ' ', 'i', ' ', 'S', 'p', 'i']

x = re.findall("[a-n]", txt) #['h', 'e', 'a', 'i', 'n', 'i', 'n', 'a', 'i', 'n']

2/27/2023 127
Specify Pattern Using RegEx
re.findall()
• The re.findall() method returns a list of strings containing all
matches.

# Program to extract numbers from a string


import re
string = 'hello 12 hi 89. Howdy 34'
pattern = '\d+‘
result = re.findall(pattern, string)
print(result)
# Output: ['12', '89', '34']

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 128


Specify Pattern Using RegEx
re.split()
• The re.split method splits the string where there is a match
and returns a list of strings where the splits have occurred.

import re
string = 'Twelve:12 Eighty nine:89.'
pattern = '\d+‘
result = re.split(pattern, string)
print(result)
# Output: ['Twelve:', ' Eighty nine:', '.']

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 129


Specify Pattern Using RegEx
re.sub()
• The syntax of re.sub() is:
re.sub(pattern, replace, string)

import re
string = 'abc 12 e 23 f45 6‘
# matches all whitespace characters
pattern = '\s+‘
# empty string
replace = '‘
new_string = re.sub(pattern, replace, string)
print(new_string)
# Output: abc12de23f456

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 130


Specify Pattern Using RegEx
re.subn()
• The re.subn() is similar to re.sub() except it returns a tuple of 2 items
containing the new string and the number of substitutions made.

# Program to remove all whitespaces


import re
# multiline string
string = 'abc 12\
de 23 \n f45 6‘
# matches all whitespace characters
pattern = '\s+‘
# empty string
replace = '‘
new_string = re.subn(pattern, replace, string)
print(new_string)
# Output: ('abc12de23f456', 4)
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 131
Specify Pattern Using RegEx
re.search()
• The re.search() method takes two arguments: a pattern and a string. The
method looks for the first location where the RegEx pattern produces a
match with the string.
• If the search is successful, re.search() returns a match object; if not, it
returns None.
• match = re.search(pattern, str)
import re
string = "Python is fun“
# check if 'Python' is at the beginning
match = re.search('\APython', string)
if match:
print("pattern found inside the string")
else:
print("pattern not found")
# Output: pattern found inside the string
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 132
Specify Pattern Using RegEx
match.group()
• The group() method returns the part of the string where there is a match.

import re
string = '39801 356, 2102 1111‘
# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})‘
# match variable contains a Match object.
match = re.search(pattern, string)
if match:
print(match.group())
else:
print("pattern not found")
# Output: 801 35

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 133


Specify Pattern Using RegEx
match.group()
• The group() method returns the part of the string where there is a match.

import re
string = '39801 356, 2102 1111‘
# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})‘
# match variable contains a Match object.
match = re.search(pattern, string)
if match:
print(match.group())
else:
print("pattern not found")
# Output: 801 35

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 134


Span(), String(),group() Function
• span() returns a tuple containing the start-, and end positions of the
match..
• String () returns the string passed into the function.
• group() returns the part of the string where there was a match

2/27/2023 135
Example
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span()) #OUTPUT: (12,19)
Y = re.search(r"\bS\w+", txt)
print(Y.string) # OUTPUT: The rain in Spain
z = re.search(r"\bS\w+", txt)
print(z.group()) # output: Spain

2/27/2023 136
Python Decorators
• As mentioned earlier, A Python decorator is a function that takes in a
function and returns it by adding some functionality.
• In fact, any object which implements the special __call__() method is
termed callable. So, in the most basic sense, a decorator is a callable that
returns a callable.
• Basically, a decorator takes in a function, adds some functionality and
returns it.
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
# Output: I am ordinary

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 137


Python Decorators
Here, we have created two functions:
• ordinary() that prints "I am ordinary"
• make_pretty() that takes a function as its argument and has a nested
function named inner(), and returns the inner function.
• We are calling the ordinary() function normally, so we get the output "I am
ordinary". Now, let's call it using the decorator function.
def make_pretty(func):
# define the inner function
def inner():
# add some additional behavior to decorated function
print("I got decorated") # define ordinary function
# call original function def ordinary():
print("I am ordinary")
func() # decorate the ordinary function
# return the inner function decorated_func = make_pretty(ordinary)
return inner # call the decorated function
decorated_func()
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 138
Python Decorators
@ Symbol With Decorator
• Instead of assigning the function call to a variable, Python provides a much
more elegant way to achieve this functionality using the @ symbol. For
example,
def make_pretty(func):

def inner():
print("I got decorated")
func()
return inner

@make_pretty
def ordinary():
print("I am ordinary")

ordinary()
2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 139
Decorating Functions with Parameters

def smart_divide(func):
def inner(a, b):
print("I am going to divide", a, "and", b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a, b)
return inner
@smart_divide
def divide(a, b):
print(a/b)
divide(2,5)
divide(2,0)

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 140


Thank You

2/27/2023 Ms.S. Shanmuga Priya, AP/CSE, Vel Tech 141

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy