Course Management and Data Comparison

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to create and manage course information, including creating course instances, storing them in a dictionary, making a backup of the data, updating the course information, and comparing the updated information with the previous data.

👀 Preview

Linux - New Data
COURSE Linux
Lab count: 20
Public: True
Price: 199
Linux - Old Data
COURSE Linux
Lab count: 10
Public: True
Price: 99
---------------------------
Python - New Data
COURSE Python
Lab count: 10
Public: True
Price: 99
Python - Old Data
COURSE Python
Lab count: 7
Public: True
Price: 79
---------------------------

🎯 Tasks

In this project, you will learn:

  • How to create Course and BootcampCourse classes
  • How to create instances of BootcampCourse and store them in a dictionary
  • How to make a backup of the course information
  • How to update the number of labs and the price for each course
  • How to compare the updated course information with the previously saved data

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of inheritance and how to create subclasses in Python
  • Implement methods to update and compare course information
  • Utilize dictionaries to store and manage course data
  • Make backups of data and compare updated information with previous records

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/with_statement -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/variables_data_types -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/booleans -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/conditional_statements -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/for_loops -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/lists -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/tuples -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/dictionaries -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/function_definition -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/importing_modules -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/classes_objects -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/constructor -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/polymorphism -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/encapsulation -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/data_collections -.-> lab-302713{{"`Course Management and Data Comparison`"}} python/build_in_functions -.-> lab-302713{{"`Course Management and Data Comparison`"}} end

Create Course and BootcampCourse Classes

In this step, you will learn how to create the Course and BootcampCourse classes.

  1. Open the course.py file located in the /home/labex/project directory.

  2. In the Course class:

    • Define the __init__ method that takes in the course name, whether it is public or not, and the number of labs.
    • Implement the count property to return the number of labs.
    • Implement the typename method to return the object type.
    • Implement the info method to print the course name and the typename.
    • Implement the is_public method to return the public status of the course.
    class Course:
     OBJECT_TYPE = "COURSE"
    
     def __init__(self, name, public, lab_count):
         self.name = name
         self.public = public
         self.lab_count = lab_count
    
     @property
     def count(self):
         return self.lab_count
    
     def typename(self):
         return self.OBJECT_TYPE
    
     def info(self):
         print(self.typename(), self.name)
    
     def is_public(self):
         return self.public
  3. In the BootcampCourse class:

    • Define the __init__ method that takes in the course name, the number of labs, and the price. Call the __init__ method of the Course class and set the public status to True.
    • Implement the update method that takes in the new number of labs and the new price, and updates the corresponding attributes.
    • Implement the info method that calls the info method of the Course class and then prints the number of labs, the public status, and the price.
    class BootcampCourse(Course):
     def __init__(self, name, lab_count, price):
         super().__init__(name, True, lab_count)
         self.price = price
    
     def update(self, lab_count, price):
         self.lab_count = lab_count
         self.price = price
    
     def info(self):
         super().info()
         print("Lab count:", self.count)
         print("Public:", self.public)
         print("Price:", self.price)

Create and Manage Courses

In this step, you will learn how to create instances of BootcampCourse, store them in a dictionary, and make a backup of the course information.

  1. In the if __name__ == "__main__": block:

    • Create two instances of BootcampCourse: linux_course and python_course.
    • Create a dictionary called course_dict and store the two course instances in it, using the course names as keys.
    • Create a backup of the course_dict by making a deep copy of it and storing it in backup_course_dict.
    if __name__ == "__main__":
       linux_course = BootcampCourse("Linux", 10, 99)
       python_course = BootcampCourse("Python", 7, 79)
    
       ## build course dict
       course_dict = {"Linux": linux_course, "Python": python_course}
    
       ## copy and backup course dict
       backup_course_dict = copy.deepcopy(course_dict)

Update Course Information

In this step, you will learn how to update the number of labs and the price for each course.

  1. In the if __name__ == "__main__": block:

    • Update the number of labs and the price for the linux_course using the update method.
    • Update the number of labs and the price for the python_course using the update method.
    ## update lab count and price
    linux_course.update(20, 199)
    python_course.update(10, 99)

Compare Course Information

In this step, you will learn how to compare the updated course information with the previously saved course information.

  1. In the if __name__ == "__main__": block:

    1. Iterate through the keys in the course_dict (which are "Linux" and "Python").
    2. For each key:
    • Print the course name and the label "- New Data".
    • Call the info method on the corresponding course instance in the course_dict.
    • Print the course name and the label "- Old Data".
    • Call the info method on the corresponding course instance in the backup_course_dict.
    • Print a separator line.
## compare with backup
for name in ("Linux", "Python"):
    print(name, "- New Data")
    course_dict[name].info()
    print(name, "- Old Data")
    backup_course_dict[name].info()
    print("---------------------------")

Run the course.py file to see the output.

python3 course.py

Output:

Linux - New Data
COURSE Linux
Lab count: 20
Public: True
Price: 199
Linux - Old Data
COURSE Linux
Lab count: 10
Public: True
Price: 99
---------------------------
Python - New Data
COURSE Python
Lab count: 10
Public: True
Price: 99
Python - Old Data
COURSE Python
Lab count: 7
Public: True
Price: 79
---------------------------

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like