SlideShare a Scribd company logo
1 of 99
UNIT-IV
G. Venkata Kishore
Asst. Prof
Python- OOPs concept:
Class and object, Attributes,
Inheritance, Overloading Overriding,
Data hiding.
Regular Expressions: Match function,
Search function, Matching VS
Searching, Modifiers Patterns.
Object-oriented programming is one of
the most effective approaches to writing
soft ware.
In object-oriented programming you
write classes that represent real-world
things and situations, and you create
objects based on these classes.
When you write a class, you define the
general behavior that a whole category
of objects can have.
PROCEDURE ORIENTED
⦿The languages like C, Pascal, etc are called
Procedure Oriented Programming languages
since in these languages, a programmer uses
procedures or functions to perform a task.
⦿While developing a software, the main task is
divided into several sub tasks and each sub
task is represented as a procedure or
function.
⦿The main task is thus composed of several
procedures and functions. This approach is
called Procedure oriented approach.
Main()
function
Function 1
Function 2
Function 3
Sub task1
Sub task 2
Sub task3
PROCEDURE ORIENTED APPROACH
PROBLEMS IN PROCEDURE ORIENTED
APPROACH
⦿ No reusability of an existing code. A new task every time requires
developing the code from the scratch.
⦿ Functions may depend on other functions, this makes debugging
difficult.
⦿ Updating the software will also be difficult.
⦿ The program code is harder to write when Procedural
Programming is employed as the control on the code decreases
after certain point.
OBJECT ORIENTED
⦿ OOPs is a program paradigm based on the concept of
objects.
⦿ Languages like C++, Java and Python use classes and
objects in their program are called Object Oriented
programming languages.
⦿ A class is a module which itself contains data and
methods(functions) to achieve the task.
⦿ The main task is divided into several sub tasks, and
these are represented as classes.
⦿ Each class can perform several inter-related tasks
for which several methods are written in a class.
This approach is called Object Oriented approach.
Class with
main()
method
Class1 with data
and methods
Class1 with data
and methods
Class1 with data
and methods
Sub task1
Sub task 2
Sub task 3
main task
OBJECT ORIENTED APPROACH
SPECIALTY OF PYTHON LANGUAGE
⦿Though, Python is an object oriented
programming language like Java, it does not
force the programmers to write programs in
complete object oriented way.
⦿Unlike Java, Python has the blend of both the
object oriented and procedure oriented
features.
⦿Hence the programmer can write the program
as per their requirement.
FEATURES OF OOPS:
There are five important features related to
object oriented programming system. They are:
⦿Classes and Objects.
⦿Encapsulation.
⦿Abstraction.
⦿Inheritance.
⦿Polymorphism.
CLASSES AND OBJECTS
⦿ In python every thing is an object.
⦿ To create objects we require some Model or Plan or
Blue print, which is nothing but class.
⦿ We can write a class to represent properties
(attributes) and actions (behavior) of object.
ENCAPSULATION
⦿ Encapsulation is a mechanism where the data(variables) and
the code(methods) that act on the data will bind together. If
we take a class, we write the variables and methods inside the
class, Class is binding them together. Class is an example for
Encapsulation.
⦿ Eg: We can write a Student class with ‘id’ and ‘name’ as
attributes along with the display() method that displays this
data.
⦿ Eg: Capsule
ABSTRACTION
Hides unnecessary data from user and expose only essential
information is Abstraction.(hides unnecessary information)
Eg: Car, Toon App, AC.
INHERITANCE
⦿ Creating new classes from existing classes, so that
the new classes will acquire all the features of the
existing classes is called Inheritance.
⦿ E.g.: Parent Child.
POLYMORPHISM
⦿ Poly means ‘many’ and morphos means ‘forms’,
Polymorphism represents the ability to assume several
different forms.
⦿ If an object is exhibiting different behavior in different
contexts, it is called polymorphic nature.
⦿ Eg: Human
CLASSES AND OBJECTS
⦿ Class is a collection of objects. Class is a model or
blue print to create an object.
⦿ We write a class with attributes and actions of
objects.
⦿ Object is an instance of a class.
⦿ Properties can be represented by variables
⦿ Actions can be represented by Methods.
⦿ Hence class contains both variables and methods.
⦿ Physical existence of a class is nothing but object.
⦿ We can create any number of objects for a class.
CAR
COLOUR
PRICE
BRAND
OBJECT
ATTRIBUTE
S
CLASS OBJECTS
CAR SUV
KIA
CLASS OBJECTS
ANIMAL LION
LEOPARD
TIGER
⮚ Class is a blueprint which is used to create an object.
⮚ OBJECT IS AN INSTANCE OF A CLASS.
E.g.:
⦿ Every object has some behavior. The behavior of
an object is represented by attributes and actions.
⦿ Eg: A Person named ‘Joe’
⦿ Joe is an object because he exists physically. He
has attributes like name, age, gender etc.
⦿ These attributes can be represented by variables in
our programming.
⦿ Eg: ‘name’ is a string type variable,
⦿ ‘age’ is an integer type variable.
Name
Age
Gender
Talking
Walking
Eating
talk()
walk()
eat()
Joe
20
M
attributes
actions
attributes
methods
Class: Person Object: Joe
Person
Id
Name
Age
Sex
City
Ph no
Eat()
Walk()
Study()
Play()
Person 2/Emp 2:
Jessie
|
|
|
Person 3/Emp 3:
John
|
|
|
Person 1/Emp 1:
1
Joe
20
M
Hyd
123423
class
Attributes()
Methods()
CREATE A CLASS
⦿ A CLASS IS CREATED with the keyword class and then mention the
class name.
⦿ After the class name ‘object’ is written inside the class name.
⦿ class ClassName:
<statement-1>
.
.
<statement-N>
Eg: class class1():
pass
object1= class1()
object1.name = ‘Joe’
CREATE A CLASS
Eg: 1
Eg: 2
CREATE A CLASS WITH AN OBJECT
CREATE A CLASS WITH MULTIPLE OBJECTS
METHOD EXECUTION VS CONSTRUCTOR EXECUTION
THE SELF
VARIABLE:
⦿ ‘Self is a default variable that contains the memory
address of the instance of the current class. So, we
can use ‘Self’ to refer all the instance variables and
instance methods.
⦿ Eg: s1 =Student()
⦿ Here, s1 contains the memory address of the
instance. This memory address is internally and by
default passed to ‘Self’ variable. Since ‘Self’ knows
the memory address of the instance, it can refer to
all the members of the instance.
⦿ SELF CAN BE USED IN TWO WAYS
⦿ 1) def __init__(self):
⦿ 2) def talk(self):
CONSTRUCTOR
⦿Constructor is a special method used to initialize
the instance variables of a class.
⦿In constructor we create the instance variables
and initialize them with some starting values.
⦿The first parameter of the constructor will be
‘self’ variable that contains memory address of the
instance.
⦿Eg: def __init__(self):
⦿ self.name=‘Joe’
⦿ self.age = 20
⦿S1 = student()
EXAMPLES:
CREATE A CLASS WITH CONSTRUCTOR
CLASS WITH A METHOD (FUNCTION)
EG: 1
CLASS WITH METHODS (FUNCTIONS)
EG: 2
CLASS WITH METHODS (FUNCTIONS)
Eg: 3
A python program to define student class and create an object to it. Also
calling the method and display the students details.
TYPES OF VARIABLES:
⦿The variables which are written inside a class
are of 2 types:
⦿Instance Variables
⦿Class variables or Static variables
INSTANCE VARIABLE
If we change anything in instance variables, it will
not effect to other objects.
We can declare Instance variables:
⦿1. Inside Constructor by using self variable
⦿2. Inside Instance Method by using self variable
⦿3. Outside of the class by using object reference
variable
1. INSIDE CONSTRUCTOR BY USING SELF VARIABLE
⦿We can declare instance variables inside a
constructor by using self keyword.
⦿Once we create object, automatically these
variables will be added to the object.
2. INSIDE INSTANCE METHOD BY USING SELF
VARIABLE:
⦿ We can also declare instance variables inside
instance method by using self variable.
⦿ If any instance variable declared inside instance
method, that instance variable will be added once
we call that method.
3. OUTSIDE OF THE CLASS BY USING OBJECT
REFERENCE VARIABLE:
⦿We can also add instance variables outside of a
class to a particular object.
EXAMPLE OF INSTANCE VARIABLE
Instance variables
STATIC VARIABLES:
⦿Class variables are the variables whose single
copy is available to all the instances of the
class. If we modify the copy of class variable in
an instance, it will modify all the copies in the
other instances.
⦿In general we can declare within the class
directly but from out side of any method.
EXAMPLE OF STATIC/CLASS VARIABLES
Static/ Class variable
To access the static
method we have to
use object name
dot(.) static
variable name
Another way to access the static variable
HOW TO MODIFY STATIC VARIABLE
⦿We can also modify or the change the static/
class variable.
⦿To modify the static variable we have to use
the class name dot (.) static variable name.
HOW TO DELETE STATIC VARIABLE
We can delete static variables from anywhere by using the
following syntax
del classname.variablename
TYPES OF METHODS
⦿The purpose of the method is to process the
variables provided in the class or in the method.
⦿Methods can be classified into the following 3
types:
⦿Instance Methods
⦿Class Methods
⦿Static Methods
INSTANCE METHODS
CLASS METHODS
⦿Inside method implementation if we are using
only class variables(static variables),then such
type of methods we should declare as class
method.
⦿We can declare class method explicitly by using
@classmethod.
⦿By default, the first parameter for class is cls,
which refers to the class itself.
Eg:
STATIC METHODS
⦿Static method are used when processing is
related to the class, but it does not include any
class or instances to perform any task.
⦿For Eg: counting the number of instances in the
class or changing the attributes in class.
⦿Static methods are written with @staticmethod
Eg:
ATTRIBUTES
INHERITANCE
⦿ Inheritance is an important aspect of the object-oriented
paradigm.
⦿ Inheritance provides code reusability to the program
because we can use an existing class to create a new
class instead of creating it from scratch.
⦿ In inheritance, the child class acquires the properties
and can access all the data members and functions
defined in the parent class.
⦿ A child class can also provide its specific implementation
to the functions of the parent class.
⦿ In python, a derived class can inherit base class by just
mentioning the base in the bracket after the derived
class name.
⦿ Consider the following syntax to inherit a base class into
the derived class.
⦿ The relationship from person to a student is termed
‘Specialization’. Conversely, every student is a person, this is
called Generalization.
⦿ In this representation, we use an arrow towards the base class
as a UML (Unified Modeling Language) convention.
⦿ Here,
⦿ Parent can be called any of the following:
⦿ Super Class
⦿ Parent Class
⦿ Base Class
⦿ Likewise, Student here is:
⦿ Sub Class
⦿ Child Class
⦿ Derived Class
Parent
Student
1. SINGLE INHERITANCE:
⦿When a child class inherits from only one
parent class, it is called as single inheritance.
⦿
Base Class
Derived Class
Deriving one or sub classes from a single base class is
called single inheritance.
SINGLE INHERITANCE:
Without inheriting After inheriting
MULTI-LEVEL INHERITANCE
⦿ The multi-level inheritance includes the involvement
of at least two or more than two classes.
⦿ Multi-Level Inheritance is achieved when a derived
class inherits another derived class.
⦿ One class inherits the features from a parent class
and the newly created sub-class becomes the base
class for another new class.
Class -1
Class- 2
Class- N
MULTIPLE INHERITANCE
⦿ Multiple inheritances is where a subclass can inherit
features from more than one parent class.
⦿ In multiple inheritance the newly derived class can
have more than one superclass. And this newly
derived class can inherit the features from these
super classes it has inherited from, so there are no
restrictions.
BASE CLASS1 BASE CLASS 2 BASE CLASS 3
DERIVED
CLASS
POLYMORPHISM
⦿ Polymorphism is a word that came from two Greek
words, poly means many and morphos means forms.
If something exhibits various forms, it is called as
polymorphism.
⦿ Eg: Human, Wheat
⦿ In programming, a variable or an object or a method
will also exhibit the same nature.
⦿ A variable may store different types of data, an
object may exhibit different behaviors in different
contexts, or a method may perform various tasks.
⦿ The main examples for polymorphism in python are:
⦿ METHOD OVERLOADING
⦿ METHOD OVERRIDING
Eg:
⦿ An operator is a symbol that performs some action.
For eg: ‘+’ is an operator performs addition
operation when used on numbers, when an
operator can perform different actions, it is said
to exhibit polymorphism.
METHOD OVERLOADING
⦿If a method is written such that it can perform
more than one task, it is called method
overloading.
⦿We see method overloading in the languages like
Java.
⦿Method overloading is not available in Python,
writing more, We can achieve method overloading
by writing same method with several parameters.
⦿The method performs the operation depending on
the number of Arguments passed in the method
call.
⦿For eg: We can write sum() method with default
value ‘None’ for the arguments.
def sum(self, a=None, b=None, c=None):
if a!=None and b! =None and c!=None:
print(‘sum of three=‘, a+b+c)
elif a!=None and b!=None:
print(‘sum of two=‘, a+b)
METHOD OVERRIDING
⦿When there is a method in the super class,
writing the same method in the sub class so that
it replaces the super class method is called
‘method overriding’
⦿Base class will override the method in the super
class.
⦿Base class's method is called as the overridden
method and the derived class method is
called as overriding method.
DATA HIDING
⦿It is a method used in (OOP) to hide with the
information/ data.
⦿The object details such as data members, are
hidden within a class.
⦿We can restrict the data members to class to
maintain integrity.
⦿To hide any variable we have to use double
underscore __ prefix the variable name, then
such variables are not directly visible to the
outsiders.
REGULAR
EXPRESSION
REGEX
REGULAR EXPRESSION:
⦿ A regular expression is a string that contains special
symbols and characters to find and extract the
information needed by us from the given data.
⦿ A regular expression helps us in search information,
match, find, and split information as per our
requirements.
⦿ A regular expression is also called as ‘Regex’
⦿ Python provides re module that stands for regular
expression. This module contains methods like
compile(), search(), match(), findall(), split()
⦿ When we write a regular expression we have to import
re module as
⦿ Import re
The regular expressions are used to perform the
following operations:
⦿Matching strings
⦿Searching for all strings
⦿Finding all strings
⦿Splitting a string into pieces
⦿Replacing strings.
The following methods belong to the ‘re’ module
that are used in the regular expression
⦿ The match() method searches the beginning of the
strings and if the matching string is found, it returns
the resultant string, otherwise it returns None. To
access the string from the returned value use group()
method.
⦿ The search() method searches the string from
beginning till the end and returns the first occurrence
of the matching string, otherwise it returns None.
We can use group() method to retrieve the string
from returned value.
⦿ To findall() method searches the string from
beginning till the end and returns all occurrences of
the matching string in the form of a list object. If the
matching strings are not found it returns empty list.
We can retrieve the resultant string pieces from the
list using a for loop.
⦿ The split() method splits the string according to
the regular expression and the resultant pieces are
returned as a list. If there are no string pieces,
then it returns an empty list. We can retrieve the
resultant string pieces from the list using a for
loop.
⦿ The sub() method substitutes(or replaces) new
strings in the place of existing strings.
Sequence characters in regular expressions
Pre-defined
Character
Description
d Represents any digit (0-9)
D Represents any non digit
s Represents white spaces(t, n)
S Represents non whitespaces
w Represents any aplhanumeric(A-Z, a-z,0-9)
W Represents any non aplhanumeric
b Represents a space around words
A Matches only at start of the string
z Matches only at end of the string
⦿ import re
#importing the regular expression module
⦿ prog =re.compile(r’mww’)
#prog represents an object that contains regular expression
⦿ str = ‘car mat bat rat’
#this is the string on which regular expression will act.
⦿ result = prog.search(str)
#searching the regular expression in the string
⦿ print(result.group)
#the result will store in ‘result’ object and we can display it
by calling the group() method.
A python program to create a regular expression to search for strings
starting with ‘c’ and having 3 characters using the SEARCH METHOD()
A python program to create a regular expression using the
MATCH METHOD() to search for strings starting with ‘m’ and having 3
characters.
A python program to create a regular expression using the
match method() to search for strings starting with ‘m’ and
having 4 characters.
A python program to create a regular expression to search for strings
starting with ‘m’ and having 3 characters using the findall() method
A PYTHON PROGRAM TO CREATE A REGULAR EXPRESSION TO
REPLACE A STRING WITH A NEW STRING
FULLMATCH()
⦿ We can use fullmatch() function to match a pattern to all of
target string. i.e. complete string should be matched
according to given pattern.
⦿ If complete string matched then this function returns Match
object otherwise it returns None.
FULL STRING NOT MATCHED
MATCHING VERSUS SEARCHING
⦿ Python offers two different primitive operations based on
regular expressions: match checks for a match only at the
beginning of the string, while search checks for a match
anywhere in the string.
⦿ re.search() function will search the regular expression
pattern and return the first occurrence. Unlike Python
re.match(), it will check all lines of the input string. The
Python re.search() function returns a match object when
the pattern is found and “null” if the pattern is not found
Character classes
Character Description
[abc] Either a or b or c
[^abc] Except a and b and c
[a-z] Any lower case alphabet symbol
[A-Z] Any uppercase alphabet symbol
[a-zA-Z] Any alphabet symbol
[0-9] Any digit from 0 to 9
[a-zA-Z0-9] Any alphanumeric character
[^a-zA-Z0-9] Except alphanumeric character(special
characters)
Sequence characters in regular expressions
Pre-defined
Character
Description
d Represents any digit (0-9)
D Represents any non digit
s Represents white spaces(t, n)
S Represents non whitespaces
w Represents any aplhanumeric(A-Z, a-z,0-9)
W Represents any non aplhanumeric
b Represents a space around words
A Matches only at start of the string
z Matches only at end of the string
A python program to create a regular expression to retrieve
all words starting with a given string
It returned ‘ay’ also, ay is not a word
it is a part of away. To avoid such
things use b before and after the
words in regular expression
b represents a space around words
w indicates any one
alphanumeric character.
Suppose if we write it as
[w]*, Here * represents 0
or more repetitions.
Hence [w]* represents 0
or more alphanumeric
characters.
a – any string starting with a
ba- any word starts with a
bab- matches only word a
Ba- any string that contains but does not begin with a
A python program to create a regular expression to retrieve all words
starting with a numeric digit
We want to retrieve all the words starting with a numeric
digit i.e 0,1 – 9. The numeric digit is represented by ‘d’
and hence, the expression is r’d[w]*
A python program to create a regular expression to retrieve
all words having 5 characters length.
A python program to create a regular expression to retrieve
all words having 3 characters length.
A python program to create a regular expression to retrieve
all words between 3 or 4 or 5 characters length.
QUANTIFIERS
Character Description
? Occurs 0 or 1 time
+ Occurs 1 or more than one time
* Occurs 0 or more times
{n} Occurs n no. of times
{n, } Occurs n or more than n times
{x,y} Occurs at least x times but less than y times
A python program to create a regular expression to retrieve
the phone number of a person.
d represents any digit(0-9)
+ indicates 1 or more times
A python program to create a regular expression to retrieve
the name of a person.
D represents any digit(0-9)
+ indicates 1 or more times

