← Back to list

Mastering Python: Day 7 — Building a Contact Manager with Object-Oriented Programming (OOP) Concept

Welcome to the final day of your Python learning journey! Today, we’ll dive into the fascinating world of Object-Oriented Programming…

Risky Mulya Nugraha · 2023-09-03 00:27 · 0 claps · 1.9 min read
#python #pythonpro #python-fund #python-for-beginners #python-course-online
Open on Medium ↗
Wiki topics: EDU · Education & Learning 💻 · Programming

Mastering Python: Day 7 — Building a Contact Manager with Object-Oriented Programming (OOP) Concept

Welcome to the final day of your Python learning journey! Today, we’ll dive into the fascinating world of Object-Oriented Programming (OOP). OOP is a powerful programming paradigm that allows you to model real-world concepts using classes and objects.

Part 1: Basics of OOP

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of “objects.” These objects have attributes (data) and methods (functions) associated with them. Here are some key points:

  • Classes: In the code below, classes act as blueprints for creating objects, defining their structure and behavior. For example, the Animal class serves as a template for animals and includes a speak() method to represent their vocalization.
  • Objects: Objects, shown in the code as dog and cat, are instances of classes. They embody specific real-world entities with unique attributes and behaviors. When they speak, dog produces "Woof!" and cat produces "Meow!", showcasing their distinct characteristics.
  • Encapsulation: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on that data into a single unit, called a class. It helps protect data from unauthorized access.

Part 2: Creating Classes and Objects

Now, let’s get our hands dirty by creating our own classes and objects. We’ll explore two essential OOP concepts:

  • Inheritance: Inheritance allows a new class (subclass or derived class) to inherit properties and methods from an existing class (base class or parent class). This promotes code reusability.
  • Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables flexibility and dynamic behavior in your code.

Here’s a quick Python example:

class Animal:
    def speak(self):
        pass  # Base class, no implementation

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

# Creating instances of Dog and Cat
dog = Dog()
cat = Cat()

print(dog.speak())  # Output: Woof!
print(cat.speak())  # Output: Meow!

In this code, we showcase how inheritance allows the Dog and Cat classes to inherit the speak() method from the base Animal class, enabling them to express their unique sounds.

Part 3: Building a Contact Manager

To put your OOP knowledge into practice, we’re embarking on a small project: creating a contact manager using OOP principles. This project provides hands-on experience in designing and implementing classes, objects, and their interactions.

Here’s a simplified version of a contact manager:

class Contact:
    def __init__(self, name, email):
        self.name = name
        self.email = email

class ContactManager:
    def __init__(self):
        self.contacts = []

    def add_contact(self, contact):
        self.contacts.append(contact)

    def view_contacts(self):
        for contact in self.contacts:
            print(f"Name: {contact.name}, Email: {contact.email}")

# Create a contact manager and add contacts
contact_manager = ContactManager()
contact1 = Contact("Bella Hadid", "bella@example.com")
contact2 = Contact("Allie Johnson", "alie@example.com")

contact_manager.add_contact(contact1)
contact_manager.add_contact(contact2)

# View contacts
contact_manager.view_contacts()

Embrace the chance to refine your OOP skills by crafting a functional contact manager. This project is a hands-on opportunity to dive into Python’s OOP world, where you’ll create an efficient and well-structured contact manager. Enjoy the journey and congrats on completing this Python learning series! completing this Python learning series!


메타데이터
post_id
2d66d43dcfae
slug
mastering-python-day-7-building-a-contact-manager-with-object-oriented-programming-oop-concept-2d66d43dcfae
url
https://medium.com/@rizmulya/mastering-python-day-7-building-a-contact-manager-with-object-oriented-programming-oop-concept-2d66d43dcfae
canonical_url
https://medium.com/@rizmulya/mastering-python-day-7-building-a-contact-manager-with-object-oriented-programming-oop-concept-2d66d43dcfae
author_url
https://medium.com/@rizmulya
status
ok
fetched_at
2026-08-05 00:11:00