SlideShare a Scribd company logo
1 of 27
Download to read offline
Module 3
Regular Expressions, Dictionaries
1
22MCSIT201 – Python Programming
Master of Science in Computer Science & Information Technology
[MSc-CSIT]
• 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
Raghavendra R Assistant Professor School of CS & IT 2
RegEx
Unit 3
Regular Expressions,
Dictionaries
Function Description
match This method matches the regex pattern in the string with the optional
flag. It returns true if a match is found in the string otherwise it
returns false.
search This method returns the match object if there is a match found in the
string.
findall It returns a list that contains all the matches of a pattern in the string.
split Returns a list in which the string has been split in each match.
sub Replace one or many matches in the string.
Raghavendra R Assistant Professor School of CS & IT 3
Regex Functions
Unit 3
Regular Expressions,
Dictionaries
The following regex functions are used in the python.
Meta-Characters
• Metacharacter is a character with the specified meaning.
Raghavendra R Assistant Professor School of CS & IT 4
Forming a regular expression
Unit 3
Regular Expressions,
Dictionaries
Metach
aracter
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. “Py.h."
^ It represents the pattern present at the beginning of the string. "^Python"
$ It represents the pattern present at the end of the string. “program"
* 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. “Python{2}"
| It represents either this or that character is present. “Python|program"
() Capture and group
Special Sequences
• Special sequences are the sequences containing  followed by one of the
characters.
Raghavendra R Assistant Professor School of CS & IT 5
Forming a regular expression
Unit 3
Regular Expressions,
Dictionaries
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.
B It returns a match if the specified characters are present at the beginning of the string but not
at the end.
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.
• A set is a group of characters given inside a pair of square brackets. It
represents the special meaning.
Raghavendra R Assistant Professor School of CS & IT 6
Forming a regular expression
Unit 3
Regular Expressions,
Dictionaries
Set Description
[arn] Returns a match if the string contains any of the specified characters
in the set.
[a-n] Returns a match if the string contains any of the characters between a
to n.
[^arn] Returns a match if the string contains the characters except a, r, and n.
[0123] Returns a match if the string contains any of the specified digits.
[0-9] Returns a match if the string contains any digit between 0 and 9.
[0-5][0-9] Returns a match if the string contains any digit between 00 and 59.
[a-zA-Z] Returns a match if the string contains any alphabet (lower-case or
upper-case).
• 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.
import re
string = 'hello 12 hi 89. Howdy 34'
pattern = 'd+'
result = re.findall(pattern, string)
print(result)
Output: ['12', '89', '34']
Raghavendra R Assistant Professor School of CS & IT 7
findall() function
Unit 3
Regular Expressions,
Dictionaries
• 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:', '.']
Raghavendra R Assistant Professor School of CS & IT 8
re.split()
Unit 3
Regular Expressions,
Dictionaries
• You can pass maxsplit argument to the re.split() method. It's the maximum
number of splits that will occur.
import re
string = 'Twelve:12 Eighty nine:89 Nine:9.'
pattern = 'd+'
# maxsplit = 1
# split only at the first occurrence
result = re.split(pattern, string, 1)
print(result)
# Output: ['Twelve:', ' Eighty nine:89 Nine:9.']
Raghavendra R Assistant Professor School of CS & IT 9
Unit 3
Regular Expressions,
Dictionaries
• re.sub(pattern, replace, string)
• The method returns a string where matched occurrences are replaced with
the content of replace variable.
# 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.sub(pattern, replace, string)
print(new_string) # Output: abc12de23f456
Raghavendra R Assistant Professor School of CS & IT 10
re.sub()
Unit 3
Regular Expressions,
Dictionaries
• 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)
Raghavendra R Assistant Professor School of CS & IT 11
re.search()
Unit 3
Regular Expressions,
Dictionaries
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
Raghavendra R Assistant Professor School of CS & IT 12
Unit 3
Regular Expressions,
Dictionaries
• You can get methods and attributes of a match object using dir() function.
• Some of the commonly used methods and attributes of match objects are:
match.group()
• The group() method returns the part of the string where there is a match.
import re
Num =
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
Raghavendra R Assistant Professor School of CS & IT 13
Match object
Unit 3
Regular Expressions,
Dictionaries
• When r or R prefix is used before a regular expression, it means raw string.
• For example, 'n' is a new line whereas r'n' means two characters:
a backslash  followed by n.
• Backlash  is used to escape various characters including all
metacharacters.
• However, using r prefix makes  treat as a normal character.
import re
string = 'n and r are escape sequences.'
result = re.findall(r'[nr]', string)
print(result)
# Output: ['n', 'r']
Raghavendra R Assistant Professor School of CS & IT 14
Using r prefix before RegEx
Unit 3
Regular Expressions,
Dictionaries
Raghavendra R Assistant Professor School of CS & IT 15
Unit 3
Regular Expressions,
Dictionaries
• Python dictionary is an unordered collection of items.
• While other compound data types have only value as an element, a
dictionary has a key: value pair.
• Dictionaries are optimized to retrieve values when the key is known.
How to create a dictionary?
• Creating a dictionary is as simple as placing items inside curly braces {}
separated by comma.
• An item has a key and the corresponding value expressed as a pair, key:
value.
• While values can be of any data type and can repeat, keys must be of
immutable type (string, number or tuple with immutable elements) and
must be unique.
Raghavendra R Assistant Professor School of CS & IT 16
Dictionary
Unit 3
Regular Expressions,
Dictionaries
# empty dictionary
my_dict = {}
# dictionary with integer keys
my_dict = {1: 'apple', 2: 'ball'}
# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}
# using dict()
my_dict = dict({1:'apple', 2:'ball'})
# from sequence having each item as a pair
my_dict = dict([(1,'apple'), (2,'ball')])
Raghavendra R Assistant Professor School of CS & IT 17
Unit 3
Regular Expressions,
Dictionaries
• While indexing is used with other container types to access values,
dictionary uses keys. Key can be used either inside square brackets or with
the get() method.
• The difference while using get() is that it returns None instead of KeyError,
if the key is not found.
my_dict = {'name':'Jack', 'age': 26}
# Output: Jack
print(my_dict['name'])
# Output: 26
print(my_dict.get('age'))
# Trying to access keys which doesn't exist throws error
# my_dict.get('address')
# my_dict['address']
Raghavendra R Assistant Professor School of CS & IT 18
access elements from a dictionary
Unit 3
Regular Expressions,
Dictionaries
• Dictionary are mutable. We can add new items or change the value of
existing items using assignment operator.
• If the key is already present, value gets updated, else a new key: value pair
is added to the dictionary.
my_dict = {'name':'Jack', 'age': 26}
# update value
my_dict['age'] = 27
#Output: {'age': 27, 'name': 'Jack'}
print(my_dict)
# add item
my_dict['address'] = 'Downtown'
# Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'}
print(my_dict)
Raghavendra R Assistant Professor School of CS & IT 19
change or add elements in a dictionary
Unit 3
Regular Expressions,
Dictionaries
• We can remove a particular item in a dictionary by using the method pop().
• This method removes as item with the provided key and returns the value.
• The method, popitem() can be used to remove and return an arbitrary item
(key, value) form the dictionary.
• All the items can be removed at once using the clear() method.
• We can also use the del keyword to remove individual items or the entire
dictionary itself.
Raghavendra R Assistant Professor School of CS & IT 20
delete or remove elements from a dictionary
Unit 3
Regular Expressions,
Dictionaries
# create a dictionary
squares = {1:1, 2:4, 3:9, 4:16, 5:25}
# remove a particular item
# Output: 16
print(squares.pop(4))
# Output: {1: 1, 2: 4, 3: 9, 5: 25}
print(squares)
# remove an arbitrary item
# Output: (1, 1)
print(squares.popitem())
# Output: {2: 4, 3: 9, 5: 25}
print(squares)
# delete a particular item
del squares[5]
# Output: {2: 4, 3: 9}
print(squares)
# remove all items
squares.clear()
# Output: {}
print(squares)
# delete the dictionary itself
del squares
# Throws Error
# print(squares)
Raghavendra R Assistant Professor School of CS & IT 21
Unit 3
Regular Expressions,
Dictionaries
Raghavendra R Assistant Professor School of CS & IT 22
Methods
Unit 3
Regular Expressions,
Dictionaries
Raghavendra R Assistant Professor School of CS & IT 23
Built-in Functions
Unit 3
Regular Expressions,
Dictionaries
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
# Output: 5
print(len(squares))
# Output: [1, 3, 5, 7, 9]
print(sorted(squares))
Raghavendra R Assistant Professor School of CS & IT 24
Unit 3
Regular Expressions,
Dictionaries
• We can test if a key is in a dictionary or not using the keyword in.
• Notice that membership test is for keys only, not for values.
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
# Output: True
print(1 in squares)
# Output: True
print(2 not in squares)
# membership tests for key only not value
# Output: False
print(49 in squares)
Raghavendra R Assistant Professor School of CS & IT 25
Dictionary Membership Test
Unit 3
Regular Expressions,
Dictionaries
Using a for loop we can iterate though each key in a dictionary.
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
for i in squares:
print(squares[i])
Raghavendra R Assistant Professor School of CS & IT 26
Iterating Through a Dictionary
Unit 3
Regular Expressions,
Dictionaries
Raghavendra R Assistant Professor School of CS & IT 27
Unit 3
Regular Expressions,
Dictionaries

