Saturday, 2 November 2024

Python OOPS Challenge | Day 6 |What is the output of following Python code?



In this code, we define a class Rectangle with an initializer method __init__ and a method calcPerimeter to calculate the perimeter of the rectangle.

Code Breakdown:

1. __init__ method: This is the constructor of the class. When an object of the Rectangle class is created, it initializes the attributes a and b with the values provided as arguments.

def __init__(self, aVal, bVal):
    self.a = aVal
    self.b = bVal

Here, self.a and self.b are set to aVal and bVal, respectively.


2. calcPerimeter method: This method calculates the perimeter of the rectangle using the formula .

def calcPerimeter(self):
    return 2 * self.a + 2 * self.b


3. Creating an object and calling calcPerimeter:

obj = Rectangle(1, 5)
print(obj.calcPerimeter())

Here, we create an object obj of the Rectangle class with a = 1 and b = 5.

Then, calcPerimeter is called on obj, which calculates .




Output:

The output of this code is 12.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (353) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses