Python Exam 5: Classes, Objects, and File Handling (10 mixed exercises)
Python Exam 5: OOP and Files
Test your skills in building solid class structures, using inheritance, and reading/writing files.
Exercise 1
Create a Class
Create a simple class:
- Define a class named `MyClass`.
- It contains a property `x = 5`.
- Create an object `p1` and print `x`.
Solution
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
Exercise 2
__init__ Method
Use a constructor:
- Define a `Person` class with an `__init__` method.
- The method takes `name` and `age`.
- Create an object named "John" with age 36.
- Print the name and age.
Solution
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Exercise 3
Object Methods
Add behavior to a class:
- In the `Person` class, add a `myfunc` method.
- The method prints "Hello my name is " followed by the name.
- Call the method from object `p1`.
Solution
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Exercise 4
The `self` Parameter
Understand the `self` parameter:
- Must the first parameter always be named `self`?
- Try renaming it to `mysillyobject` in `myfunc` and confirm it works.
Solution
# Yes, you can rename it, but it must be the first parameter.
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Exercise 5
Inheritance
Create a child class:
- Define a `Student` class that inherits from `Person`.
- Do not add any new properties (use `pass`).
- Create a `Student` object and use `Person` properties.
Solution
class Student(Person):
pass
x = Student("Mike", 24)
x.myfunc()
Exercise 6
Add Child Properties
Customize the child class:
- In `Student`, add an `__init__` method.
- Use `super().__init__(name, age)` to keep the parent behavior.
- Add a new property `graduationyear`.
Solution
class Student(Person):
def __init__(self, name, age, year):
super().__init__(name, age)
self.graduationyear = year
x = Student("Mike", 24, 2019)
print(x.graduationyear)
Exercise 7
Iterators
Create an iterator:
- You have a tuple `mytuple = ("apple", "banana", "cherry")`.
- Get the iterator using `iter()`.
- Print the next item using `next()`.
Solution
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
Exercise 8
File Open
Read a text file:
- Open `demofile.txt` for reading (`"r"`).
- Print its contents using `read()`.
Solution
f = open("demofile.txt", "r")
print(f.read())
Exercise 9
File Write
Append content to a file:
- Open `demofile2.txt` in append mode (`"a"`).
- Write "Now the file has more content!".
- Close the file.
Solution
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
Exercise 10
Delete File
Delete a file from the system:
- Import the `os` module.
- Check if `demofile.txt` exists.
- If it exists, delete it with `os.remove()`.
Solution
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")