Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions Lecture 02/Python Code/Abstraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from abc import ABC, abstractmethod

class Car(ABC):
"""
Car Interface --> Acts as an interface for the outside world to operate the car.
This interface tells 'WHAT' all it can do rather than 'HOW' it does that.
"""

@abstractmethod
def start_engine(self):
pass

@abstractmethod
def shift_gear(self, gear):
pass

@abstractmethod
def accelerate(self):
pass

@abstractmethod
def brake(self):
pass

@abstractmethod
def stop_engine(self):
pass


class SportsCar(Car):
"""
Concrete class that provides implementation details of the Car interface.
Represents a real-world sports car with all functionalities.
"""

def __init__(self, brand, model):
self.brand = brand
self.model = model
self.is_engine_on = False
self.current_speed = 0
self.current_gear = 0

def start_engine(self):
self.is_engine_on = True
print(f"{self.brand} {self.model} : Engine starts with a roar!")

def shift_gear(self, gear):
if not self.is_engine_on:
print(f"{self.brand} {self.model} : Engine is off! Cannot shift gear.")
return
self.current_gear = gear
print(f"{self.brand} {self.model} : Shifted to gear {self.current_gear}")

def accelerate(self):
if not self.is_engine_on:
print(f"{self.brand} {self.model} : Engine is off! Cannot accelerate.")
return
self.current_speed += 20
print(f"{self.brand} {self.model} : Accelerating to {self.current_speed} km/h")

def brake(self):
self.current_speed -= 20
if self.current_speed < 0:
self.current_speed = 0
print(f"{self.brand} {self.model} : Braking! Speed is now {self.current_speed} km/h")

def stop_engine(self):
self.is_engine_on = False
self.current_gear = 0
self.current_speed = 0
print(f"{self.brand} {self.model} : Engine turned off.")


# Main Method
if __name__ == "__main__":
my_car = SportsCar("Ford", "Mustang")

my_car.start_engine()
my_car.shift_gear(1)
my_car.accelerate()
my_car.shift_gear(2)
my_car.accelerate()
my_car.brake()
my_car.stop_engine()
74 changes: 74 additions & 0 deletions Lecture 02/Python Code/Encapsulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class SportsCar:
"""
SportsCar class demonstrates encapsulation by combining characteristics (attributes)
and behaviors (methods) into a single class and restricting access to certain attributes
using private variables and getter/setter methods.
"""

def __init__(self, brand, model):
self.__brand = brand
self.__model = model
self.__is_engine_on = False
self.__current_speed = 0
self.__current_gear = 0
self.__tyre_company = None # Introduced to explain setters

# Getter for current speed
def get_speed(self):
return self.__current_speed

# Getter for tyre company
def get_tyre_company(self):
return self.__tyre_company

# Setter for tyre company
def set_tyre_company(self, tyre_company):
self.__tyre_company = tyre_company

# Method to start the engine
def start_engine(self):
self.__is_engine_on = True
print(f"{self.__brand} {self.__model} : Engine starts with a roar!")

# Method to shift gear
def shift_gear(self, gear):
self.__current_gear = gear
print(f"{self.__brand} {self.__model} : Shifted to gear {self.__current_gear}")

# Method to accelerate
def accelerate(self):
if not self.__is_engine_on:
print(f"{self.__brand} {self.__model} : Engine is off! Cannot accelerate.")
return
self.__current_speed += 20
print(f"{self.__brand} {self.__model} : Accelerating to {self.__current_speed} km/h")

# Method to brake
def brake(self):
self.__current_speed -= 20
if self.__current_speed < 0:
self.__current_speed = 0
print(f"{self.__brand} {self.__model} : Braking! Speed is now {self.__current_speed} km/h")

# Method to stop the engine
def stop_engine(self):
self.__is_engine_on = False
self.__current_gear = 0
self.__current_speed = 0
print(f"{self.__brand} {self.__model} : Engine turned off.")


# Main Method
if __name__ == "__main__":
my_sports_car = SportsCar("Ford", "Mustang")

my_sports_car.start_engine()
my_sports_car.shift_gear(1)
my_sports_car.accelerate()
my_sports_car.shift_gear(2)
my_sports_car.accelerate()
my_sports_car.brake()
my_sports_car.stop_engine()

# Accessing speed using getter
print(f"Current Speed of My Sports Car is {my_sports_car.get_speed()}")
105 changes: 105 additions & 0 deletions Lecture 03/Python Code/DynamicPolymorphism.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from abc import ABC, abstractmethod

class Car(ABC):
"""
Abstract base class representing a generic car.
"""

def __init__(self, brand, model):
self.brand = brand
self.model = model
self.is_engine_on = False
self.current_speed = 0

def start_engine(self):
self.is_engine_on = True
print(f"{self.brand} {self.model} : Engine started.")

