Code Explanation:
Importing the math module
import math
This imports Python’s built-in math module.
We need it because math.pi gives the value of π (3.14159…).
Defining the Circle class
class Circle:
This starts the definition of a class named Circle.
A class is a blueprint for creating objects.
Initializer method (constructor)
def __init__(self, r):
self.r = r
Explanation:
__init__ is called automatically when an object is created.
It receives r (radius).
self.r = r stores the radius in the object’s attribute r.
So, when we do Circle(5),
the object stores r = 5 inside itself.
Creating a readable property: area
@property
def area(self):
return math.pi * self.r**2
Explanation:
@property decorator
Turns the method area() into a property, meaning you can access it like a variable, not a function.
area calculation
Formula used:
Area = π × r²
So:
Area = math.pi * (5)^2
= 3.14159 * 25
= 78.5398...
Printing the area (converted to integer)
print(int(Circle(5).area))
Breakdown:
Circle(5) → Creates a Circle with radius = 5.
.area → Gets the computed area property (≈ 78.5398).
int(...) → Converts it to an integer → 78.
print(...) → Prints 78.
Final Output
78


0 Comments:
Post a Comment