0% found this document useful (0 votes)
662 views

Python Excercises For Intermediate

The document contains 14 exercises related to object-oriented programming in Python. The exercises cover topics like creating classes with attributes and methods, inheritance between classes, checking object types, defining class attributes, overriding methods in child classes, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
662 views

Python Excercises For Intermediate

The document contains 14 exercises related to object-oriented programming in Python. The exercises cover topics like creating classes with attributes and methods, inheritance between classes, checking object types, defining class attributes, overriding methods in child classes, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

OOP Exercise 1: Create a Vehicle class with max_speed and mileage instance

attributes.

OOP Exercise 2: Create a Vehicle class without any variables and methods

OOP Exercise 3: Create a child class Bus that will inherit all of the variables and
methods of the Vehicle class.

OOP Exercise 4: Class Inheritance. Create a Bus class that inherits from the
Vehicle class. Give the capacity argument of Bus.seating_capacity() a default value
of 50.

OOP Exercise 5: Define property that should have the same value for every class
instance
Define a class attribute”color” with a default value white. I.e., Every Vehicle
should be white.

OOP Exercise 6: Class Inheritance


Given:
Create a Bus child class that inherits from the Vehicle class. The default fare
charge of any vehicle is seating capacity * 100. If Vehicle is Bus instance, we
need to add an extra 10% on full fare as a maintenance charge. So total fare for
bus instance will become the final amount = total fare + 10% of the total fare.

Note: The bus seating capacity is 50. so the final fare amount should be 5500. You
need to override the fare() method of a Vehicle class in Bus class.

OOP Exercise 7: Determine which class a given Bus object belongs to (Check type of
an object)

OOP Exercise 8: Determine if School_bus is also an instance of the Vehicle class

OOP Exercise 9: 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.

OOP Exercise 10: 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.

OOP Exercise 11:Write a Python class to convert an integer to a roman numeral.

OOP Exercise 12: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.

OOP Exercise 13: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]]

OOP Exercise 14: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

OOP Exercise 15:Write a Python class to implement pow(x, n).

OOP Exercise 16:Write a Python class to reverse a string word by word.


Input string : 'hello .py'
Expected Output : '.py hello'

#### DATE TIME EXCERCISES

Exercise 1: Print current date and time in Python

Exercise 2: Convert string into a datetime object

Exercise 3: Subtract a week (7 days) from a given date in Python


Given:
given_date = datetime(2020, 2, 25)

Expected output:
2020-02-18

Exercise 4: Print a date in a the following format


Day_name Day_number Month_name Year

Exercise 5: Find the day of the week of a given date


Given:

given_date = datetime(2020, 7, 26)

Exercise 6: Add a week (7 days) and 12 hours to a given date


Given:

# 2020-03-22 10:00:00
given_date = datetime(2020, 3, 22, 10, 0, 0)

Exercise 7: Print current time in milliseconds

Exercise 8: Convert the following datetime into a string


Given:

given_date = datetime(2020, 2, 25)

Exercise 9: Calculate the date 4 months from the current date


Given:

# 2020-02-25
given_date = datetime(2020, 2, 25).date()

Exercise 10: Calculate number of days between two given dates


Given:

# 2020-02-25
date_1 = datetime(2020, 2, 25)
# 2020-09-17
date_2 = datetime(2020, 9, 17)

You might also like