filmov
tv
Python Practice on Parameters Inheritance || Lesson 2 || Python Placements || Learning Monkey ||

Показать описание
#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
Python Practice on Parameters Inheritance
In this class, we do python practice on parameters inheritance.
The examples discussed will help in improving coding skills.
These placement training examples give you a deeper understanding of the concepts of python.
Check the questions given below and try to solve them on your own. That is the best practice.
Q1) Check the program given below and identify the output displayed by the program.
def function1(arg1,*arg2):
print(arg1)
print(arg2)
for num in arg2:
if arg1num:
return num
return 0
function1(9,12,45,0)
In the above program, they are asking the value stored in variables arg1 and arg2.
They are trying to check your understanding of different argument types in python.
Variable arg2 has a * symbol. These variables take the variable number of arguments.
function1(9,12,45,0). The function called with values 9,12,45,0.
Nine is assigned to argument arg1.
12,45,0 is assigned to arg2.
Q2) Check the below code and identify the output displayed.
class flat:
def __init__(self,flatrent):
self.__flatrent=flatrent
self.__totalrent=None
def gettotalrent(self):
return self.__totalrent
def settotalrent(self,value):
self.__totalrent=value
def calculatetotalrent(self,maintainancecost):
self.__totalrent=self.__flatrent+maintainancecost//2
class furnishedflat(flat):
def __init__(self,flat_rent,amenity_cost):
super().__init__(flat_rent)
def calculatetotalrent(self,maintainancecost):
super().calculatetotalrent(maintainancecost//2)
flat1=furnishedflat(7000,800)
The above program is an inheritance problem. These types of programs are solved in a bottom-up approach.
__flatrent instance variable is a private variable. We can not access the variable outside the class.
To access the private variable. We write getter and setter methods.
To get the value of the total rent variable. The getter method is written in the program.
To set the value to total rent variable. The setter method is written.
The above example use method overriding and super-class constructor call.
With all these concepts, you should be flexible in solving the questions.
Created an object for furnished flat class, and variables are initialized with 7000 and 800.
Called the calculate total rent method using the furnished flat object. The maintenance cost is 600.
Calculate total rent method is calling the super-class method with the value 300.
This is how it continues its execution. Analyze the output displayed.
The output displayed is 7950.
Q3) Check the code given below. Identify the output displayed.
class house:
def __init__(self):
self.__initial_rent=4000.0
def find_rent_per_person(self,no_of_persons):
return self.__initial_rent/no_of_persons
def calculate_maintainance_cost(self,maint_percent):
return self.__initial_rent*((maint_percent+5)/100)
def get_initial_rent(self):
return self.__initial_rent
class twoBHKhouse(house):
def find_rent_per_person(self,no_of_persons):
return rent/no_of_persons
class threeBHKhouse(twoBHKhouse):
def find_rent_per_person(self,number_of_persons):
return super().find_rent_per_person(number_of_persons)
house_obj=threeBHKhouse()
The output displayed by the program.
Final rent of each person: 2500.0
Initial rent: 4000.0
Link for playlists:
Python Practice on Parameters Inheritance
In this class, we do python practice on parameters inheritance.
The examples discussed will help in improving coding skills.
These placement training examples give you a deeper understanding of the concepts of python.
Check the questions given below and try to solve them on your own. That is the best practice.
Q1) Check the program given below and identify the output displayed by the program.
def function1(arg1,*arg2):
print(arg1)
print(arg2)
for num in arg2:
if arg1num:
return num
return 0
function1(9,12,45,0)
In the above program, they are asking the value stored in variables arg1 and arg2.
They are trying to check your understanding of different argument types in python.
Variable arg2 has a * symbol. These variables take the variable number of arguments.
function1(9,12,45,0). The function called with values 9,12,45,0.
Nine is assigned to argument arg1.
12,45,0 is assigned to arg2.
Q2) Check the below code and identify the output displayed.
class flat:
def __init__(self,flatrent):
self.__flatrent=flatrent
self.__totalrent=None
def gettotalrent(self):
return self.__totalrent
def settotalrent(self,value):
self.__totalrent=value
def calculatetotalrent(self,maintainancecost):
self.__totalrent=self.__flatrent+maintainancecost//2
class furnishedflat(flat):
def __init__(self,flat_rent,amenity_cost):
super().__init__(flat_rent)
def calculatetotalrent(self,maintainancecost):
super().calculatetotalrent(maintainancecost//2)
flat1=furnishedflat(7000,800)
The above program is an inheritance problem. These types of programs are solved in a bottom-up approach.
__flatrent instance variable is a private variable. We can not access the variable outside the class.
To access the private variable. We write getter and setter methods.
To get the value of the total rent variable. The getter method is written in the program.
To set the value to total rent variable. The setter method is written.
The above example use method overriding and super-class constructor call.
With all these concepts, you should be flexible in solving the questions.
Created an object for furnished flat class, and variables are initialized with 7000 and 800.
Called the calculate total rent method using the furnished flat object. The maintenance cost is 600.
Calculate total rent method is calling the super-class method with the value 300.
This is how it continues its execution. Analyze the output displayed.
The output displayed is 7950.
Q3) Check the code given below. Identify the output displayed.
class house:
def __init__(self):
self.__initial_rent=4000.0
def find_rent_per_person(self,no_of_persons):
return self.__initial_rent/no_of_persons
def calculate_maintainance_cost(self,maint_percent):
return self.__initial_rent*((maint_percent+5)/100)
def get_initial_rent(self):
return self.__initial_rent
class twoBHKhouse(house):
def find_rent_per_person(self,no_of_persons):
return rent/no_of_persons
class threeBHKhouse(twoBHKhouse):
def find_rent_per_person(self,number_of_persons):
return super().find_rent_per_person(number_of_persons)
house_obj=threeBHKhouse()
The output displayed by the program.
Final rent of each person: 2500.0
Initial rent: 4000.0
Link for playlists:
Комментарии