Python Class - Exercises, Practice, Solution
Python class [24 exercises with solution]
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.
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. Write a Python program to import built-in array module and display the namespace of the said module. Go to the editor
2. Write a Python program to create a class and display the namespace of the said class. Go to the editor
3. Write a Python program to create an instance of a specified class and display the namespace of the said instance. Go to the editor
4. 'builtins' module provides direct access to all 'built-in' identifiers of Python.
Write a python program which import the abs() function using the builtins module, display the documentation of abs() function and find the absolute value of -155. Go to the editor
5. Define a Python function student(). Using function attributes display the names of all arguments. Go to the editor
6. Write a Python function student_data () which 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. Go to the editor
7. 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. Go to the editor
8. Write a Python program to crate 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. Go to the editor
9. 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. Go to the editor
10. 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 their values of the said class. Now remove the student_name attribute and display the entire attribute with values. Go to the editor
11. Write a Python class named Student with two attributes student_id, student_name. Add a new attribute student_class. Create a function to display the entire attribute and their values in Student class. Go to the editor
12. Write a Python class named Student with two instances student1, student2 and assign given values to the said instances attributes. Print all the attributes of student1, student2 instances with their values in the given format. Go to the editor
Python class, Basic application [12 exercises with solution]
1. Write a Python class to convert an integer to a roman numeral. Go to the editor
2. Write a Python class to convert a roman numeral to an integer. Go to the editor
3. Write a Python class to find validity of a string of parentheses, '(', ')', '{', '}', '[' and ']. These brackets must be close in the correct order, for example "()" and "()[]{}" are valid but "[)", "({[)]" and "{{{" are invalid. Go to the editor
4. Write a Python class to get all possible unique subsets from a set of distinct integers. Go to the editor
Input : [4, 5, 6]
Output : [[], [6], [5], [5, 6], [4], [4, 6], [4, 5], [4, 5, 6]]
5. 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. Go to the editor
Input: numbers= [10,20,10,40,50,60,70], target=50
Output: 3, 4
Difficulty: Medium. Company: Google, Facebook
6. Write a Python class to find the three elements that sum to zero from a set of n real numbers. Go to the editor
Input array : [-25, -10, -7, -3, 2, 4, 8, 10]
Output : [[-10, 2, 8], [-7, -3, 10]]
7. Write a Python class to implement pow(x, n). Go to the editor
8. Write a Python class to reverse a string word by word.
Go to the editor
Input string : 'hello .py'
Expected Output : '.py hello'
9. Write a Python class which has two methods get_String and print_String. get_String accept a string from the user and print_String print the string in upper case. Go to the editor
Click me to see the solution
10. Write a Python class named Rectangle constructed by a length and width and a method which will compute the area of a rectangle. Go to the editor
Click me to see the solution
11. Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle. Go to the editor
Click me to see the solution
Click me to see the solution
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
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework