We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !
Python uses class for object-oriented programming. A class represents the general behavior or information that the programmer or data analyst focuses on. When a class is created, particular objects belonging to this class can be created. This process is called instantiation. Class contains attributes, methods, or functions for general purpose. Attributes for instances can be modified by directly assigning new values, or by using methods defined in a class.
In the following code example, we first define a class for automobile. The class Auto contains four attributes: producer, type, year, and the distance(km) it has been driven. There are four methods (functions) defined for this class:
show_info() – to show basic information of the automobile.
show_km() – to show distance in kilometers the car has been driven.
update_km – to update the driving distance with the input new value.
increment_km() – to update the driving distance by add the increment to the current distance.
class Auto():
"""A class for automobile."""
def __init__(self, producer, type, year):
"""Initialize attributes to describe a car."""
self.producer = producer
self.type = type
self.year = year
self.km = 0
def show_info(self):
"""Show information of the car"""
full_info = str(self.year) + ' ' + self.producer + '
' + self.type
return full_info.title()
def show_km(self):
"""Show the kilometer of the car."""
print("The automobil has driven " + str(self.km) + "
now.")
def update_km(self, new_km):
"""
Update the distance of the car.
"""
if new_km >= self.km:
self.km = new_km
else:
print("The updated value should be larger than
the current!")
def increment_km(self, increment):
"""Add the given kilometer to the current"""
self.km += increment
After the class Auto definition is completed, object instances can be created by calling the class. In the following code example, an instance of Auto class, my_volvo is created. Then the driving distance is updated by different methods and shown.
#create an automobil instance from the class Auto
my_volvo = Auto('Volvo', 'V60', 2012)
#show basic information of my_volvo
print(my_volvo.show_info())
# 2012 Volvo V60
#show driving distance of the car
my_volvo.show_km()
# The automobil has driven 0 now.
#update the driving distance by method updata_km()
my_volvo.update_km(56700)
#show driving distance of the car
my_volvo.show_km()
#The automobil has driven 56700 now.
#make increment of the driving distance by method #increment_km()
my_volvo.increment_km(300)
#show driving distance of the car
my_volvo.show_km()
#The automobil has driven 57000 now.
#update driving distance by directly assigning a new value
#to attribute 'km'
my_volvo.km = 100000
#show driving distance of the car
my_volvo.show_km()
The automobil has driven 100000 now.
You can also watch video of Python full course training from our YouTube channel.
0 Comments