From 764a994ba5c5d94fcfdec597f7097a94870b438c Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Wed, 23 Oct 2024 11:55:24 +0200 Subject: [PATCH] Added new basic lessons. /JL --- .../basics/mutable_and_immutable_objects.py | 1 + Intermediate/basics/naming_convention.py | 2 +- basics/programming_paradigms.py | 63 +++++++++++++++++++ lessons/lesson_01/functions.py | 0 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 basics/programming_paradigms.py create mode 100644 lessons/lesson_01/functions.py diff --git a/Intermediate/basics/mutable_and_immutable_objects.py b/Intermediate/basics/mutable_and_immutable_objects.py index d724728..f4b0300 100644 --- a/Intermediate/basics/mutable_and_immutable_objects.py +++ b/Intermediate/basics/mutable_and_immutable_objects.py @@ -57,3 +57,4 @@ print(id(B)) # have been created and variable 'B' now points to this object. # The same is the case with string objects. Each string object can only contain the string # value, it was created with. + diff --git a/Intermediate/basics/naming_convention.py b/Intermediate/basics/naming_convention.py index 3bf21e8..dc08484 100644 --- a/Intermediate/basics/naming_convention.py +++ b/Intermediate/basics/naming_convention.py @@ -1,7 +1,7 @@ """ Basic naming conventions in python. We will be covering the following. -Variables, functions, classes, methods +Variables, functions, classes, methods etc. """ """ diff --git a/basics/programming_paradigms.py b/basics/programming_paradigms.py new file mode 100644 index 0000000..3b7775e --- /dev/null +++ b/basics/programming_paradigms.py @@ -0,0 +1,63 @@ +""" +A short description of the 3 main programming paradigms in Python +- Object oriented Programming - OOP +- Procedure Oriented programming +- Functional programming +""" + +# Object Oriented Programming - OOP +# This style of programming revolves around objects (classes) as the key element. +# Lets say you're building cars. +# A car can be split into several components, each described by a class. +# i.e. Chassis, Wheels, Engine, Interior +# At the toplevel, you would have the Car class, storing the other classes as it's attributes. +# Here's an example + +class Chassis: + CHASSIS_TYPE = ["Sedan", "Estate", "Pickup"] + COLOR = ["Red", "Black", "Emerald"] + def __init__(self, type="Sedan", col="Black"): + self.type = type + self.color = col + +class Wheels: + RIM_TYPE = ["Steel rim", "Alloy rim"] + TYRE_TYPE = ["R16/55 x 195", "R17/50 x 205", "R18/45 x 215", "R18/45 x 225", + "R19/40 x 225", "R19/40 x 235"] + def __init__(self, rim="Steel", tyre="R16/55 x 195"): + self.rim = rim + self.tyre = tyre + +class Engine: + ENGINE_TYPE = ["Row", "V"] + CYLINDER_COUNT = [4, 5, 6, 8] + ENGINE_VOLUME = [1.4, 1.6, 1.8, 2.0, 3.0, 4.5] + ENGINE_FUEL = ["Diesel", "Petrol", "Hydrogen", "Hybrid"] + def __init__(self, type="Row", cyl=4, vol=1.4, fuel="Petrol"): + self.type = type + self.cylinder = cyl + self.volume = vol + self.fuel = fuel + +class Interior: + INTERIOR_TYPE = ["Fabric", "Leather", "Vinyl"] + STEERING_WHEEL = ["3-spoke bare", "2-spoke Leather"] + def __init__(self, int="Fabric", steering="3-spoke bare"): + self.interior = int + self.steering_wheel = steering + +class Car: + def __init__(self, type="Sedan", col="Black", rim="Steel rim", + tyre="R16/55 x 195", engine="Row", cyl=4, vol=1.4, + fuel="Petrol", int="Fabric", steering="3-spoke bare"): + self.chassis = Chassis(type, col) + self.wheels = Wheels(rim, tyre) + self.engine = Engine(engine, cyl, vol, fuel) + self.interior = Interior(int, steering) + +# Now we have the classes define with their default values. +# So now we can create our Car() object like this. +if __name__ == "__main__": + mycar = Car() + print(mycar.chassis.type) + print(mycar.engine.type, str(mycar.engine.cylinder)+" Cylinder", str(mycar.engine.volume)+" Litre", mycar.engine.fuel) \ No newline at end of file diff --git a/lessons/lesson_01/functions.py b/lessons/lesson_01/functions.py new file mode 100644 index 0000000..e69de29