× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

OOPS In Python

OOPS:
'OOPS' means object oriented programming structure.It can be achieved by
1.Security
2.Reusability
3.Memory Management

OOPS Principles:
1.Encapsulation
2.Data abstraction
3.Inheritance
4.Polymorphism

By using these principles we can achieve above 3 properties is called OOPS.
Based on the OOPS principles programming languages are classified into 2 types

1.Object oriented programming language(OOPL)
2.Object based programming language(OBPL)

Object oriented programming language(OOPL):
If any language supports all the oops principles is known as Object oriented programming language(OOPL).
Example: C++ , Python , .NET , Java

Object based programming language(OBPL):
If any language supports all the oops principles except Inheritance and dynamic Polymorphism is known as Object based programming language(OBPL).
Example: javaScript , VBScript

Object: Object is as realtime entity, which is existing physically with some specific properties.
1.Identity
2.Attributes
3.Behavior

Examples of object is Pen , Book, TV, Remote, Water Bottle etc.
In Python:
class Addition:
    def add(self):
        x=10
        y=20
        z=30
        
        
In the above example
'Addition' is as Identity.
int x,y,z are a Attributes.
void add() is a Behavior.

Encapsulation

Binding of attributes and behaviors in a single container .In Python language it can be achieved by 'class' keyword.
class:
'class' is container which is collection of variable and methods (or) it is a blueprint of the object.
Syntax:

class ClassName:
    def methodname(self):
        list of variables
        ..
        ..
        ..
    ...
    ...
    ...
    
in the above syntax class is keyword.
ClassName any userdefined name.




Variable:
Variable is an identifer whose value can can be changed.
Example:
x=10;
x=20;

In Python language these variable are classified into following types

1.Local variable
2.Global variable
3.Formal variable
4.Block variable

Local variable:
If any variable declare with in method or constructor.
Global variable
If any variable declare outside of the class.
Formal variable
If any variable declare with in method signature or constructor signature.
Block variable
If any variable declare with in block(if block ,for block ete..)

x=10  #Global Variable
class Demo:
    def add(self,y):#Formal Variable
        z=20 #Local Variable
        if(condition):
            p=30 #BLock Variable
Global and Local Variable Program
Image

x=12 #global Varaible
class Rain:
   
    def myfun(self):
        y=10  #local Variable
        print("Local Varialble of myfun ",y)
        print("Global Varialble of myfun ",x) 
    def mydisp(self):
        z=20; #local Variable
        print("Global Varialble of mydisp ",x)
        #print(y) Here y is local Varialbe of myfun
        print("Local Varialble of mydisp ",z)
    
if __name__=="__main__":
    r=Rain()
    r.myfun()
    r.mydisp()
                                            

                                        
Output:
x value is 12
y value is 10
x value is 12
z value is 20
Method:

Method is a collection of statements used to perform specific operation.

Syntax:
def methdname(self,list of parameters/no parameters):
    ....
    ...
    ..
    
In the above syntax def is a method Defination

returntype:
These are classified into two types.
1.Noreturn type
2.Return type
Method name any userdefined name.
In Python language these methods are classisfied into following types
1.Noparametarized method
2.Parametarized Method

Noparametarized method: If any method signature does not contain parameters is known as Noparametarized method
Syntax:
def methdname(self):
    ....
    ...
    ..
Parametarized Method
If any method signature contains List of parameters is known as parametarized method
Syntax:
def methdname(self,variable1,variable2):
    ....
    ...
    ..

Based on the return type these methods are classified into following types
1.No return type with no Parametarized
2.No return type with Parametarized
3.return type with no Parametarized
4.return type with Parametarized

1. MaxNumber Program Using No return type with No Parametarized
class Maxnum:
    def max(self):
        x=10
        y=20
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    m=Maxnum()
    m.max()    
    
Output:
y is Max
class Maxnum:
    def max(self):
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    m=Maxnum()
    m.max()    
    
