Python Class - Exercises, Practice, Solution
This resource offers a total of 140 Python class problems for practice. It includes 28 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
The basic idea behind an object-oriented programming (OOP) is to combine both data and associated procedures (known as methods) into a single unit which operate on the data. Such a unit is called an object.
List of Exercises:
- Python class, Basic exercises [12 exercises with solution]
- Python class, Basic application [12 exercises with solution]
- Python class, Real-life problems [4 exercises with solution]
You may read our Python classes tutorial before solving the following exercises.
[An Editor is available at the bottom of the page to write and execute the scripts.]
Python class, Basic exercises [12 exercises with solution]
1. Import Built-in Array Module and Display Namespace
Write a Python program to import a built-in array module and display the namespace of the said module.
2. Create a Class and Display Its Namespace
Write a Python program to create a class and display the namespace of that class.
3. Create an instance of a class and display its namespace
Write a Python program to create an instance of a specified class and display the namespace of the said instance.
4. Access builtins module: Import abs(), show docs & absolute value
'builtins' module provides direct access to all 'built-in' identifiers of Python.
Write a Python program that imports the abs() function using the builtins module, displays the documentation of the abs() function and finds the absolute value of -155.
5. Define function student() and show argument names using attributes
Define a Python function student(). Using function attributes display the names of all arguments.
6. Define function student_data() to print ID, name, and class if given
Write a Python function student_data () that will print the ID of a student (student_id). If the user passes an argument student_name or student_class the function will print the student name and class.
7. Create class Student and display its type, __dict__ keys, and module
Write a simple Python class named Student and display its type. Also, display the __dict__ attribute keys and the value of the __module__ attribute of the Student class.
8. Create empty classes Student and Marks, instantiate them, and check instance and subclass relationships
Write a Python program to create two empty classes, Student and Marks. Now create some instances and check whether they are instances of the said classes or not. Also, check whether the said classes are subclasses of the built-in object class or not.
9. Create a Class Named Student with Attributes; Modify and Print Original and Modified Values
Write a Python class named Student with two attributes student_name, marks. Modify the attribute values of the said class and print the original and modified values of the said attributes.
10. Create a Class Named Student with Attributes; Add and Remove an Attribute and Display the Attributes
Write a Python class named Student with two attributes student_id, student_name. Add a new attribute student_class and display the entire attribute and the values of the class. Now remove the student_name attribute and display the entire attribute with values.
11. Create a Class Named Student with Attributes and a Function to Display All Attributes and Values
Write a Python class named Student with two attributes: student_id, student_name. Add a new attribute: student_class. Create a function to display all attributes and their values in the Student class.
12. Create a Class Named Student, Instantiate Two Instances, and Print Their Attributes
Write a Python class named Student with two instances student1, student2 and assign values to the instances' attributes. Print all the attributes of the student1, student2 instances with their values in the given format.
Python class, Basic application [12 exercises with solution]
1. Convert an Integer to a Roman Numeral
Write a Python class to convert an integer to a Roman numeral.
2. Convert a Roman Numeral to an Integer
Write a Python class to convert a Roman numeral to an integer.
3. Check Validity of a String of Parentheses
Write a Python class to check the validity of a string of parentheses, '(', ')', '{', '}', '[' and ']. These brackets must be closed in the correct order, for example "()" and "()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid.
4. Get All Unique Subsets from a Set of Distinct Integers
Write a Python class to get all possible unique subsets from a set of distinct integers.
Input : [4, 5, 6]
Output : [[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]
5. Find a Pair of Elements Whose Sum Equals a Specific Target
Write a Python class to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.
Note: There will be one solution for each input and do not use the same element twice.
Input: numbers= [10,20,10,40,50,60,70], target=50
Output: 3, 4
Difficulty: Medium. Company: Google, Facebook
6. Find Three Elements that Sum to Zero
Write a Python class to find the three elements that sum to zero from a set of n real numbers.
Input array : [-25, -10, -7, -3, 2, 4, 8, 10]
Output : [[-10, 2, 8], [-7, -3, 10]]
7. Implement pow(x, n)
Write a Python class to implement pow(x, n).
8. Reverse a String Word by Word
Write a Python class to reverse a string word by word.
Input string : 'hello .py'
Expected Output : '.py hello'
9. Class with get_String and print_String Methods
Write a Python class that has two methods: get_String and print_String , get_String accept a string from the user and print_String prints the string in upper case.
10. Class Named Rectangle to Compute Area
Write a Python class named Rectangle constructed from length and width and a method that will compute the area of a rectangle.
11. Class Named Circle to Compute Area and Perimeter
Write a Python class named Circle constructed from a radius and two methods that will compute the area and the perimeter of a circle.
12. Get the Class Name of an Instance
Write a Python program to get the class name of an instance in Python.
Python class, Real-life problems [4 exercises with solution]
1. Employee Class with Salary, Department, and Overtime Calculation
Write a Python class Employee with attributes like emp_id, emp_name, emp_salary, and emp_department and methods like calculate_emp_salary, emp_assign_department, and print_employee_details.
Sample Employee Data:
"ADAMS", "E7876", 50000, "ACCOUNTING"
"JONES", "E7499", 45000, "RESEARCH"
"MARTIN", "E7900", 50000, "SALES"
"SMITH", "E7698", 55000, "OPERATIONS"
- Use 'assign_department' method to change the department of an employee.
- Use 'print_employee_details' method to print the details of an employee.
- Use 'calculate_emp_salary' method takes two arguments: salary and hours_worked, which is the number of hours worked by the employee. If the number of hours worked is more than 50, the method computes overtime and adds it to the salary. Overtime is calculated as following formula:
overtime = hours_worked - 50
Overtime amount = (overtime * (salary / 50))
2. Restaurant Class with Menu, Table Reservation, and Order Management
Write a Python class Restaurant with attributes like menu_items, book_table, and customer_orders, and methods like add_item_to_menu, book_tables, and customer_order.
Perform the following tasks now:
- Now add items to the menu.
- Make table reservations.
- Take customer orders.
- Print the menu.
- Print table reservations.
- Print customer orders.
Note: Use dictionaries and lists to store the data.
3. BankAccount Class with Deposit, Withdrawal, and Transaction History
Write a Python class BankAccount with attributes like account_number, balance, date_of_opening and customer_name, and methods like deposit, withdraw, and check_balance.
4. Inventory Class with Item Management and Reporting
Write a Python class Inventory with attributes like item_id, item_name, stock_count, and price, and methods like add_item, update_item, and check_item_details.
Use a dictionary to store the item details, where the key is the item_id and the value is a dictionary containing the item_name, stock_count, and price.
Python Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz