Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

OOPS (Object-Oriented Programming) Practice

Core OOP concepts: classes, objects, methods, attributes and design.

1
Create a Simple Class
Easy
Write a Python class Person with an attribute name. Create an object, set name to the input, and print it.
A single line containing a name.
Print the person's name.
Sample Input 1
Alice
Sample Output 1
Alice
Sample Input 2
Bob
Sample Output 2
Bob
Sample Input 3
Sam
Sample Output 3
Sam
2
Class with Greet Method
Easy
Write a class Person with a method greet() that prints 'Hello, [name]!'. Create an object and call greet().
A single line containing a name.
Print the greeting message.
Sample Input 1
Bob
Sample Output 1
Hello, Bob!
Sample Input 2
Alice
Sample Output 2
Hello, Alice!
Sample Input 3

                      
Sample Output 3
Hello, !
3
Store and Print Age
Easy
Write a class Student with attributes name and age. Read name and age, create an object, and print both.
First line: name. Second line: age.
Print 'Name: [name]' and 'Age: [age]' on separate lines.
Sample Input 1
Alice
20
Sample Output 1
Name: Alice
Age: 20
Sample Input 2
Bob
30
Sample Output 2
Name: Bob
Age: 30
Sample Input 3
Cara
18
Sample Output 3
Name: Cara
Age: 18
4
Class Method to Return Square
Easy
Write a class Calculator with a method square(n) that returns n*n. Create an object and print the square of the input.
A single line containing integer n.
Print the square.
Sample Input 1
7
Sample Output 1
49
Sample Input 2
8
Sample Output 2
64
Sample Input 3
0
Sample Output 3
0
5
Class with Multiple Methods
Easy
Write a class Rectangle with methods area() and perimeter() using length and breadth attributes.
A single line containing two space-separated integers length and breadth.
Print area on line 1 and perimeter on line 2.
Sample Input 1
4 6
Sample Output 1
24
20
Sample Input 2
2 3
Sample Output 2
6
10
Sample Input 3
1 1
Sample Output 3
1
4
6
Book Class with Title
Easy
Write a class Book with attribute title. Read a title, create an object, and print it.
A single line containing the book title.
Print the book title.
Sample Input 1
Python Basics
Sample Output 1
Python Basics
Sample Input 2
Learn Python
Sample Output 2
Learn Python
Sample Input 3
OOP
Sample Output 3
OOP
7
Circle Class — Area
Easy
Write a class Circle with radius attribute and a method area() that returns 3.14 * r * r.
A single line containing the radius r.
Print the area rounded to 2 decimal places.
Sample Input 1
5
Sample Output 1
78.50
Sample Input 2
10
Sample Output 2
314.00
Sample Input 3
1
Sample Output 3
3.14
8
Object Attribute Update
Easy
Write a class Car with attribute speed. Create an object with speed 0, read a new speed, update the attribute, and print it.
A single line containing the new speed.
Print the updated speed.
Sample Input 1
60
Sample Output 1
60
Sample Input 2
100
Sample Output 2
100
Sample Input 3
0
Sample Output 3
0
9
Two Objects of Same Class
Easy
Write a class Dog with method bark() that prints 'Woof!'. Create two objects and call bark() on both.
No input.
Print 'Woof!' twice, once per line.
Sample Input 1

                      
Sample Output 1
Woof!
Woof!
Sample Input 2

                      
Sample Output 2
Woof!
Woof!
Sample Input 3

                      
Sample Output 3
Woof!
Woof!
10
Class with Display Method
Easy
Write a class Employee with name and salary attributes and a display() method that prints both.
First line: name. Second line: salary.
Print 'Name: [name]' and 'Salary: [salary]' on separate lines.
Sample Input 1
Ravi
45000
Sample Output 1
Name: Ravi
Salary: 45000
Sample Input 2
Priya
50000
Sample Output 2
Name: Priya
Salary: 50000
Sample Input 3
Amit
30000
Sample Output 3
Name: Amit
Salary: 30000
1
Class with __str__ Method
Medium
Write a class Point with x and y attributes and a __str__ method that returns '(x, y)'. Read x and y and print the object.
A single line containing two space-separated integers x and y.
Print the point as (x, y).
Sample Input 1
3 4
Sample Output 1
(3, 4)
Sample Input 2
0 0
Sample Output 2
(0, 0)
Sample Input 3
5 -2
Sample Output 3
(5, -2)
2
Bank Account with Methods
Medium
Write a class BankAccount with methods deposit(amount) and get_balance(). Start with balance 0, perform a deposit, and print the balance.
A single line containing the deposit amount.
Print the balance after deposit.
Sample Input 1
500
Sample Output 1
500
Sample Input 2
1000
Sample Output 2
1000
Sample Input 3
0
Sample Output 3
0
3
Class with Class Variable
Medium
Write a class Employee with a class variable company = 'TechBridge'. Read an employee name, create an object, and print 'name works at TechBridge'.
A single line containing an employee name.
Print the sentence with the company name.
Sample Input 1
Alice
Sample Output 1
Alice works at TechBridge
Sample Input 2
Bob
Sample Output 2
Bob works at TechBridge
Sample Input 3

                      
Sample Output 3
works at TechBridge
4
Object Count Using Class Variable
Medium
Write a class Counter with a class variable count that increments every time an object is created. Create 3 objects and print the count.
No input.
Print the number of objects created.
Sample Input 1

                      
Sample Output 1
3
Sample Input 2

                      
Sample Output 2
3
Sample Input 3

                      
Sample Output 3
3
5
Instance vs Class Attributes
Medium
Write a class Student with class attribute school = 'Green Valley' and instance attribute name. Read a name, create an object, and print both.
A single line containing a name.
Print 'Name: [name]' and 'School: [school]' on separate lines.
Sample Input 1
Alice
Sample Output 1
Name: Alice
School: Green Valley
Sample Input 2
Bob
Sample Output 2
Name: Bob
School: Green Valley
Sample Input 3

                      
Sample Output 3
Name: 
School: Green Valley
6
Method Calling Another Method
Medium
Write a class Calculator with methods add(a, b) and add_then_double(a, b) that calls add() and doubles the result.
A single line containing two space-separated integers.
Print the doubled sum.
Sample Input 1
5 6
Sample Output 1
22
Sample Input 2
3 4
Sample Output 2
14
Sample Input 3
0 1
Sample Output 3
2
7
Simple Shopping Cart
Medium
Write a class Cart with an items list and methods add(item) and show(). Add 2 items from input and print the cart contents.
Two lines, each containing an item name.
Print the list of items.
Sample Input 1
apple
mango
Sample Output 1
['apple', 'mango']
Sample Input 2
tea
coffee
Sample Output 2
['tea', 'coffee']
Sample Input 3
juice
water
Sample Output 3
['juice', 'water']
8
Comparing Two Objects
Medium
Write a class Student with name and marks. Read two students' data, create objects, and print the name of the student with higher marks.
First line: name and marks of student 1. Second line: name and marks of student 2.
Print the name of the student with higher marks.
Sample Input 1
Alice 85
Bob 90
Sample Output 1
Bob
Sample Input 2
X 60
Y 80
Sample Output 2
Y
Sample Input 3
P 90
Q 85
Sample Output 3
P
9
Class Method for Greeting
Medium
Write a class Greeter with a static-like method greet(name) accessed via the class (classmethod) that returns 'Hello, [name]!'.
A single line containing a name.
Print the greeting.
Sample Input 1
Sam
Sample Output 1
Hello, Sam!
Sample Input 2
Tom
Sample Output 2
Hello, Tom!
Sample Input 3
Jerry
Sample Output 3
Hello, Jerry!
10
Product Class with Discount
Medium
Write a class Product with price and discount attributes and a method final_price() that returns price minus discount.
A single line containing price and discount space-separated.
Print the final price.
Sample Input 1
1000 150
Sample Output 1
850
Sample Input 2
500 50
Sample Output 2
450
Sample Input 3
2000 0
Sample Output 3
2000
1
Design a Library System
Hard
Write classes Book and Library. Library has a list of books and methods add_book(book) and show_books(). Add 3 books and print all titles.
Three lines, each containing a book title.
Print each book title on a new line.
Sample Input 1
Python
Java
C++
Sample Output 1
Python
Java
C++
Sample Input 2
A
B
C
Sample Output 2
A
B
C
Sample Input 3
X
Y
Z
Sample Output 3
X
Y
Z
2
Shape Hierarchy with Area
Hard
Write a base class Shape with method area() and derived classes Circle and Rectangle that override it. Read shape type and dimensions, and print the area.
First line: shape type ('circle' or 'rectangle'). Next lines contain the needed dimensions.
Print the area rounded to 2 decimal places.
Sample Input 1
circle
5
Sample Output 1
78.50
Sample Input 2
rectangle
4 6
Sample Output 2
24.00
Sample Input 3
circle
1
Sample Output 3
3.14
3
Student Report Card System
Hard
Write a class Student with marks list and methods add_mark(m), average(), and grade(). Add 3 marks and print average and grade.
A single line containing three space-separated marks.
Print 'Average: [avg]' and 'Grade: [grade]' on separate lines (A ≥ 90, B ≥ 75, C ≥ 60, else F).
Sample Input 1
80 90 85
Sample Output 1
Average: 85.0
Grade: B
Sample Input 2
50 60 70
Sample Output 2
Average: 60.0
Grade: C
Sample Input 3
95 98 99
Sample Output 3
Average: 97.33
Grade: A
4
Employee with Salary Raise
Hard
Write a class Employee with name, base salary, and a method apply_raise(percent) that updates the salary.
First line: name and salary. Second line: raise percent.
Print the updated salary.
Sample Input 1
Alice 40000
10
Sample Output 1
44000.0
Sample Input 2
Bob 30000
20
Sample Output 2
36000.0
Sample Input 3
Cara 50000
0
Sample Output 3
50000.0
5
Simple Banking with Withdrawal
Hard
Write a class BankAccount with balance and methods deposit(amount) and withdraw(amount) that print 'Insufficient funds' if balance is not enough.
First line: initial balance. Second line: deposit amount. Third line: withdrawal amount.
Print balance after deposit, then after withdrawal.
Sample Input 1
1000
500
1200
Sample Output 1
1500
Insufficient funds
1500
Sample Input 2
100
0
50
Sample Output 2
100
150
Sample Input 3
500
100
700
Sample Output 3
600
Insufficient funds
600
6
Inventory Management
Hard
Write a class Inventory with a dictionary of items and methods add(item, qty) and total_items() that sums all quantities.
Multiple lines: each line contains an item and quantity separated by space. End with empty line.
Print the total quantity of all items.
Sample Input 1
apple 10
banana 5
mango 8
Sample Output 1
23
Sample Input 2
a 1
b 2
Sample Output 2
3
Sample Input 3
x 5
x 5
Sample Output 3
10
7
OOP — Matrix Class
Hard
Write a class Matrix that stores a 2D list, and a method transpose() that returns the transposed matrix.
First line: rows and cols. Next rows lines: row values space-separated.
Print the transposed matrix, one row per line.
Sample Input 1
2 3
1 2 3
4 5 6
Sample Output 1
1 4
2 5
3 6
Sample Input 2
2 2
1 2
3 4
Sample Output 2
1 3
2 4
Sample Input 3
1 3
1 2 3
Sample Output 3
1
2
3
8
Vending Machine Simulation
Hard
Write a class VendingMachine with a dict of item prices and methods insert_money(amount), select(item), and balance().
First line: amount inserted. Second line: item name.
Print 'Dispensed: [item]' and 'Balance: [remaining]'.
Sample Input 1
50
chips
Sample Output 1
Dispensed: chips
Balance: 20
Sample Input 2
20
water
Sample Output 2
Dispensed: water
Balance: 5
Sample Input 3
100
soda
Sample Output 3
Dispensed: soda
Balance: 65
9
OOP — Fraction Class
Hard
Write a class Fraction with numerator and denominator and a method simplify() that reduces the fraction using GCD.
A single line containing numerator and denominator space-separated.
Print the simplified fraction as 'num/den'.
Sample Input 1
8 12
Sample Output 1
2/3
Sample Input 2
6 9
Sample Output 2
2/3
Sample Input 3
10 4
Sample Output 3
5/2
10
Hotel Room Booking System
Hard
Write a class Hotel with rooms dict and methods book(room) and available_rooms(). Book 2 rooms and print the remaining available rooms.
Two lines, each containing a room number.
Print the list of remaining available rooms.
Sample Input 1
101
102
Sample Output 1
[103, 104, 105]
Sample Input 2
201
202
Sample Output 2
[103, 104, 105]
Sample Input 3
103
Sample Output 3
[104, 105]