Output:
Enter x ,y values
23 45
y is Max
2. MaxNumber Program Using No return type with Parametarized
class Maxnum:
    def max(self,x,y):
        
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    m=Maxnum()
    m.max(10,20)    
    
Output:
y is Max
class Maxnum:
    def max(self,x,y):
        
        if(x>y):
            print("x is max")
        else:
            print("y is max")

if __name__=="__main__:
    m=Maxnum()
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value"))  
    m.max(x,y)    
    
Output:
Enter x ,y values
23 45
y is Max
3. MaxNumber Program Using return type with No Parametarized
class Maxnum:
    def max(self):
        x=10
        y=20
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    m=Maxnum()
    s=m.max()  
    print(s)
    
Output:
y is Max
class Maxnum:
    def max(self):
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))  
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    m=Maxnum()
    s=m.max()  
    print(s)
    
Output:
Enter x ,y values
23 45
y is Max
4. MaxNumber Program Using return type with Parametarized
class Maxnum:
    def max(self,x,y):
        
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    m=Maxnum()
    s=m.max(10,20)  
    print(s)
    
Output:
20
class Maxnum:
    def max(self,x,y):
         
        if(x>y):
            return("x is max")
        else:
            return("y is max")

if __name__=="__main__:
    m=Maxnum()
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value")) 
    s=m.max(x,y)  
    print(s)
    
Output:
Enter x ,y values
23 45
45
3. MaxNumber Program Using return type with No Parametarized (Integer return)
class Maxnum:
    def max(self):
        x=10
        y=20
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    m=Maxnum()
    s=m.max()  
    print(s)
    
 
Output:
20
class Maxnum:
    def max(self):
        x=int(input("Enter x Value"))
        y=int(input("Enter y Value"))
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    m=Maxnum()
    s=m.max()  
    print(s)    
Output:
Enter x ,y values
23 45
45
4. MaxNumber Program Using return type with Parametarized(Integer Return)
class Maxnum:
    def max(self,x,y):
        
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    m=Maxnum()
    s=m.max(10,20)  
    print(s)
    
 
Output:
20
class Maxnum:
    def max(self,x,y):
        
        if(x>y):
            return x
        else:
            return y  

if __name__=="__main__:
    m=Maxnum()
    x=int(input("Enter x Value"))
    y=int(input("Enter y Value"))
    s=m.max(x,y)  
    print(s)    
Output:
Enter x ,y values
23 45
45
Factorial Progrram Using Method 4 Ways

Program-1:
1.No return type with no Parametarized
Program-2:
2.No return type with Parametarized
Program-3:
3.return type with no Parametarized
Program-4:
4.return type with Parametarized

Factorial Program Using No return type with No Parametarized

class Factorial:
    def fact(self):
        x=1
        f=1
        n=int(input("enter n value"))
        while(x<=n):
            f=f*x
            x=x+1
        print(f)

if __name__=="__main__":
    ff=Factorial()
    ff.fact()
    
Output:
Enter n Value
5
120

Factorial Program Using No return type with Parametarized

class Factorial:
    def fact(self,n):
        x=1
        f=1
        
        while(x<=n):
            f=f*x
            x=x+1
        print(f)

if __name__=="__main__":
    ff=Factorial()
    n=int(input("enter n value"))
    ff.fact(n)
    
Output:
Enter n Value
5
120

Factorial Program Using return type with No Parametarized

class Factorial:
    def fact(self):
        x=1
        f=1
        n=int(input("enter n value"))
        while(x<=n):
            f=f*x
            x=x+1
        return(f)

if __name__=="__main__":
    ff=Factorial()
    
    s=ff.fact()
    print(s)
    
Output:
Enter n Value
5
120

Factorial Program Using return type with Parametarized

class Factorial:
    def fact(self,n):
        x=1
        f=1
        
        while(x<=n):
            f=f*x
            x=x+1
        return(f)

if __name__=="__main__":
    ff=Factorial()
    n=int(input("enter n value"))
    s=ff.fact(n)
    
Output:
Enter n Value
5
120