Common Class-Based Python Practice Problems
Problem 1: Student Class: Pass/Fail Check
Problem:
Create a class `Student` with attributes `name`, `marks` (out of 100). Write methods to display details and
check if the student has passed (marks >= 40).
Solution:
```python
class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks
def display(self):
print(f"Name: {[Link]}, Marks: {[Link]}")
def has_passed(self):
return [Link] >= 40
# Test
s1 = Student("Rahul", 55)
[Link]()
print("Passed:", s1.has_passed())
```
Common Class-Based Python Practice Problems
Problem 2: BankAccount Class: Deposit & Withdraw
Problem:
Create a class `BankAccount` with attributes `account_holder` and `balance`. Implement methods to deposit
money, withdraw money (if sufficient balance), and display balance.
Solution:
```python
class BankAccount:
def __init__(self, account_holder, balance=0):
self.account_holder = account_holder
[Link] = balance
def deposit(self, amount):
[Link] += amount
def withdraw(self, amount):
if amount > [Link]:
print("Insufficient balance")
else:
[Link] -= amount
def display_balance(self):
print(f"{self.account_holder}'s balance: {[Link]}")
Common Class-Based Python Practice Problems
# Test
acc = BankAccount("Priya", 1000)
[Link](500)
[Link](200)
acc.display_balance()
```
Problem 3: Employee Class: Salary Raise
Problem:
Create an `Employee` class with `name` and `salary`. Add a method to increase salary by a given
percentage.
Solution:
```python
class Employee:
def __init__(self, name, salary):
[Link] = name
[Link] = salary
def give_raise(self, percent):
[Link] += ([Link] * percent / 100)
def display(self):
print(f"Employee: {[Link]}, Salary: {[Link]}")
Common Class-Based Python Practice Problems
# Test
e = Employee("Anita", 50000)
e.give_raise(10)
[Link]()
```
Problem 4: Shape Classes with Inheritance
Problem:
Create a base class `Shape` with a method `area()` that returns 0. Then create classes `Rectangle` and
`Circle` inheriting from `Shape`. Override `area()` to calculate correct area.
Solution:
```python
import math
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, width, height):
[Link] = width
[Link] = height
Common Class-Based Python Practice Problems
def area(self):
return [Link] * [Link]
class Circle(Shape):
def __init__(self, radius):
[Link] = radius
def area(self):
return [Link] * [Link] ** 2
# Test
r = Rectangle(5, 3)
c = Circle(4)
print("Rectangle area:", [Link]())
print("Circle area:", [Link]())
```
Problem 5: Library Book Management
Problem:
Create a class `Book` with `title`, `author`, and `copies`. Write a method to display book details and a method
to borrow a book (reducing copies by 1 if available).
Solution:
Common Class-Based Python Practice Problems
```python
class Book:
def __init__(self, title, author, copies):
[Link] = title
[Link] = author
[Link] = copies
def display(self):
print(f"{[Link]} by {[Link]} - Copies: {[Link]}")
def borrow(self):
if [Link] > 0:
[Link] -= 1
print("Book borrowed successfully")
else:
print("No copies left to borrow")
# Test
book = Book("Python 101", "John Doe", 3)
[Link]()
[Link]()
[Link]()
```