Python Class - Exercises, Practice, Solution
Python class [12 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts.]
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. Go to the editor
Input: numbers= [10,20,10,40,50,60,70], target=50
Output: 3, 4
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
Python: Time it
timeit library is great for timing Python execution times. Just pass a function in string format to timeit.timeit() method and it will carry out 1 million executions to report the minimum time the code takes.
Its very useful to compare small code pieces and different functions but can be sluggish with big code.
Check out the example below demonstrating the execution time difference between 2 very similar list comprehension methods in Python:
import timeit lst1='''list(range(100))''' lst2='''[i for i in range(100)]''' a=timeit.timeit(lst1) b=timeit.timeit(lst2) print(a, b, sep="------")
Output:
1.9301698543131351------5.389458132907748
- New Content published on w3resource:
- 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
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework