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 (10) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (156) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (121) 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 (53) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (912) Python Coding Challenge (295) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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