Python Practice Multiple Inheritance Loops

In this class, We discuss Python Practice Multiple Inheritance Loops.

For Complete YouTube Video: Click Here

The reader will have a deeper understanding of the concepts of python.

After taking the complete placement training course. The reader can easily crack any campus placement exam.

Try to solve the questions on your own, then check for a solution.

Q1)
What is the output displayed by the below code?

class A:
    aval=10
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def method1(self):
        print("this is class a method")
        self.w=1 
class B:
    bval=20
    def __init__(self,z):
        self.z=z
    def method1(self):
        print("this is class b method")
        z=self.z+B.bval
        print(z)
class C(A,B):
    aval=30
    def __init__(self,x,y,z,w):
        A.__init__(self,x,y)
        B.__init__(self,z)
        self.w=w
    def method1(self):
        super().method1()
        print(self.w)
        A.method1(self)
        B.method1(self)
        A.aval=B.bval+C.aval
        print(A.aval)
        print(C.aval)
obj=C(10,20,30,40)
obj.method1()

In the above program, we are using the concept of multiple inheritances.

To learn the basics of multiple inheritances, click here.

When we call the superclass methods using multiple inheritances. Python checks the method from left to right.

In our example, method1 is taken from class A.

Point to understand:  when creating the child class object, The instance variable w is already defined.

Given below is the output displayed by the program.

Output:
this is class a method
1
this is class a method
this is class b method
50
50
30
Q2)
What is the output displayed by the below code?

counter=0
sumval=2
for counter in range(1,6):
    sumval+=counter
    if(sumval!=10):
        continue
    if(sumval>counter*2):
        break
while(True):
    sumval+=counter
    if(sumval<counter):
        break
print(sumval)   

In the above program, The while loop is never going to stop.

The loop executes infinite times.

After executing the first for loop, the sumval variable value is above ten.

The while loop will break if sumval<counter.

The condition sumval<counter will never happen. That’s why the program goes to an infinite loop.

For a detailed explanation, please watch the video.