0% found this document useful (0 votes)
16 views3 pages

Python Classes: Circle, Person, Calculator

The document contains three coding exercises that demonstrate object-oriented programming concepts in Python. The first exercise defines a Circle class to calculate the area and perimeter of a circle. The second defines a Person class to store personal details and calculate ages. The third defines a Calculator class to perform basic math operations and handle errors.

Uploaded by

17ddt22f1036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Python Classes: Circle, Person, Calculator

The document contains three coding exercises that demonstrate object-oriented programming concepts in Python. The first exercise defines a Circle class to calculate the area and perimeter of a circle. The second defines a Person class to store personal details and calculate ages. The third defines a Calculator class to perform basic math operations and handle errors.

Uploaded by

17ddt22f1036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 1

import math

class Circle:
def __init__(self, radius):
[Link] = radius

def calculate_circle_area(self):
return [Link] * [Link]**2

def calculate_circle_perimeter(self):
return 2 * [Link] * [Link]

radius = float(input("Input the radius of the circle: "))

circle = Circle(radius)

area = circle.calculate_circle_area()

perimeter = circle.calculate_circle_perimeter()

print("Area of the circle:", area)


print("Perimeter of the circle:", perimeter)

Output

Exercise 2

from datetime import date

class Person:
def __init__(self, name, country, date_of_birth):
[Link] = name
[Link] = country
self.date_of_birth = date_of_birth

def calculate_age(self):
today = [Link]()
age = [Link] - self.date_of_birth.year
if today < date([Link], self.date_of_birth.month, self.date_of_birth.day):
age -= 1
return age
person1 = Person("Ferdi Odilia", "France", date(1962, 7, 12))
person2 = Person("Shweta Maddox", "Canada", date(1982, 10, 20))
person3 = Person("Elizaveta Tilman", "USA", date(2000, 1, 1))

print("Person 1:")
print("Name:", [Link])
print("Country:", [Link])
print("Date of birth:", person1.date_of_birth)
print("Age:", person1.calculate_age())

print("Person 2:")
print("Name:", [Link])
print("Country:", [Link])
print("Date of birth:", person2.date_of_birth)
print("Age:", person2.calculate_age())

print("Person 3:")
print("Name:", [Link])
print("Country:", [Link])
print("Date of birth:", person3.date_of_birth)
print("Age:", person3.calculate_age())

Output
Exercise 3

class Calculator:
def add(self, x, y):
return x + y

def subtract(self, x, y):


return x - y

def multiply(self, x, y):


return x * y

def divide(self, x, y):


if y == 0:
raise ZeroDivisionError("Division by zero is not allowed")
return x / y

calculator = Calculator()

result = [Link](7, 5)
print("7 + 5 =", result)

result = [Link](34, 21)


print("34 - 21 =", result)

result = [Link](54, 2)
print("54 * 2 =", result)

result = [Link](144, 2)
print("144 * 2 =", result)

try:
result = [Link](45, 0)
print("45 / 0 =", result)

except ZeroDivisionError as e:
print("Error:", e)

Output

You might also like