Friday, July 25, 2025

DSA using Python: Assignment - 2: Classes and Objects



“In this assignment, we will learn how to use Python classes and objects to build reusable and structured code. The tasks cover real-world examples like persons, shapes, books, and teams.”



Assignment - 2: Classes and Objects

### 1️⃣ Class: Person

1. Define a python class Person with instance object variables name and age. Set
Instance object variables in __init__() method. Also define show() method to display name and age of a person.

### 2️⃣ Class: Circle
2. Define a class Circle with instance object variable radius. Provide setter and getter
for radius. Also define getArea() and getCircumference() methods.

### 3️⃣ Class: Rectangle
3. Define a class Rectangle with length and breadth as instance object variables.
Provide setDimensions(), showDimensions() and getArea() method in it.

### 4️⃣ Class: Book
4. Define a class Book with instance object variables bookid, title and price. Initialise them via __init__() method. Also define method to show book variables.

### 5️⃣ Class: Team
5. Define a class Team with instance object variable a list of team member names. Provide methods to input member names and display member names.




"These 5 class-based problems help in building a solid foundation in Object-Oriented Programming using Python. Understanding these basics is essential for any aspiring Python Developer."


#Python #DSAwithPython #OOP #ClassesAndObjects #PythonAssignment #LearnPython #FullStackPython

2 comments:

  1. class Person:
    def __init__(self, name, age):
    self.name = name
    self.age = age

    def show(self):
    print(f"Name: {self.name}, Age: {self.age}")


    import math

    class Circle:
    def __init__(self, radius=0):
    self.__radius = radius

    def setRadius(self, radius):
    self.__radius = radius

    def getRadius(self):
    return self.__radius

    def getArea(self):
    return math.pi * self.__radius ** 2

    def getCircumference(self):
    return 2 * math.pi * self.__radius


    class Rectangle:
    def __init__(self):
    self.length = 0
    self.breadth = 0

    def setDimensions(self, length, breadth):
    self.length = length
    self.breadth = breadth

    def showDimensions(self):
    print(f"Length: {self.length}, Breadth: {self.breadth}")

    def getArea(self):
    return self.length * self.breadth


    class Book:
    def __init__(self, bookid, title, price):
    self.bookid = bookid
    self.title = title
    self.price = price

    def show(self):
    print(f"Book ID: {self.bookid}, Title: {self.title}, Price: {self.price}")


    class Team:
    def __init__(self):
    self.members = []

    def inputMembers(self):
    n = int(input("Enter number of team members: "))
    for i in range(n):
    name = input(f"Enter name of member {i+1}: ")
    self.members.append(name)

    def displayMembers(self):
    print("Team Members:")
    for name in self.members:
    print(name)



    # 1. Person
    p1 = Person("Ali", 25)
    p1.show()

    # 2. Circle
    c1 = Circle()
    c1.setRadius(7)
    print("Area:", c1.getArea())
    print("Circumference:", c1.getCircumference())

    # 3. Rectangle
    r1 = Rectangle()
    r1.setDimensions(5, 3)
    r1.showDimensions()
    print("Area:", r1.getArea())

    # 4. Book
    b1 = Book(101, "Python Programming", 499.99)
    b1.show()

    # 5. Team
    t1 = Team()
    t1.inputMembers()
    t1.displayMembers()

    ReplyDelete

Recursion in DSA using Python

Follow me If you want to understand with  Graphical Representation   click on it. You can find the explanatory video at the bottom of this p...