From d173b24da1b12c468e2e9bd6960f6e2fb59d5582 Mon Sep 17 00:00:00 2001 From: rberbenkova Date: Thu, 9 Oct 2025 15:30:52 +0200 Subject: [PATCH 1/2] First Commit --- lab-oop_in_python.ipynb | 174 +++++++++++++++++++++++++++++++--------- 1 file changed, 135 insertions(+), 39 deletions(-) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index 3142c5e..d3bde02 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -30,17 +30,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "260 20000\n" + ] + } + ], "source": [ "# Your code here\n", "class Vehicle:\n", " # Hint: Define __init__ method with parameters for max_speed and mileage\n", - " pass\n", + " # Define the Vehicle class (class like a blueprint for creating objects)\n", + " \n", + " def __init__(self, max_speed, mileage): #constructor, runs automatically whenever a vehicle object created\n", + " self.max_speed = max_speed #takes parameters max speed and mileage\n", + " self.mileage = mileage #assigns them to the object\n", + " \n", + " # self.max_speed = max_speed - store the value of max_speed inside the object's data\n", "\n", - "# Example instantiation\n", - "modelX = Vehicle() # Create an instance of Vehicle\n", + "# Example instantiation - creating an object instance- makes new Vehicle\n", + "modelX = Vehicle(260, 20000) # Create an instance(modelX) of Vehicle \n", "\n", "# Print attributes\n", "print(modelX.max_speed, modelX.mileage) # Expected output: (value of max_speed, value of mileage)" @@ -68,13 +82,36 @@ } ], "source": [ - "# Your code here\n", - "class Vehicle:\n", - " pass\n", + "# This is useful when you're designing a program and want to define your structure first\n", "\n", - "# Example instantiation\n", + "class Vehicle: #defines a class named Vehicle\n", + " \n", + " #Method 1 - From built-in object type \n", + " #pass - acts as a placeholder, so Python doesn't give an error (empty class)\n", + " \n", + " #Method 2 - Using empty docstring\n", + " \"\"\"Vehicle class\"\"\"\n", + " \n", + " #Method 3 - Using ellipsis(...) RESEARCH MORE\n", + " # ...\n", + " #EXPLANATION: \n", + " #Ellipsis Literal (...) - builti-in constant; singleton object of type ellipsis - represents ommission (to be filled in later)\n", + "\n", + " \n", + " #Method 4 - Using a null lambda\n", + " #__init__ = lambda self: None\n", + " \n", + " # EXPLANATION: lambda function as a method \n", + " # lambda self: None - creates an anonymous function; takes self as parameter; Returns None (does nothing) \n", + " \n", + " # __init__ is the constructor method in Python\n", + " # assigning our lambda to __init__(creating a custom constructor) \n", + " # this overrides the default inherrited __init__ method\n", + "\n", + "# Example instantiation - can still create an object from it (with no data, methods)\n", "my_vehicle = Vehicle()\n", - "print(type(my_vehicle)) # Expected output: " + "print(type(my_vehicle)) #Python shows what type of object my vehicle is \n", + "# Expected output: " ] }, { @@ -99,13 +136,28 @@ } ], "source": [ - "# Your code here\n", - "class Bus(Vehicle):\n", - " pass\n", - "\n", + "# create a Child Class (Bus)\n", + "class Bus(Vehicle): # inherits all attributes and methods from (Vehicle) Bus is a child/subclass of Vehicle\n", + " pass # no init method defined, automatically uses one from vehicle\n", + " # Bus objects have access to self.max_speed, self.mileage and any later defined methods\n", "# Example instantiation\n", - "school_bus = Bus()\n", - "print(type(school_bus)) # Expected output: " + "school_bus = Bus() #create an instant/object of the Bus class (school_bus)\n", + "print(type(school_bus)) # Expected output: \n", + "\n", + "\n", + "\n", + "# if you want to add extra features to the Bus class(in addtition to Vehicle)\n", + "# class Bus(Vehicle):\n", + " #def __init__(self, max_speed, mileage, capacity):\n", + " # Call the parent class constructor\n", + " #super().__init__(max_speed, mileage)\n", + " #self.capacity = capacity\n", + "# then\n", + " #school_bus = Bus(120, 15000, 50)\n", + " #print(\"Max speed:\", school_bus.max_speed)\n", + " #print(\"Mileage:\", school_bus.mileage)\n", + " #print(\"Capacity:\", school_bus.capacity)\n", + " " ] }, { @@ -125,19 +177,23 @@ "name": "stdout", "output_type": "stream", "text": [ - "Base fare\n" + "Base fare with extra charge\n" ] } ], "source": [ - "# Your code here\n", + "# Define parent class Vehicle with a fare() method\n", "class Vehicle:\n", + " \n", " def fare(self):\n", " return \"Base fare\"\n", "\n", - "class Bus(Vehicle):\n", + "class Bus(Vehicle): #Bus inherits from Vehicle, but needs to override fare() method\n", " # Hint: Override fare method to include extra charges\n", - " pass\n", + " def fare(self):\n", + " base_fare = super().fare() #calls the parent Vehicle version of fare()\n", + " new_fare = base_fare + \" with extra charge\" # adds/overrides parent's method\n", + " return new_fare\n", "\n", "school_bus = Bus()\n", "print(school_bus.fare()) # Expected output: \"Base fare with extra charge\"" @@ -165,17 +221,43 @@ } ], "source": [ - "# Your code here\n", + "# Define a Class with a Class Attribute (ex with Vehicle class)\n", "class Vehicle:\n", - " color = \"White\" # Hint: Define color as a class attribute\n", - "\n", - " def __init__(self, name, max_speed, mileage):\n", + " color = \"White\" # Hint: Define color as a class attribute (shared by all instances)\n", + " # when color = \"White\" inside the class (but outside _init_); every intance gets this same default value\n", + " def __init__(self, name, max_speed, mileage): # instance attributes - belong to specific objects\n", " self.name = name\n", " self.max_speed = max_speed\n", " self.mileage = mileage\n", "\n", "school_bus = Vehicle(\"School Volvo\", 180, 12)\n", - "print(school_bus.color) # Expected output: \"White\"" + "print(school_bus.color) # Expected output: \"White\"\n", + "\n", + "# Option: Change the Class Attribute for Everyone (if you update the class attribute, it changes for all objects)\n", + "# unless instance overrides it\n", + " #Vehicle.color = \"White\"\n", + " #print(school_bus.color) # expected output - White\n", + " \n", + "# Option: Change for One Object Only (overrides the class attribute)\n", + " # school_bus.color = \"White\"\n", + " # print(school_bus.color) # expected output - \"White\"\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "# SUMMARY\n", + "# Class Attribute - ex: color = \"White\"\n", + " # defined inside the class, outside methods\n", + " # shared by all objects\n", + "# Instance Attribute - ex: self.name; self.mileage\n", + " # Inside _init_()\n", + " # unique to each object\n", + "\n" ] }, { @@ -195,27 +277,40 @@ "name": "stdout", "output_type": "stream", "text": [ - "Total Bus fare is: 5000\n" + "Total Bus fare is: 5500.0\n" ] } ], "source": [ - "# Your code here\n", - "class Vehicle:\n", - " def __init__(self, name, mileage, capacity):\n", - " self.name = name\n", + "# combine inheritance, method overriding, and a default fare formula\n", + "class Vehicle: # Base (parent) class for all types of vehicles\n", + " \n", + " def __init__(self, name, mileage, capacity): # has instance variables: name, mileage and capacity\n", + " self.name = name \n", " self.mileage = mileage\n", " self.capacity = capacity\n", "\n", - " def fare(self):\n", + " def fare(self): #default fare calculation # the method fare() returns capacity *100 - default fare formula\n", " return self.capacity * 100\n", - "\n", - "class Bus(Vehicle):\n", + " \n", + "# Child class (Bus) \n", + "class Bus(Vehicle): # Bus inherits from Vehicle\n", " # Hint: Override fare method to include maintenance charge\n", - " pass\n", + " def fare(self):\n", + " base_fare = super().fare() #get base from parent class\n", + " new_fare = base_fare + (0.1 * base_fare) # new fare wirh addirional maintainance charge\n", + " return new_fare #return overriden new_fare\n", "\n", + "\n", + "# Create a Bus object\n", "school_bus = Bus(\"School Volvo\", 12, 50)\n", - "print(\"Total Bus fare is:\", school_bus.fare()) # Expected output: Total Bus fare is: (calculated amount)" + "# print the total fare\n", + "print(f\"Total Bus fare is: {school_bus.fare()}\") \n", + "\n", + "# line has two parts \n", + " #1. school_bus.fare() - calls a method named fare(), that belongs to school_bus object in the Bus class; calculates; returns \n", + " #2. f before the string (firmatted string literal f-string); anything inside {...} is evaluated first, then inserted into the str.\n", + "# Expected output: Total Bus fare is: (calculated amount)" ] }, { @@ -240,7 +335,7 @@ } ], "source": [ - "# Your code here\n", + "# to check the type of any object - use built-in function type()\n", "school_bus = Bus(\"School Volvo\", 12, 50)\n", "print(type(school_bus)) # Expected output: " ] @@ -267,7 +362,8 @@ } ], "source": [ - "# Your code here\n", + "\n", + " \n", "print(isinstance(school_bus, Vehicle)) # Expected output: True or False based on inheritance" ] }, @@ -287,7 +383,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -301,7 +397,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.0" + "version": "3.13.7" } }, "nbformat": 4, From d62186bbfef2a3ba6a731f694848e1d15766ce49 Mon Sep 17 00:00:00 2001 From: rberbenkova Date: Thu, 9 Oct 2025 15:32:28 +0200 Subject: [PATCH 2/2] Second Commit --- lab-oop_in_python.ipynb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lab-oop_in_python.ipynb b/lab-oop_in_python.ipynb index d3bde02..55fa2bd 100644 --- a/lab-oop_in_python.ipynb +++ b/lab-oop_in_python.ipynb @@ -321,6 +321,15 @@ "Write a program to determine which class a given object belongs to." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print('Hello')" + ] + }, { "cell_type": "code", "execution_count": 7,