More Related Content

Similar to Module 3: Regular Expressions, Dictionaries

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings MethodsMr Examples
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfpaijitk
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objectsHarkamal Singh
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in ScalaAyush Mishra
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9Vince Vo
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 

Similar to Module 3: Regular Expressions, Dictionaries (20)

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
Python Strings Methods
Python Strings MethodsPython Strings Methods
Python Strings Methods
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Python strings
Python stringsPython strings
Python strings
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
Basic python part 1
Basic python part 1Basic python part 1
Basic python part 1
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 

Module 3: Regular Expressions, Dictionaries

  • 1. Module 3 Regular Expressions, Dictionaries 1 22MCSIT201 – Python Programming Master of Science in Computer Science & Information Technology [MSc-CSIT]
  • 2. • 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 Raghavendra R Assistant Professor School of CS & IT 2 RegEx Unit 3 Regular Expressions, Dictionaries
  • 3. Function Description match This method matches the regex pattern in the string with the optional flag. It returns true if a match is found in the string otherwise it returns false. search This method returns the match object if there is a match found in the string. findall It returns a list that contains all the matches of a pattern in the string. split Returns a list in which the string has been split in each match. sub Replace one or many matches in the string. Raghavendra R Assistant Professor School of CS & IT 3 Regex Functions Unit 3 Regular Expressions, Dictionaries The following regex functions are used in the python.
  • 4. Meta-Characters • Metacharacter is a character with the specified meaning. Raghavendra R Assistant Professor School of CS & IT 4 Forming a regular expression Unit 3 Regular Expressions, Dictionaries Metach aracter 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. “Py.h." ^ It represents the pattern present at the beginning of the string. "^Python" $ It represents the pattern present at the end of the string. “program" * 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. “Python{2}" | It represents either this or that character is present. “Python|program" () Capture and group
  • 5. Special Sequences • Special sequences are the sequences containing followed by one of the characters. Raghavendra R Assistant Professor School of CS & IT 5 Forming a regular expression Unit 3 Regular Expressions, Dictionaries 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. B It returns a match if the specified characters are present at the beginning of the string but not at the end. 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.
  • 6. • A set is a group of characters given inside a pair of square brackets. It represents the special meaning. Raghavendra R Assistant Professor School of CS & IT 6 Forming a regular expression Unit 3 Regular Expressions, Dictionaries Set Description [arn] Returns a match if the string contains any of the specified characters in the set. [a-n] Returns a match if the string contains any of the characters between a to n. [^arn] Returns a match if the string contains the characters except a, r, and n. [0123] Returns a match if the string contains any of the specified digits. [0-9] Returns a match if the string contains any digit between 0 and 9. [0-5][0-9] Returns a match if the string contains any digit between 00 and 59. [a-zA-Z] Returns a match if the string contains any alphabet (lower-case or upper-case).
  • 7. • 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. import re string = 'hello 12 hi 89. Howdy 34' pattern = 'd+' result = re.findall(pattern, string) print(result) Output: ['12', '89', '34'] Raghavendra R Assistant Professor School of CS & IT 7 findall() function Unit 3 Regular Expressions, Dictionaries
  • 8. • 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:', '.'] Raghavendra R Assistant Professor School of CS & IT 8 re.split() Unit 3 Regular Expressions, Dictionaries
  • 9. • You can pass maxsplit argument to the re.split() method. It's the maximum number of splits that will occur. import re string = 'Twelve:12 Eighty nine:89 Nine:9.' pattern = 'd+' # maxsplit = 1 # split only at the first occurrence result = re.split(pattern, string, 1) print(result) # Output: ['Twelve:', ' Eighty nine:89 Nine:9.'] Raghavendra R Assistant Professor School of CS & IT 9 Unit 3 Regular Expressions, Dictionaries
  • 10. • re.sub(pattern, replace, string) • The method returns a string where matched occurrences are replaced with the content of replace variable. # 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.sub(pattern, replace, string) print(new_string) # Output: abc12de23f456 Raghavendra R Assistant Professor School of CS & IT 10 re.sub() Unit 3 Regular Expressions, Dictionaries
  • 11. • 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) Raghavendra R Assistant Professor School of CS & IT 11 re.search() Unit 3 Regular Expressions, Dictionaries
  • 12. 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 Raghavendra R Assistant Professor School of CS & IT 12 Unit 3 Regular Expressions, Dictionaries
  • 13. • You can get methods and attributes of a match object using dir() function. • Some of the commonly used methods and attributes of match objects are: match.group() • The group() method returns the part of the string where there is a match. import re Num = 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 Raghavendra R Assistant Professor School of CS & IT 13 Match object Unit 3 Regular Expressions, Dictionaries
  • 14. • When r or R prefix is used before a regular expression, it means raw string. • For example, 'n' is a new line whereas r'n' means two characters: a backslash followed by n. • Backlash is used to escape various characters including all metacharacters. • However, using r prefix makes treat as a normal character. import re string = 'n and r are escape sequences.' result = re.findall(r'[nr]', string) print(result) # Output: ['n', 'r'] Raghavendra R Assistant Professor School of CS & IT 14 Using r prefix before RegEx Unit 3 Regular Expressions, Dictionaries
  • 15. Raghavendra R Assistant Professor School of CS & IT 15 Unit 3 Regular Expressions, Dictionaries
  • 16. • Python dictionary is an unordered collection of items. • While other compound data types have only value as an element, a dictionary has a key: value pair. • Dictionaries are optimized to retrieve values when the key is known. How to create a dictionary? • Creating a dictionary is as simple as placing items inside curly braces {} separated by comma. • An item has a key and the corresponding value expressed as a pair, key: value. • While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique. Raghavendra R Assistant Professor School of CS & IT 16 Dictionary Unit 3 Regular Expressions, Dictionaries
  • 17. # empty dictionary my_dict = {} # dictionary with integer keys my_dict = {1: 'apple', 2: 'ball'} # dictionary with mixed keys my_dict = {'name': 'John', 1: [2, 4, 3]} # using dict() my_dict = dict({1:'apple', 2:'ball'}) # from sequence having each item as a pair my_dict = dict([(1,'apple'), (2,'ball')]) Raghavendra R Assistant Professor School of CS & IT 17 Unit 3 Regular Expressions, Dictionaries
  • 18. • While indexing is used with other container types to access values, dictionary uses keys. Key can be used either inside square brackets or with the get() method. • The difference while using get() is that it returns None instead of KeyError, if the key is not found. my_dict = {'name':'Jack', 'age': 26} # Output: Jack print(my_dict['name']) # Output: 26 print(my_dict.get('age')) # Trying to access keys which doesn't exist throws error # my_dict.get('address') # my_dict['address'] Raghavendra R Assistant Professor School of CS & IT 18 access elements from a dictionary Unit 3 Regular Expressions, Dictionaries
  • 19. • Dictionary are mutable. We can add new items or change the value of existing items using assignment operator. • If the key is already present, value gets updated, else a new key: value pair is added to the dictionary. my_dict = {'name':'Jack', 'age': 26} # update value my_dict['age'] = 27 #Output: {'age': 27, 'name': 'Jack'} print(my_dict) # add item my_dict['address'] = 'Downtown' # Output: {'address': 'Downtown', 'age': 27, 'name': 'Jack'} print(my_dict) Raghavendra R Assistant Professor School of CS & IT 19 change or add elements in a dictionary Unit 3 Regular Expressions, Dictionaries
  • 20. • We can remove a particular item in a dictionary by using the method pop(). • This method removes as item with the provided key and returns the value. • The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary. • All the items can be removed at once using the clear() method. • We can also use the del keyword to remove individual items or the entire dictionary itself. Raghavendra R Assistant Professor School of CS & IT 20 delete or remove elements from a dictionary Unit 3 Regular Expressions, Dictionaries
  • 21. # create a dictionary squares = {1:1, 2:4, 3:9, 4:16, 5:25} # remove a particular item # Output: 16 print(squares.pop(4)) # Output: {1: 1, 2: 4, 3: 9, 5: 25} print(squares) # remove an arbitrary item # Output: (1, 1) print(squares.popitem()) # Output: {2: 4, 3: 9, 5: 25} print(squares) # delete a particular item del squares[5] # Output: {2: 4, 3: 9} print(squares) # remove all items squares.clear() # Output: {} print(squares) # delete the dictionary itself del squares # Throws Error # print(squares) Raghavendra R Assistant Professor School of CS & IT 21 Unit 3 Regular Expressions, Dictionaries
  • 22. Raghavendra R Assistant Professor School of CS & IT 22 Methods Unit 3 Regular Expressions, Dictionaries
  • 23. Raghavendra R Assistant Professor School of CS & IT 23 Built-in Functions Unit 3 Regular Expressions, Dictionaries
  • 24. squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} # Output: 5 print(len(squares)) # Output: [1, 3, 5, 7, 9] print(sorted(squares)) Raghavendra R Assistant Professor School of CS & IT 24 Unit 3 Regular Expressions, Dictionaries
  • 25. • We can test if a key is in a dictionary or not using the keyword in. • Notice that membership test is for keys only, not for values. squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} # Output: True print(1 in squares) # Output: True print(2 not in squares) # membership tests for key only not value # Output: False print(49 in squares) Raghavendra R Assistant Professor School of CS & IT 25 Dictionary Membership Test Unit 3 Regular Expressions, Dictionaries
  • 26. Using a for loop we can iterate though each key in a dictionary. squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81} for i in squares: print(squares[i]) Raghavendra R Assistant Professor School of CS & IT 26 Iterating Through a Dictionary Unit 3 Regular Expressions, Dictionaries
  • 27. Raghavendra R Assistant Professor School of CS & IT 27 Unit 3 Regular Expressions, Dictionaries