def stop_engine(self):
self.is_engine_on = False
self.current_speed = 0
print(f"{self.brand} {self.model} : Engine turned off.")

@abstractmethod
def accelerate(self):
pass

@abstractmethod
def brake(self):
pass


class ManualCar(Car):
"""
Represents a manual car with gear shifting functionality.
"""

def __init__(self, brand, model):
super().__init__(brand, model)
self.current_gear = 0

def shift_gear(self, gear):
self.current_gear = gear
print(f"{self.brand} {self.model} : Shifted to gear {self.current_gear}")

def accelerate(self):
if not self.is_engine_on:
print(f"{self.brand} {self.model} : Cannot accelerate! Engine is off.")
return
self.current_speed += 20
print(f"{self.brand} {self.model} : Accelerating to {self.current_speed} km/h")

def brake(self):
self.current_speed -= 20
if self.current_speed < 0:
self.current_speed = 0
print(f"{self.brand} {self.model} : Braking! Speed is now {self.current_speed} km/h")


class ElectricCar(Car):
"""
Represents an electric car with battery functionality.
"""

def __init__(self, brand, model):
super().__init__(brand, model)
self.battery_level = 100

def charge_battery(self):
self.battery_level = 100
print(f"{self.brand} {self.model} : Battery fully charged!")

def accelerate(self):
if not self.is_engine_on:
print(f"{self.brand} {self.model} : Cannot accelerate! Engine is off.")
return
if self.battery_level <= 0:
print(f"{self.brand} {self.model} : Battery dead! Cannot accelerate.")
return
self.battery_level -= 10
self.current_speed += 15
print(f"{self.brand} {self.model} : Accelerating to {self.current_speed} km/h. Battery at {self.battery_level}%.")

def brake(self):
self.current_speed -= 15
if self.current_speed < 0:
self.current_speed = 0
print(f"{self.brand} {self.model} : Regenerative braking! Speed is now {self.current_speed} km/h. Battery at {self.battery_level}%.")


if __name__ == "__main__":
my_manual_car = ManualCar("Suzuki", "WagonR")
my_manual_car.start_engine()
my_manual_car.accelerate()
my_manual_car.accelerate()
my_manual_car.brake()
my_manual_car.stop_engine()

print("----------------------")

my_electric_car = ElectricCar("Tesla", "Model S")
my_electric_car.start_engine()
my_electric_car.accelerate()
my_electric_car.accelerate()
my_electric_car.brake()
my_electric_car.stop_engine()
82 changes: 82 additions & 0 deletions Lecture 03/Python Code/Inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class Car:
"""
Represents a generic car with common attributes and methods.
"""

def __init__(self, brand, model):
self.brand = brand
self.model = model
self.is_engine_on = False
self.current_speed = 0

# Common methods for all cars
def start_engine(self):
self.is_engine_on = True
print(f"{self.brand} {self.model} : Engine started.")

def stop_engine(self):
self.is_engine_on = False
self.current_speed = 0
print(f"{self.brand} {self.model} : Engine turned off.")

def accelerate(self):
if not self.is_engine_on:
print(f"{self.brand} {self.model} : Cannot accelerate! Engine is off.")
return
self.current_speed += 20
print(f"{self.brand} {self.model} : Accelerating to {self.current_speed} km/h")

def brake(self):
self.current_speed -= 20
if self.current_speed < 0:
self.current_speed = 0
print(f"{self.brand} {self.model} : Braking! Speed is now {self.current_speed} km/h")


class ManualCar(Car):
"""
Represents a manual car with gear-shifting functionality.
"""

def __init__(self, brand, model):
super().__init__(brand, model)
self.current_gear = 0

# Specialized method for Manual Car
def shift_gear(self, gear):
self.current_gear = gear
print(f"{self.brand} {self.model} : Shifted to gear {self.current_gear}")


class ElectricCar(Car):
"""
Represents an electric car with battery functionality.
"""

def __init__(self, brand, model):
super().__init__(brand, model)
self.battery_level = 100

# Specialized method for Electric Car
def charge_battery(self):
self.battery_level = 100
print(f"{self.brand} {self.model} : Battery fully charged!")


# Main Method
if __name__ == "__main__":
my_manual_car = ManualCar("Suzuki", "WagonR")
my_manual_car.start_engine()
my_manual_car.shift_gear(1) # Specific to Manual Car
my_manual_car.accelerate()
my_manual_car.brake()
my_manual_car.stop_engine()

print("----------------------")

my_electric_car = ElectricCar("Tesla", "Model S")
my_electric_car.charge_battery() # Specific to Electric Car
my_electric_car.start_engine()
my_electric_car.accelerate()
my_electric_car.brake()
my_electric_car.stop_engine()
Loading