0% found this document useful (0 votes)
20 views5 pages

OOP Exercises for Python Beginners

The document outlines exercises for beginners to practice the four pillars of Object-Oriented Programming (OOP) in Python 3. It includes tasks related to classes and objects, attributes and methods, abstraction and encapsulation, inheritance, and polymorphism. Each exercise provides specific requirements for creating programs that demonstrate these OOP concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

OOP Exercises for Python Beginners

The document outlines exercises for beginners to practice the four pillars of Object-Oriented Programming (OOP) in Python 3. It includes tasks related to classes and objects, attributes and methods, abstraction and encapsulation, inheritance, and polymorphism. Each exercise provides specific requirements for creating programs that demonstrate these OOP concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Four Pillars of OOP in Python 3 for Beginners

EXERCISE - CLASSES AND OBJECTS

Write and object oriented program that performs the following


tasks:
1. Define a class called "Employee" and create an instance
of that class
2. Create an attribute called name and assign it with a
value
3. Change the name you previously defined within a
method and call this method by making use of the object you
created

Created by Febin George


The Four Pillars of OOP in Python 3 for Beginners

EXERCISE - ATTRIBUTES AND METHODS

Write an object oriented program to create a precious stone.


Not more than 5 precious stones can be held in possession at a
given point of time. If there are more than 5 precious stones,
delete the first stone and store the new one.

Created by Febin George


The Four Pillars of OOP in Python 3 for Beginners

EXERCISE - ABSTRACTION AND ENCAPSULATION

Similar to a library management system, write a program to


provide layers of abstraction for a car rental system.
Your program should perform the following:
1. Hatchback, Sedan, SUV should be type of cars that are
being provided for rent
2. Cost per day:
Hatchback - $30
Sedan - $50
SUV - $100
3. Give a prompt to the customer asking him the type of car
and the number of days he would like to borrow and provide the
fare details to the user.

Created by Febin George


The Four Pillars of OOP in Python 3 for Beginners

EXERCISE - INHERITANCE
Write an object oriented program that performs the following tasks:
1. Create a class called Chair from the base class Furniture
2. Teakwood should be the type of furniture that is used by all furnitures by
default
3. The user can be given an option to change the type of wood used for chair if
he wishes to
4. The number of legs of a chair should be a property that should not be altered
outside the class

Created by Febin George


EXERCISE - POLYMORPHISM

Create a class called Square and perform the following tasks -


(i) Create two objects for this class squareOne and
squareTwo
(ii) Find the result of side of squareOne to the power of side
of squareTwo

Example: If squareOne has length of 2cm each side and


squareTwo has a length of 4cm each side, squareOne **
squareTwo should return 16, which is 2 to the power of 4.

Hint: While performing SquareOne ** SquareTwo, you need to


overload __pow__() method

Common questions

Powered by AI

The strategy involves creating a program that limits the collection of precious stones to five, automatically removing the oldest stone to make space for a new one. This reflects encapsulation by managing the collection's state within a class and exposing only necessary operations to maintain integrity and confidentiality of the data .

Using a data structure that auto-deletes the first precious stone when a new one is added helps manage resources efficiently by maintaining a fixed-size collection. This reduces memory overhead and prevents resource leaks by ensuring the collection never exceeds its defined limit, promoting effective resource management in programs .

Effective user interaction can be managed by prompting users to select a car type and input rental days, and dynamically calculating costs based on predefined rates for each car type. The system can also include validation checks and error handling to ensure accurate inputs, providing real-time feedback and a seamless experience .

Inheritance is utilized by creating a Chair class that inherits properties and methods from a base class, Furniture. Teakwood is the default type shared by all furnitures, demonstrating code reusability and extension capabilities while allowing customization, such as changing the type of wood for the Chair class. This approach helps in organizing code into hierarchical structures, reducing redundancy, and improving maintainability .

Creating an Employee class demonstrates encapsulation by bundling the data, such as attributes like 'name,' and methods that operate on the data into a single unit, restricting unauthorized access and manipulation. Abstraction is shown by allowing users to interact with objects through defined interfaces, such as methods for changing attributes, without needing to understand the underlying implementation details .

The concept of method overloading in polymorphism is illustrated by the __pow__() method. By defining the __pow__() method in the Square class, different objects such as squareOne and squareTwo can interact with the same operation, 'power,' in a manner that suits their context. For example, squareOne with side 2 and squareTwo with side 4 use __pow__() to compute 2**4, resulting in 16, showcasing how polymorphism allows objects to use method operations fittingly .

Setting a default type like Teakwood demonstrates inheritance by establishing a standard property that all derived classes share, promoting code reuse. Allowing customization, such as changing wood type for specific instances like a Chair, enhances user flexibility by enabling modifications. This blend of default settings and customization creates a balance between uniformity across objects and specific user needs .

Abstraction in the car rental system simplifies user interaction by providing a high-level interface through which users can select car types and rental durations without dealing with implementation complexities. The program presents predefined types like Hatchback, Sedan, and SUV and calculates costs based on set rates, hiding the intricate operations from the users .

The design principle applied is encapsulation, which restricts direct access to the 'number of legs' property by defining it within the class scope, making it immutable externally. This is important to maintain object consistency and integrity, preventing unauthorized or unintended modifications that could compromise the object's intended behavior .

Encapsulation plays a crucial role in defining methods for changing attributes, such as 'name,' by bundling the attribute with methods that strictly control its access and modification. This ensures that the attribute is changed in a consistent and safe manner, preserving the object's integrity and protecting its state from unauthorized modifications .

You might also like