Wednesday, November 21, 2018

Python Ninja bootcamp 28-Q&A 4th

Learn Python like a Professional! Take You from 0 to Hero





In this Python lecture, we will learn about the Q&A fourth.


Answer the following Question-


Q. What are the four pillars of object-oriented programming?

  1. Abstraction- Abstraction is a process of exposing an essential feature of an entity while hiding other irrelevant detail.  
  2. Encapsulation- Binding of Data and Methods together to keep them safe from outside interference is called Encapsulation. Remember, Encapsulation is not data hiding, but Encapsulation leads to data hiding. 
  3. Inheritance- Inheritance is the process of creating a new class by using the features of the existing class. The new class is called is called Derived/ Sub/ Child class. The existing class is called Base/ Super/ Parent class.
  4. Polymorphism- Polymorphism is the ability to redefine methods for derived classes.

Q. Write Program to construct a class cuboid with attributes length, width and height. 

Also, create volume methods and surface area method to compute the volume and surface area of the cuboid.


class Cuboid:
    
    def __init__(self,length,breath,height):
        self.length = length
        self.breath = breath 
        self.height = height
        
    def volume(self):
        return self.length*self.breath*self.height
    
    def surface_area(self):
        return 2*(self.length*self.breath + self.breath*self.height + self.height*self.length)

ob1 = Cuboid(1,2,3)

print(ob1.volume())
>>6

print(ob1.surface_area())
>>22

Q.Write a function that accepts a number n and returns the factorial of n.



def factorial(n):
    fact=1
    for var in range(2,n+1):
        fact*=var
        
    return fact

factorial(5)
>> 120

Q. Write Program to construct a class line with attributes as a two X and Y tuple coordinate of the point. Also, define the distance method and slope method to calculate the distance and slope between them.

 

class Line:
    
    def __init__(self,coor1,coor2):
        self.coor1 = coor1
        self.coor2 = coor2
            
    def distance(self):
        x1,y1 = self.coor1
        x2,y2 = self.coor2
        return ((x1-x2)**2 + (y1-y2)**2)**0.5
    
    def slope(self):
        x1,y1 = self.coor1
        x2,y2 = self.coor2
        return (y2-y1)/(x2-x1)

ob2 = Line((1,2),(3,7))

ob2.distance()
>> 5.385164807134504
ob2.slope()
>> 2.5


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Error & Exception handling. 

https://sngurukuls247.blogspot.com/2018/11/python-ninja-bootcamp-29-error.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/


View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 




Feel free contact me on-


No comments:

Post a Comment