15 Basic Python Programs on Relationships
1. Simple Parent-Child Relationship using Class Inheritance
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
[Link]()
[Link]()
2. Has-a Relationship (Composition)
class Engine:
def start(self):
print("Engine started")
class Car:
def __init__(self):
[Link] = Engine()
def drive(self):
[Link]()
print("Car is moving")
c = Car()
[Link]()
3. Aggregation Relationship
class Department:
def __init__(self, name):
[Link] = name
class Student:
def __init__(self, name, dept):
[Link] = name
[Link] = dept
d = Department("CSE")
s = Student("Ravi", d)
print([Link], "belongs to", [Link])
4. One-to-Many Relationship
class Teacher:
def __init__(self, name):
15 Basic Python Programs on Relationships
[Link] = name
[Link] = []
def add_subject(self, subject):
[Link](subject)
t = Teacher("Mr. Kumar")
t.add_subject("Math")
t.add_subject("Science")
print([Link], "teaches", [Link])
5. Many-to-One Relationship
class Company:
def __init__(self, name):
[Link] = name
class Employee:
def __init__(self, emp_name, company):
self.emp_name = emp_name
[Link] = company
c = Company("Infosys")
e1 = Employee("John", c)
e2 = Employee("Alice", c)
print(e1.emp_name, "works at", [Link])
print(e2.emp_name, "works at", [Link])
6. Friendship Relationship (List of Friends)
class Person:
def __init__(self, name):
[Link] = name
[Link] = []
def add_friend(self, friend):
[Link](friend)
p1 = Person("Asha")
p2 = Person("Kiran")
p1.add_friend([Link])
print([Link], "'s friends are", [Link])
7. Employee and Manager (Inheritance)
class Employee:
def __init__(self, name):
15 Basic Python Programs on Relationships
[Link] = name
class Manager(Employee):
def assign_task(self):
print([Link], "is assigning tasks")
m = Manager("Manoj")
m.assign_task()
8. Student and Course Relationship (Many-to-Many)
class Student:
def __init__(self, name):
[Link] = name
[Link] = []
class Course:
def __init__(self, title):
[Link] = title
[Link] = []
s1 = Student("Meena")
s2 = Student("Raj")
c1 = Course("Python")
[Link]([Link])
[Link]([Link])
[Link]([Link])
[Link]([Link])
print("Course:", [Link], "has students", [Link])
9. Nested Composition
class Address:
def __init__(self, city):
[Link] = city
class Person:
def __init__(self, name, address):
[Link] = name
[Link] = address
a = Address("Chennai")
p = Person("Ravi", a)
print([Link], "lives in", [Link])
10. Department has Multiple Professors (List of Objects)
class Professor:
15 Basic Python Programs on Relationships
def __init__(self, name):
[Link] = name
class Department:
def __init__(self):
[Link] = []
def add_professor(self, prof):
[Link](prof)
d = Department()
d.add_professor(Professor("Dr. Smith"))
d.add_professor(Professor("Dr. Jones"))
for p in [Link]:
print("Professor:", [Link])
11. Books and Authors (Bi-directional Relationship)
class Author:
def __init__(self, name):
[Link] = name
class Book:
def __init__(self, title, author):
[Link] = title
[Link] = author
a = Author("Chetan Bhagat")
b = Book("Five Point Someone", a)
print([Link], "written by", [Link])
12. Laptop Has Configuration (Composition)
class Configuration:
def __init__(self, ram, storage):
[Link] = ram
[Link] = storage
class Laptop:
def __init__(self, brand, config):
[Link] = brand
[Link] = config
c = Configuration("8GB", "512GB SSD")
l = Laptop("Dell", c)
print([Link], "has", [Link], "RAM and", [Link])
15 Basic Python Programs on Relationships
13. Order Contains Items (List of Objects)
class Item:
def __init__(self, name, price):
[Link] = name
[Link] = price
class Order:
def __init__(self):
[Link] = []
def add_item(self, item):
[Link](item)
o = Order()
o.add_item(Item("Pen", 10))
o.add_item(Item("Book", 50))
for i in [Link]:
print([Link], "-", [Link])
14. Person with Pets (One-to-Many)
class Pet:
def __init__(self, name):
[Link] = name
class Person:
def __init__(self, name):
[Link] = name
[Link] = []
def add_pet(self, pet):
[Link](pet)
p = Person("Sita")
p.add_pet(Pet("Tommy"))
p.add_pet(Pet("Kitty"))
for pet in [Link]:
print([Link], "has a pet named", [Link])
15. Project and Team Members (List Relationship)
class Member:
def __init__(self, name):
[Link] = name
class Project:
def __init__(self, title):
15 Basic Python Programs on Relationships
[Link] = title
[Link] = []
def add_member(self, member):
[Link](member)
proj = Project("AI App")
proj.add_member(Member("Arun"))
proj.add_member(Member("Latha"))
for m in [Link]:
print([Link], "is part of", [Link])