More Related Content

Similar to Regex,functions, inheritance,class, attribute,overloding

Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in PythonAditiPawaskar5
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Conceptrohitsharma24121
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1Geophery sanga
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)Jyoti shukla
 
PHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxPHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxAtikur Rahman
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesBalamuruganV28
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java MayaTofik
 
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
ITS-16163: Module 7 Introduction to Object-Oriented Programming BasicsITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basicsoudesign
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction paramisoft
 

Similar to Regex,functions, inheritance,class, attribute,overloding (20)

oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Creating class, self variables in Python
Creating class, self variables in PythonCreating class, self variables in Python
Creating class, self variables in Python
 
Problem solving with python programming OOP's Concept
Problem solving with python programming OOP's ConceptProblem solving with python programming OOP's Concept
Problem solving with python programming OOP's Concept
 
Oop java
Oop javaOop java
Oop java
 
javaopps concepts
javaopps conceptsjavaopps concepts
javaopps concepts
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
 
PHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxPHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptx
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
8. objects & classes
8. objects & classes8. objects & classes
8. objects & classes
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
ITS-16163: Module 7 Introduction to Object-Oriented Programming BasicsITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
ITS-16163: Module 7 Introduction to Object-Oriented Programming Basics
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Regex,functions, inheritance,class, attribute,overloding

  • 2. Python- OOPs concept: Class and object, Attributes, Inheritance, Overloading Overriding, Data hiding. Regular Expressions: Match function, Search function, Matching VS Searching, Modifiers Patterns.
  • 3. Object-oriented programming is one of the most effective approaches to writing soft ware. In object-oriented programming you write classes that represent real-world things and situations, and you create objects based on these classes. When you write a class, you define the general behavior that a whole category of objects can have.
  • 4. PROCEDURE ORIENTED ⦿The languages like C, Pascal, etc are called Procedure Oriented Programming languages since in these languages, a programmer uses procedures or functions to perform a task. ⦿While developing a software, the main task is divided into several sub tasks and each sub task is represented as a procedure or function. ⦿The main task is thus composed of several procedures and functions. This approach is called Procedure oriented approach.
  • 5. Main() function Function 1 Function 2 Function 3 Sub task1 Sub task 2 Sub task3 PROCEDURE ORIENTED APPROACH
  • 6. PROBLEMS IN PROCEDURE ORIENTED APPROACH ⦿ No reusability of an existing code. A new task every time requires developing the code from the scratch. ⦿ Functions may depend on other functions, this makes debugging difficult. ⦿ Updating the software will also be difficult. ⦿ The program code is harder to write when Procedural Programming is employed as the control on the code decreases after certain point.
  • 7. OBJECT ORIENTED ⦿ OOPs is a program paradigm based on the concept of objects. ⦿ Languages like C++, Java and Python use classes and objects in their program are called Object Oriented programming languages. ⦿ A class is a module which itself contains data and methods(functions) to achieve the task. ⦿ The main task is divided into several sub tasks, and these are represented as classes. ⦿ Each class can perform several inter-related tasks for which several methods are written in a class. This approach is called Object Oriented approach.
  • 8. Class with main() method Class1 with data and methods Class1 with data and methods Class1 with data and methods Sub task1 Sub task 2 Sub task 3 main task OBJECT ORIENTED APPROACH
  • 9. SPECIALTY OF PYTHON LANGUAGE ⦿Though, Python is an object oriented programming language like Java, it does not force the programmers to write programs in complete object oriented way. ⦿Unlike Java, Python has the blend of both the object oriented and procedure oriented features. ⦿Hence the programmer can write the program as per their requirement.
  • 10. FEATURES OF OOPS: There are five important features related to object oriented programming system. They are: ⦿Classes and Objects. ⦿Encapsulation. ⦿Abstraction. ⦿Inheritance. ⦿Polymorphism.
  • 11. CLASSES AND OBJECTS ⦿ In python every thing is an object. ⦿ To create objects we require some Model or Plan or Blue print, which is nothing but class. ⦿ We can write a class to represent properties (attributes) and actions (behavior) of object.
  • 12. ENCAPSULATION ⦿ Encapsulation is a mechanism where the data(variables) and the code(methods) that act on the data will bind together. If we take a class, we write the variables and methods inside the class, Class is binding them together. Class is an example for Encapsulation. ⦿ Eg: We can write a Student class with ‘id’ and ‘name’ as attributes along with the display() method that displays this data. ⦿ Eg: Capsule ABSTRACTION Hides unnecessary data from user and expose only essential information is Abstraction.(hides unnecessary information) Eg: Car, Toon App, AC.
  • 13. INHERITANCE ⦿ Creating new classes from existing classes, so that the new classes will acquire all the features of the existing classes is called Inheritance. ⦿ E.g.: Parent Child. POLYMORPHISM ⦿ Poly means ‘many’ and morphos means ‘forms’, Polymorphism represents the ability to assume several different forms. ⦿ If an object is exhibiting different behavior in different contexts, it is called polymorphic nature. ⦿ Eg: Human
  • 14. CLASSES AND OBJECTS ⦿ Class is a collection of objects. Class is a model or blue print to create an object. ⦿ We write a class with attributes and actions of objects. ⦿ Object is an instance of a class. ⦿ Properties can be represented by variables ⦿ Actions can be represented by Methods. ⦿ Hence class contains both variables and methods. ⦿ Physical existence of a class is nothing but object. ⦿ We can create any number of objects for a class.
  • 16. CLASS OBJECTS CAR SUV KIA CLASS OBJECTS ANIMAL LION LEOPARD TIGER ⮚ Class is a blueprint which is used to create an object. ⮚ OBJECT IS AN INSTANCE OF A CLASS.
  • 17. E.g.: ⦿ Every object has some behavior. The behavior of an object is represented by attributes and actions. ⦿ Eg: A Person named ‘Joe’ ⦿ Joe is an object because he exists physically. He has attributes like name, age, gender etc. ⦿ These attributes can be represented by variables in our programming. ⦿ Eg: ‘name’ is a string type variable, ⦿ ‘age’ is an integer type variable.
  • 19. Person Id Name Age Sex City Ph no Eat() Walk() Study() Play() Person 2/Emp 2: Jessie | | | Person 3/Emp 3: John | | | Person 1/Emp 1: 1 Joe 20 M Hyd 123423 class Attributes() Methods()
  • 20. CREATE A CLASS ⦿ A CLASS IS CREATED with the keyword class and then mention the class name. ⦿ After the class name ‘object’ is written inside the class name. ⦿ class ClassName: <statement-1> . . <statement-N> Eg: class class1(): pass object1= class1() object1.name = ‘Joe’
  • 22. CREATE A CLASS WITH AN OBJECT
  • 23. CREATE A CLASS WITH MULTIPLE OBJECTS
  • 24.
  • 25. METHOD EXECUTION VS CONSTRUCTOR EXECUTION
  • 26. THE SELF VARIABLE: ⦿ ‘Self is a default variable that contains the memory address of the instance of the current class. So, we can use ‘Self’ to refer all the instance variables and instance methods. ⦿ Eg: s1 =Student() ⦿ Here, s1 contains the memory address of the instance. This memory address is internally and by default passed to ‘Self’ variable. Since ‘Self’ knows the memory address of the instance, it can refer to all the members of the instance. ⦿ SELF CAN BE USED IN TWO WAYS ⦿ 1) def __init__(self): ⦿ 2) def talk(self):
  • 27. CONSTRUCTOR ⦿Constructor is a special method used to initialize the instance variables of a class. ⦿In constructor we create the instance variables and initialize them with some starting values. ⦿The first parameter of the constructor will be ‘self’ variable that contains memory address of the instance. ⦿Eg: def __init__(self): ⦿ self.name=‘Joe’ ⦿ self.age = 20 ⦿S1 = student()
  • 29. CREATE A CLASS WITH CONSTRUCTOR
  • 30. CLASS WITH A METHOD (FUNCTION) EG: 1
  • 31. CLASS WITH METHODS (FUNCTIONS) EG: 2
  • 32. CLASS WITH METHODS (FUNCTIONS) Eg: 3
  • 33. A python program to define student class and create an object to it. Also calling the method and display the students details.
  • 34. TYPES OF VARIABLES: ⦿The variables which are written inside a class are of 2 types: ⦿Instance Variables ⦿Class variables or Static variables
  • 35. INSTANCE VARIABLE If we change anything in instance variables, it will not effect to other objects. We can declare Instance variables: ⦿1. Inside Constructor by using self variable ⦿2. Inside Instance Method by using self variable ⦿3. Outside of the class by using object reference variable
  • 36. 1. INSIDE CONSTRUCTOR BY USING SELF VARIABLE ⦿We can declare instance variables inside a constructor by using self keyword. ⦿Once we create object, automatically these variables will be added to the object.
  • 37. 2. INSIDE INSTANCE METHOD BY USING SELF VARIABLE: ⦿ We can also declare instance variables inside instance method by using self variable. ⦿ If any instance variable declared inside instance method, that instance variable will be added once we call that method.
  • 38. 3. OUTSIDE OF THE CLASS BY USING OBJECT REFERENCE VARIABLE: ⦿We can also add instance variables outside of a class to a particular object.
  • 39. EXAMPLE OF INSTANCE VARIABLE Instance variables
  • 40. STATIC VARIABLES: ⦿Class variables are the variables whose single copy is available to all the instances of the class. If we modify the copy of class variable in an instance, it will modify all the copies in the other instances. ⦿In general we can declare within the class directly but from out side of any method.
  • 41. EXAMPLE OF STATIC/CLASS VARIABLES Static/ Class variable To access the static method we have to use object name dot(.) static variable name
  • 42. Another way to access the static variable
  • 43. HOW TO MODIFY STATIC VARIABLE ⦿We can also modify or the change the static/ class variable. ⦿To modify the static variable we have to use the class name dot (.) static variable name.
  • 44. HOW TO DELETE STATIC VARIABLE We can delete static variables from anywhere by using the following syntax del classname.variablename
  • 45. TYPES OF METHODS ⦿The purpose of the method is to process the variables provided in the class or in the method. ⦿Methods can be classified into the following 3 types: ⦿Instance Methods ⦿Class Methods ⦿Static Methods
  • 47. CLASS METHODS ⦿Inside method implementation if we are using only class variables(static variables),then such type of methods we should declare as class method. ⦿We can declare class method explicitly by using @classmethod. ⦿By default, the first parameter for class is cls, which refers to the class itself.
  • 48. Eg:
  • 49. STATIC METHODS ⦿Static method are used when processing is related to the class, but it does not include any class or instances to perform any task. ⦿For Eg: counting the number of instances in the class or changing the attributes in class. ⦿Static methods are written with @staticmethod
  • 50. Eg:
  • 52.
  • 53. INHERITANCE ⦿ Inheritance is an important aspect of the object-oriented paradigm. ⦿ Inheritance provides code reusability to the program because we can use an existing class to create a new class instead of creating it from scratch. ⦿ In inheritance, the child class acquires the properties and can access all the data members and functions defined in the parent class. ⦿ A child class can also provide its specific implementation to the functions of the parent class. ⦿ In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. ⦿ Consider the following syntax to inherit a base class into the derived class.
  • 54. ⦿ The relationship from person to a student is termed ‘Specialization’. Conversely, every student is a person, this is called Generalization. ⦿ In this representation, we use an arrow towards the base class as a UML (Unified Modeling Language) convention. ⦿ Here, ⦿ Parent can be called any of the following: ⦿ Super Class ⦿ Parent Class ⦿ Base Class ⦿ Likewise, Student here is: ⦿ Sub Class ⦿ Child Class ⦿ Derived Class Parent Student
  • 55.
  • 56. 1. SINGLE INHERITANCE: ⦿When a child class inherits from only one parent class, it is called as single inheritance. ⦿ Base Class Derived Class Deriving one or sub classes from a single base class is called single inheritance.
  • 58. MULTI-LEVEL INHERITANCE ⦿ The multi-level inheritance includes the involvement of at least two or more than two classes. ⦿ Multi-Level Inheritance is achieved when a derived class inherits another derived class. ⦿ One class inherits the features from a parent class and the newly created sub-class becomes the base class for another new class. Class -1 Class- 2 Class- N
  • 59.
  • 60. MULTIPLE INHERITANCE ⦿ Multiple inheritances is where a subclass can inherit features from more than one parent class. ⦿ In multiple inheritance the newly derived class can have more than one superclass. And this newly derived class can inherit the features from these super classes it has inherited from, so there are no restrictions. BASE CLASS1 BASE CLASS 2 BASE CLASS 3 DERIVED CLASS
  • 61.
  • 62. POLYMORPHISM ⦿ Polymorphism is a word that came from two Greek words, poly means many and morphos means forms. If something exhibits various forms, it is called as polymorphism. ⦿ Eg: Human, Wheat ⦿ In programming, a variable or an object or a method will also exhibit the same nature. ⦿ A variable may store different types of data, an object may exhibit different behaviors in different contexts, or a method may perform various tasks. ⦿ The main examples for polymorphism in python are: ⦿ METHOD OVERLOADING ⦿ METHOD OVERRIDING
  • 63. Eg: ⦿ An operator is a symbol that performs some action. For eg: ‘+’ is an operator performs addition operation when used on numbers, when an operator can perform different actions, it is said to exhibit polymorphism.
  • 64. METHOD OVERLOADING ⦿If a method is written such that it can perform more than one task, it is called method overloading. ⦿We see method overloading in the languages like Java. ⦿Method overloading is not available in Python, writing more, We can achieve method overloading by writing same method with several parameters. ⦿The method performs the operation depending on the number of Arguments passed in the method call. ⦿For eg: We can write sum() method with default value ‘None’ for the arguments.
  • 65. def sum(self, a=None, b=None, c=None): if a!=None and b! =None and c!=None: print(‘sum of three=‘, a+b+c) elif a!=None and b!=None: print(‘sum of two=‘, a+b)
  • 66.
  • 67.
  • 68.
  • 69.
  • 70. METHOD OVERRIDING ⦿When there is a method in the super class, writing the same method in the sub class so that it replaces the super class method is called ‘method overriding’ ⦿Base class will override the method in the super class. ⦿Base class's method is called as the overridden method and the derived class method is called as overriding method.
  • 71.
  • 72. DATA HIDING ⦿It is a method used in (OOP) to hide with the information/ data. ⦿The object details such as data members, are hidden within a class. ⦿We can restrict the data members to class to maintain integrity. ⦿To hide any variable we have to use double underscore __ prefix the variable name, then such variables are not directly visible to the outsiders.
  • 73.
  • 74.
  • 76. REGULAR EXPRESSION: ⦿ A regular expression is a string that contains special symbols and characters to find and extract the information needed by us from the given data. ⦿ A regular expression helps us in search information, match, find, and split information as per our requirements. ⦿ A regular expression is also called as ‘Regex’ ⦿ Python provides re module that stands for regular expression. This module contains methods like compile(), search(), match(), findall(), split() ⦿ When we write a regular expression we have to import re module as ⦿ Import re
  • 77.
  • 78. The regular expressions are used to perform the following operations: ⦿Matching strings ⦿Searching for all strings ⦿Finding all strings ⦿Splitting a string into pieces ⦿Replacing strings.
  • 79. The following methods belong to the ‘re’ module that are used in the regular expression ⦿ The match() method searches the beginning of the strings and if the matching string is found, it returns the resultant string, otherwise it returns None. To access the string from the returned value use group() method. ⦿ The search() method searches the string from beginning till the end and returns the first occurrence of the matching string, otherwise it returns None. We can use group() method to retrieve the string from returned value. ⦿ To findall() method searches the string from beginning till the end and returns all occurrences of the matching string in the form of a list object. If the matching strings are not found it returns empty list. We can retrieve the resultant string pieces from the list using a for loop.
  • 80. ⦿ The split() method splits the string according to the regular expression and the resultant pieces are returned as a list. If there are no string pieces, then it returns an empty list. We can retrieve the resultant string pieces from the list using a for loop. ⦿ The sub() method substitutes(or replaces) new strings in the place of existing strings.
  • 81.
  • 82. Sequence characters in regular expressions Pre-defined Character Description d Represents any digit (0-9) D Represents any non digit s Represents white spaces(t, n) S Represents non whitespaces w Represents any aplhanumeric(A-Z, a-z,0-9) W Represents any non aplhanumeric b Represents a space around words A Matches only at start of the string z Matches only at end of the string
  • 83. ⦿ import re #importing the regular expression module ⦿ prog =re.compile(r’mww’) #prog represents an object that contains regular expression ⦿ str = ‘car mat bat rat’ #this is the string on which regular expression will act. ⦿ result = prog.search(str) #searching the regular expression in the string ⦿ print(result.group) #the result will store in ‘result’ object and we can display it by calling the group() method.
  • 84. A python program to create a regular expression to search for strings starting with ‘c’ and having 3 characters using the SEARCH METHOD()
  • 85. A python program to create a regular expression using the MATCH METHOD() to search for strings starting with ‘m’ and having 3 characters. A python program to create a regular expression using the match method() to search for strings starting with ‘m’ and having 4 characters.
  • 86. A python program to create a regular expression to search for strings starting with ‘m’ and having 3 characters using the findall() method
  • 87. A PYTHON PROGRAM TO CREATE A REGULAR EXPRESSION TO REPLACE A STRING WITH A NEW STRING
  • 88. FULLMATCH() ⦿ We can use fullmatch() function to match a pattern to all of target string. i.e. complete string should be matched according to given pattern. ⦿ If complete string matched then this function returns Match object otherwise it returns None.
  • 89. FULL STRING NOT MATCHED
  • 90. MATCHING VERSUS SEARCHING ⦿ Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string. ⦿ re.search() function will search the regular expression pattern and return the first occurrence. Unlike Python re.match(), it will check all lines of the input string. The Python re.search() function returns a match object when the pattern is found and “null” if the pattern is not found
  • 91. Character classes Character Description [abc] Either a or b or c [^abc] Except a and b and c [a-z] Any lower case alphabet symbol [A-Z] Any uppercase alphabet symbol [a-zA-Z] Any alphabet symbol [0-9] Any digit from 0 to 9 [a-zA-Z0-9] Any alphanumeric character [^a-zA-Z0-9] Except alphanumeric character(special characters)
  • 92. Sequence characters in regular expressions Pre-defined Character Description d Represents any digit (0-9) D Represents any non digit s Represents white spaces(t, n) S Represents non whitespaces w Represents any aplhanumeric(A-Z, a-z,0-9) W Represents any non aplhanumeric b Represents a space around words A Matches only at start of the string z Matches only at end of the string
  • 93. A python program to create a regular expression to retrieve all words starting with a given string It returned ‘ay’ also, ay is not a word it is a part of away. To avoid such things use b before and after the words in regular expression b represents a space around words w indicates any one alphanumeric character. Suppose if we write it as [w]*, Here * represents 0 or more repetitions. Hence [w]* represents 0 or more alphanumeric characters.
  • 94. a – any string starting with a ba- any word starts with a bab- matches only word a Ba- any string that contains but does not begin with a
  • 95. A python program to create a regular expression to retrieve all words starting with a numeric digit We want to retrieve all the words starting with a numeric digit i.e 0,1 – 9. The numeric digit is represented by ‘d’ and hence, the expression is r’d[w]*
  • 96. A python program to create a regular expression to retrieve all words having 5 characters length. A python program to create a regular expression to retrieve all words having 3 characters length.
  • 97. A python program to create a regular expression to retrieve all words between 3 or 4 or 5 characters length.
  • 98. QUANTIFIERS Character Description ? Occurs 0 or 1 time + Occurs 1 or more than one time * Occurs 0 or more times {n} Occurs n no. of times {n, } Occurs n or more than n times {x,y} Occurs at least x times but less than y times
  • 99. A python program to create a regular expression to retrieve the phone number of a person. d represents any digit(0-9) + indicates 1 or more times A python program to create a regular expression to retrieve the name of a person. D represents any digit(0-9) + indicates 1 or more times