Introduction: This document presents learning steps for Python 09. In Python 09, you will get introduced with classes and objects in Python. This will be the first step in Object Oriented Programming in Python.
Note: In this phase, it is expected the learner can divide the program into smaller learning steps. The goal and direction of the topics will be provided. The learning must take smaller steps towards the goals such that can implement solutions to the given problems and product(s).
The activities are designed based on these following references:
- BRef-01: Book, Bill Lubanovic; "Introducing Python: Modern Computing in Simple Packages"; Available here
- ORef-01: Online Tutorial; Charles Severance; "Python for Everybody"; Available here
After taking this step, you will be able to:
1. interpret and implement Python programs with Python objects and classes: defining a class, instantiating an object, attributes and methods, initializing and object.
2. understand the concepts of namespace and scope in Python programs.
- Using BRef-01: Chapter 10 answer and experiment the following questions:
- What is an Object in Python?
- What is a class and how can you define a class in Python? Make an example.
- How can you create and use an object from a class? Experiment with examples.
- What are attributes? How they help programmers to encapsulate their data values?
- What is a method? Focus only on instance methods.
- How can you initialize an instance (object)? Implement a class with init method.
- Design at least ten different exercises of your own. They should improve understanding topics of this step. Share your exercises with your learning community and practice.
Problem: In a programming assignment, students are asked to implement a simple Python program where libraries can store books. Below, you can find a sample solution provided by one of the students.
- Without executing the code, read the code and try to predict what will be printed at the end.
- Run the code in your IDE. Does it print what you had predicted? If not, why?
- Debug the code carefully and fix the bug, if you find any.
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __repr__(self) -> str:
return f'Title: {self.title} , Author: {self.author}'
class Library:
def __init__(self, name):
self.name = name
self.books = []
def add_books(self, books):
self.books = books
def display_books(self):
print(f"Books in {self.name} Library:")
for book in self.books:
print(book)
def main():
# Create Books
book1 = Book('Introducing Python','Bill Lubanovic')
book2 = Book('The Python Workbook','Ben Stephenson')
book3 = Book('Learn Python Programming','Fabrizio Romano')
book4 = Book('Fleunt Python','Luciano Ramalho')
# Create libraries
univ_lib = Library('University')
city_lib = Library('City')
# Add books to the libraries
univ_lib.add_books([book1,book4])
city_lib.add_books([book1,book3])
univ_lib.add_books([book1,book2])
city_lib.add_books([book3,book4])
# Display books in libraries
univ_lib.display_books()
city_lib.display_books()
main()