Object Oriented Programming in Python
In this article, you will learn the Python program and Python program and the basic concepts and examples.
Table of Contents
- Introduction to OOP in Python
- Class
- Object
- Methods
- Inheritance
- Encapsulation
- Polymorphism
- Key Points to Remember
Introduction to OOP in Python:
Python is a multi-paradigm program. That is, it supports a variety of programs.
The most popular way to solve the problem by making software programs. This is called a product-oriented program (OOP).
The object has two characteristics:
• characteristics
• Conditions
Let's take an example:
Parrot is a thing,
• Name, age, color characteristics
• Singing, dance is a tradition
Python's program objective is to focus on creating a recyclable number. This idea is also called dry (Do not repeat itself).
In Python, the idea of the program's program is fundamental principles:
Inheritance
|
A process of using details from a new class without modifying an existing class.
|
Encapsulation
|
Hiding the private details of a class from other objects.
|
Polymorphism
|
A concept of using common operation in different ways for different data input.
|
Class:
The class is a type of thing.
We can think of a parrot and a label. This includes all the details about the name, color, size, etc. According to this explanation, we can study the nursery schools. Here, theft is something.
Examples of the Parrot class may be:
Class Parrot:
Pass
Here, we use the keyword in the classroom to describe a clean class design. From class, I will build institutions. Examples are specific items made from a particular class.
Object:
An object (eg) is possible in the classroom. When classified, the only explanation of the item is defined. Therefore, it does not give memory or storage.
Examples of laboratory labels can be Parrot:
Here, the parrot obj classroom.
Please have a description of the brand. Now, we want to show you how to do stuff and classrooms.
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.age = age
# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))
# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
When we implement the program, productivity will be:
Blu is a bird
Woo is also a bird
Blu is 10 years old
Woo is 15 years old
Next, we create a classified classroom. Here, Blu and Woo are a reference (value) for a new product.
Then, we find the class features using the __CLASS __. Class sizes are the same in all parts of the classroom. Also, obtain the example attributes used in blu.age blu.name. However, for example, the character is different from any type of class.
Method:
The system of procedures is defined in the classroom. It is used to describe the behavior of the product.
class Parrot:
# instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def sing(self, song):
return "{} sings {}".format(self.name, song)
def dance(self):
return "{} is now dancing".format(self.name)
# instantiate the object
blu = Parrot("Blu", 10)
# call our instance methods
print(blu.sing("'Happy'"))
print(blu.dance())
When we implement the program, productivity will be:
Blu sings 'Happy'
Blu is now dancing
In the first program, two methods are defined, which are songs (), and dance (). This system is called for example, because it is in Blu-ray.
Inheritance:
Support is a way to create a new classroom using a classroom that explains nothing. The new class produced by class (or children's classes). Also, there is a classroom (or a parent class).
# parent class
class Bird:
def __init__(self):
print("Bird is ready")
def whoisThis(self):
print("Bird")
def swim(self):
print("Swim faster")
# child class
class Penguin(Bird):
def __init__(self):
# call super() function
super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Penguin")
def run(self):
print("Run faster")
peggy = Penguin()
peggy.whoisThis()
peggy.swim()
peggy.run()
When we implement this program, production will be:
Bird is ready
Penguin is ready
Penguin
Swim faster
Run faster
The program was created in two classrooms such as the Teacher (parent class) and penguin (classroom children). the classroom is between the parents' class. We can see the swimming pool (). Again, the classroom has changed the behavior of parents in the classroom. Here we can see this method. Next, we increase classroom parents, so the new way of true ().
We also use super () before __ __init (). This is because we want to produce the __init () types in the classroom for the parents' classroom.
Encapsulation:
Using the Python programming program, we can control the availability of methods and variables. This prevents data transfers from the camps. In Python, we indicate the specific use of the line as a block, such as "_" or "double" or "__".
class Computer:
def __init__(self):
self.__maxprice = 900
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Computer()
c.sell()
# change the price
c.__maxprice = 1000
c.sell()
# using setter function
c.setMaxPrice(1000)
c.sell()
When we implement this program, production will be:
Selling Price: 900
Selling Price: 900
Selling Price: 1000
The highest level of our program is defined in the computer class. We use the __ __init () method for the computer. We try to evaluate the value. However, we can not change because Python works on __maxprice like the private sector. To change the value, the setMaxPrice service is used, ie, where () it takes the value of the item.
Polymorphism:
Polymorphism is the power (in OOP) using a common interface in several types (data type).
Tell them that we have color schemes, there are different types of rails (rails, square, circle). However, we can use the same methods and different color types. This idea is called polymorphism.
class Parrot:
def fly(self):
print("Parrot can fly")
def swim(self):
print("Parrot can't swim")
class Penguin:
def fly(self):
print("Penguin can't fly")
def swim(self):
print("Penguin can swim")
# common interface
def flying_test(bird):
bird.fly()
#instantiate objects
blu = Parrot()
peggy = Penguin()
# passing the object
flying_test(blu)
flying_test(Peggy)
When we implement the old program, productivity will be:
Parrot can fly
Penguin can't fly
In the first two classes, we multiply the exercise and penguin. Each of them must be the way ().
To getting expect level training for Python training in your Location – Python training in Chennai | Python training institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore | Python training in Electronic City | Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram | Python training in Annanagar | Python training in Marathahalli | Python training in Btm | Python Online Training | Online Certification Course | Python Course in Chennai | Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli | Python training institute in Marathahalli | Python course institute in Btm | Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore
ReplyDeleteNice blog! Thanks for sharing this valuable information
RPA Training in Hyderabad
RPA Training in Gurgaon
Mindblowing blog very useful thanks
ReplyDeleteGerman Classes in OMR
German Classes in chennai
This blog is really helpful to deliver updated affairs over the internet which is really appreciable.
ReplyDeleteBest Coding Courses in Rajasthan