mirror of
https://gitea.com/Lerking/python-crash-course.git
synced 2026-01-07 09:10:08 +01:00
Added new basic lessons. /JL
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
Basic naming conventions in python.
|
||||
We will be covering the following.
|
||||
Variables, functions, classes, methods
|
||||
Variables, functions, classes, methods etc.
|
||||
"""
|
||||
|
||||
"""
|
||||
|
||||
63
basics/programming_paradigms.py
Normal file
63
basics/programming_paradigms.py
Normal file
@@ -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)
|
||||
0
lessons/lesson_01/functions.py
Normal file
0
lessons/lesson_01/functions.py
Normal file
Reference in New Issue
Block a user