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

Regex,functions, inheritance,class, attribute,overloding

  • 1.
  • 2.
    Python- OOPs concept: Classand object, Attributes, Inheritance, Overloading Overriding, Data hiding. Regular Expressions: Match function, Search function, Matching VS Searching, Modifiers Patterns.
  • 3.
    Object-oriented programming isone 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 languageslike 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 Function3 Sub task1 Sub task 2 Sub task3 PROCEDURE ORIENTED APPROACH
  • 6.
    PROBLEMS IN PROCEDUREORIENTED 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 ⦿ OOPsis 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 withdata 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 PYTHONLANGUAGE ⦿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: Thereare 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 isa 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 newclasses 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.
  • 15.
  • 16.
    CLASS OBJECTS CAR SUV KIA CLASSOBJECTS 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 objecthas 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.
  • 18.
  • 19.
    Person Id Name Age Sex City Ph no Eat() Walk() Study() Play() Person 2/Emp2: 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’
  • 21.
  • 22.
    CREATE A CLASSWITH AN OBJECT
  • 23.
    CREATE A CLASSWITH MULTIPLE OBJECTS
  • 25.
    METHOD EXECUTION VSCONSTRUCTOR EXECUTION
  • 26.
    THE SELF VARIABLE: ⦿ ‘Selfis 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 aspecial 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()
  • 28.
  • 29.
    CREATE A CLASSWITH CONSTRUCTOR
  • 30.
    CLASS WITH AMETHOD (FUNCTION) EG: 1
  • 31.
    CLASS WITH METHODS(FUNCTIONS) EG: 2
  • 32.
    CLASS WITH METHODS(FUNCTIONS) Eg: 3
  • 33.
    A python programto define student class and create an object to it. Also calling the method and display the students details.
  • 34.
    TYPES OF VARIABLES: ⦿Thevariables which are written inside a class are of 2 types: ⦿Instance Variables ⦿Class variables or Static variables
  • 35.
    INSTANCE VARIABLE If wechange 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 CONSTRUCTORBY 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 INSTANCEMETHOD 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 OFTHE CLASS BY USING OBJECT REFERENCE VARIABLE: ⦿We can also add instance variables outside of a class to a particular object.
  • 39.
    EXAMPLE OF INSTANCEVARIABLE Instance variables
  • 40.
    STATIC VARIABLES: ⦿Class variablesare 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/CLASSVARIABLES Static/ Class variable To access the static method we have to use object name dot(.) static variable name
  • 42.
    Another way toaccess the static variable
  • 43.
    HOW TO MODIFYSTATIC 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 DELETESTATIC VARIABLE We can delete static variables from anywhere by using the following syntax del classname.variablename
  • 45.
    TYPES OF METHODS ⦿Thepurpose 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
  • 46.
  • 47.
    CLASS METHODS ⦿Inside methodimplementation 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.
  • 49.
    STATIC METHODS ⦿Static methodare 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.
  • 51.
  • 53.
    INHERITANCE ⦿ Inheritance isan 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 relationshipfrom 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
  • 56.
    1. SINGLE INHERITANCE: ⦿Whena 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.
  • 57.
  • 58.
    MULTI-LEVEL INHERITANCE ⦿ Themulti-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
  • 60.
    MULTIPLE INHERITANCE ⦿ Multipleinheritances 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
  • 62.
    POLYMORPHISM ⦿ Polymorphism isa 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 operatoris 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 amethod 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)
  • 70.
    METHOD OVERRIDING ⦿When thereis 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.
  • 72.
    DATA HIDING ⦿It isa 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.
  • 75.
  • 76.
    REGULAR EXPRESSION: ⦿ Aregular 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
  • 78.
    The regular expressionsare 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 methodsbelong 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.
  • 82.
    Sequence characters inregular 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 #importingthe 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 programto create a regular expression to search for strings starting with ‘c’ and having 3 characters using the SEARCH METHOD()
  • 85.
    A python programto 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 programto create a regular expression to search for strings starting with ‘m’ and having 3 characters using the findall() method
  • 87.
    A PYTHON PROGRAMTO CREATE A REGULAR EXPRESSION TO REPLACE A STRING WITH A NEW STRING
  • 88.
    FULLMATCH() ⦿ We canuse 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.
  • 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 inregular 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 programto 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 – anystring 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 programto 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 programto 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 programto create a regular expression to retrieve all words between 3 or 4 or 5 characters length.
  • 98.
    QUANTIFIERS Character Description ? Occurs0 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 programto 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