Object Oriented Programming (Simple Activities)
Object Oriented Programming (Simple Activities)
Object Oriented Programming (Simple Activities)
PYTHON’S OBJECT-ORIENTED
PROGRAMMING
LEARNING EXPECTATIONS
✓ Students must understand how to construct and use object and class in Python.
✓ Students must understand how the different important parts (attributes, method,
etc.) in creating a class in Python.
✓ Students must know what and how to create a class out of another class in
Python.
EXERCISE 3.1
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.
Use the following code for your parent Vehicle class. We need to access the
parent class from inside a method of a child class.
Use the following code for your parent Vehicle class. You need to use method overriding.
import random
class Player:
def __init__(self, name):
self.name = name
self.choice = None
self.score = 0
def add_and_update_score(self):
self.score += 1
def rock_paper_scissors():
human_player = Player(input('Enter name of human: '))
computer_player = Player(input('Enter name of computer: '))
if human_player.choice == computer_player.choice:
pass
elif player_choices in human_wins:
human_player.add_and_update_score()
else:
computer_player.add_and_update_score()
print(f'{human_player.name}: {human_player.score}
{computer_player.name}: {computer_player.score}')
if human_player.score == 3:
print(f'{human_player.name.upper()} WINS')
else:
print(f'{computer_player.name.upper()} WINS')
if __name__ == '__main__':
rock_paper_scissors()
ANSWER THE FOLLOWING QUESTIONS
• Define object and class in Python.
Pythons Object Oriented Programming (OOP) plays a role, in the language’s success and
broad acceptance in the programming world. It is a paradigm that emphasizes structuring code,
into objects which are simply instances of classes. These objects encompass both data and
behavior contributing to Pythons adaptability and ability to express ideas effectively.
The Python OOP has four basic principles for writing clean and concise code. These are
Abstraction, Encapsulation, Inheritance, and Polymorphism. First, Abstraction is the process of
shielding the user from the code's inner workings. This keeps the code simple and makes sure
we just pay attention to what matters. In Python’s OOP, abstraction is accomplished by
developing implementation classes (subclasses) and interface classes (base classes). Then,
Encapsulation is the process of enclosing data and its processes in a 'capsule' or unit so that it
cannot be accessed or changed outside of that unit, making the data private. Making variables
inside of a class private does this. Furthermore, Inheritance is what helps in writing a reusable
and more concise code. A parent class and a child class exist in inheritance. The methods and
properties of the parent class are inherited by the child class. This feature skyrockets the code’s
reusability. Lastly, simply put, polymorphism means "many forms." This means that one function
or object in Python can be utilized in a variety of ways or data types. In OOP, polymorphism may
be implemented with inheritance through method overriding. Polymorphism is useful if
modification is needed for the parent method to fit the needs of the child class while still allowing
access to the parent class method.
[2] Vishal, “Classes and Objects in Python,” PYnative, Aug. 25, 2021.
https://pynative.com/python-classes-and-objects/
[6] “Object Oriented Programming in Pyth: Class and Instance Variables,” DigitalOcean.
https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-
in-python-3
[7] “Class Variables vs Instance Variables in Python,” Atatus Blog - For DevOps Engineers,
Web App Developers and Server Admins., Feb. 17, 2023. https://www.atatus.com/blog/class-
variables-vs-instance-variables-in-java/
[11] R. Python, “Inheritance and Composition: A Python OOP Guide – Real Python,”
realpython.com. https://realpython.com/inheritance-composition-python/
[14] R. Python, “Python Class Constructors: Control Your Object Instantiation – Real
Python,” realpython.com. https://realpython.com/python-class-constructor/
[15] “What does instantiate mean in the context of this lesson?,” Codecademy Forums, Jan.
08, 2020. https://discuss.codecademy.com/t/what-does-instantiate-mean-in-the-context-of-this-
lesson/465216 (accessed Oct. 19, 2023).
[16] “What is instantiate in Python?,” Quora, 2019. https://www.quora.com/What-is-
instantiate-in-Python (accessed Oct. 19, 2023).