Python Object Oriented Programming, Classes, Objects, Methods, Constructors, Inheritance, Polymorphism, Abstraction, and Encapsulation.
Python Object-Oriented Programming
Object-oriented programming (OOP) refers to a type of computer programming in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.
Simula is considered the first object-oriented programming language, Smalltalk is considered the first truly object-oriented programming language.
The popular object-oriented languages are C++, Java, C#, PHP, Python, Ruby, Kotlin, etc.
Everything in Python is an object. An object has a state and behaviors. To create an object, you define a class first. And then, from the class, you can create one or more objects. The objects are instances of a class.
Python OOP Major Concepts and Principles:
- Class
- Object
- Method
- Inheritance
- Polymorphism
- Data Abstraction
- Encapsulation
1. Class
A Class is like an object constructor or a “blueprint” for creating objects.
Create or Define a Python class:
To define a class, we use the class keyword followed by the class name. For example, the following defines a Country class:
class Country:
pass
Note: The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when an empty code is not allowed.
2. Object
The object is an entity that has a state and behavior. It may be any real-world object like a mouse, keyboard, chair, table, pen, etc. When we define a class, it needs to create an object to allocate the memory.
To create an object from the Country class, you use the class name followed by parentheses (), like calling a function:
Syntax:
objectname = Classname()
Example 1:
class bus:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)
c1 = bus(“Tata”, 2016)
c1.display()
Example 2:
class myclass:
def add(self, num1, num2): #instance method
res = num1+num2
return res
def mul(num1, num2): #class method
res = num1*num2
return res
myobject = myclass()
x = myobject.add(10,76)
print(x)
x = myclass.mul(10,76)
print(x)
Note: The self is used to represent the instance of the class.
3. Method
A Python method is like a Python function, but it must be called on an object.
Example:
class abcd:
def mymethod(self):
print (“It is a Method”)
def method2(self):
pass
obj= abcd()
obj.mymethod()
obj.method2()
Python Complete Tutorial
Python Video Tutorial
Python Programming Syllabus
Python Programming Quiz
Python Interview Questions for Fresher
1. Introduction to Python Programming Language
2. Download and Install Python
4. Python Keywords and Identifiers
9. Python Conditional Statements
11. Python Branching Statements
14. Python Data Structures – Lists
15. Python Data Structures – Sets
16. Python Data Structures – Tuples
17. Python Data Structures – Dictionaries
18. Python User Defined Functions
Follow me on social media: