Python Programming: CH - 3 Standard Library and Regular Expression

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Python Programming

Hitakshi Patel,
Ch - 3 Standard Assistant
Library Professor
and Regular Expression
Computer Science and Engineering, PIET
CHAPTER-3
Standard Library and Regular Expression
Standard Library and Modules
Python standard library provides
built in modules to use directly

Built in modules written in C:


provides access to system
functionality

Built in modules written in


python: provides standardized
solutions for common real
problems

Image source :
http://www.chaoxinto
Math for Mathematical Task
Import math library using : ‘import math’
List out various methods and constants using : ‘dir(math)’

Overview of module

Figure 5.1 :math


module overview
Playing with math
cmath for complex mathematics
Playing with random
OS Module

● The OS Module in python provides functions for interacting with


the operating system.
● OS comes under Python standard utility modules.
● Most of the functions in this module are implemented by platform
specific modules , such as posix and nt.
● The os module automatically loads the right implementation
module when it is first imported.
OS Module
OS Module
OS Module: getcwd()

returns a string representing the current working directory

>>> import os

>>> curr_dir= os.getcwd()

>>> print curr_dir

Output:
C:\Users\Hitakshi\Python\test
OS Module: Make and change directories
>>> import os
>>> curr_dir= os.getcwd()
>>> print curr_dir

>>> os.mkdir(os.path.join(curr_dir,’test1’))
>>> os.chdir(os.path.join(curr_dir,’test1’))

Output:
C:\Users\Hitakshi\Python\test
C:\Users\Hitakshi\test\test1
OS Module: List all file in directory
>>> import os

>>> print os.listdir(os.getcwd())

Returns a list containing the names of the entries in the directory


given by path
Sys Module

sys module provides a number of functions and variables that can


be used to manipulate different parts of Python runtime
environment
Sys Module: Command-line arguments
Sys Module: Command-line arguments
Sys Module: Command-line arguments
Sys Module: Exiting the program
● When you reach at the end of the main program, the
interpreter is automatically terminated.
● If you need to exit in midflight, you can call the sys.exit
function instead
● the function takes an optional integer value, which is
returned to the calling program
● If it is an integer, zero is considered “successful termination”
and any nonzero value is considered “abnormal termination”
Sys Module: Exiting the program
Machine Learning Library
Make your computer intelligent
Get your machine learning library installed on python tool to use it
Statistics is Fun

Machine learning
library for
statistics
Solving Linear Algebra
Plot your Data
Graphics with Turtle
Regular Expression
Regular Expression

Sequence of characters that forms a pattern to search


Referred to as ‘regex’ or ‘regexp’
Module named as : ‘re’
Powerful
Regexp Quick Guide

^ To Match the beginning of a line


$ To Match the end of the line
. To Match any character
\s To Match whitespace
\S To Match any non-whitespace character
\d To Match a decimal digit[0-9]
\D To Match non digits
\w To Match alphanumeric character class[a-zA-Z0-9_]
\W To Match non-alphanumeric character class
Regexp Quick Guide

* It Repeats a character zero or more times


*? It Repeats a character zero or more times (non-greedy)
+ It Repeats a character one or more times
+? It Repeats a character one or more times (non-greedy)
[aeiou] It Matches a single character in the listed set
[^XYZ] It Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
Searching Data having Specific Pattern
re.search(<re pattern> , <source_string>)
Searching Data having Specific Pattern
Search a line starting with ‘Hello’ using search() method
Let’s create regular
expression
^Hello

Matches the
beginning of line
Matching and Extracting Data
Match & find the position of the word starting with a
particular character
Matching and Extracting Data
Extract numbers from line using findall() method
Matches the number
re.findall(<re pattern> , <source_string>) from given sequence

[0-9]+

Repeat for one or


more time
Splitting Data
Split your statement with particular pattern or word using split() method
re.split(<re pattern> , <source_string>)
Splitting Data
Splitting Data
Substituting Data
Replacing old word with new word using sub() method
re.sub(<old_word> , <new_word> , <source_string>)
Substituting Data
We can also control the number of replacements by specifying the Count Parameter
Substituting Data
Bored of writing regular expression every time??
Use re.compile() to make regular expression object
re.compile(<RegExp>)
Some Useful Regular Expression
regular expression for email id verification
Matches ‘@’ sign

‘\S+@\S
+’ At least one
non-whitespace
regular expression for mobile number verification character

'[0-9]{10}’ Length of 10 digit

regular expression for mobile number verification


Check the dollar sign At least one digit
using escape '\$[0-9.]+’ after dollar sign
character
www.paruluniversity.ac.in

You might also like