|
| 1 | +### ✅ Class |
| 2 | +""" |
| 3 | +A class in python is a blueprint for creating objects. |
| 4 | +It groups related data (attributes) and actions (methods) together. |
| 5 | +Think of a class like a template; |
| 6 | +where it defines characteristics like color, size, etc... |
| 7 | +""" |
| 8 | +class Book: |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +### ✅ Object: |
| 13 | +""" |
| 14 | +An object is an instance of a class in Python. It represents a specific |
| 15 | +example of the blueprint defined by class. Now with our Dog; |
| 16 | +If Dog is a class, then `variable` my_dog could be an object representing a specific dog with |
| 17 | +it's details or functions, like name or age. |
| 18 | +""" |
| 19 | + |
| 20 | +class Dog: |
| 21 | + def __init__(self, name, age): |
| 22 | + self.name = name |
| 23 | + self.age = age |
| 24 | + |
| 25 | +# Object of the Dog class |
| 26 | +my_dog = Dog("Schrodinger", 5) |
| 27 | + |
| 28 | + |
| 29 | +### ✅ Inheritance |
| 30 | +""" |
| 31 | +Inheritance allows you to create a new class that uses the features of |
| 32 | +an already existing class without necessarily changing it. |
| 33 | +
|
| 34 | +This lets you build on existing functionality while keeping the original |
| 35 | +class intact. |
| 36 | +""" |
| 37 | + |
| 38 | +class Animal: |
| 39 | + def __init__(self, name): |
| 40 | + self.name = name |
| 41 | + |
| 42 | + def spreche(self): |
| 43 | + print(f"{self.name} sagst: Sei zufrienden") |
| 44 | + |
| 45 | +#Derived class |
| 46 | +class Dog(Animal): |
| 47 | + def spreche(self): |
| 48 | + print(f"{self.name} bellst") |
| 49 | + |
| 50 | + |
| 51 | +# Creating an object for the dog class |
| 52 | +my_dog =Dog("Arf") |
| 53 | +my_dog.spreche() # Output: Arf bellst |
| 54 | +''' |
| 55 | +Dog is a derived class which inherits from Animal class. |
| 56 | +--- |
| 57 | +Overrides spreche(speak) method to provide a behaviour for parsed in (Animal). |
| 58 | +''' |
| 59 | + |
| 60 | + |
| 61 | +### ✅ Encapsulation |
| 62 | +""" |
| 63 | +In Python's Object-Oriented Programming (OOP), we can restrict access |
| 64 | +to methods and variables to prevent direct modification, |
| 65 | +especially when dealing with multiple lines of codes all performing various functions. |
| 66 | +This is referred to as encapsulation. |
| 67 | +""" |
| 68 | + |
| 69 | +class Person: |
| 70 | + def __init__(self, name, age): |
| 71 | + self.name = name |
| 72 | + self.__age = age #Private attribute |
| 73 | + |
| 74 | + def get_age(self): |
| 75 | + return self.__age #Accessor method |
| 76 | + |
| 77 | +person = Person("Alice", 30) |
| 78 | +print(person.get_age()) #Gives an output of 30 |
| 79 | + |
| 80 | +''' |
| 81 | +It's noted from this example that the 'the__age attribute is private, |
| 82 | +and can only be accessed by calling the get_age() method. |
| 83 | +''' |
| 84 | + |
| 85 | + |
| 86 | +### ✅ Polymorphism: |
| 87 | +""" |
| 88 | +Polymorphism really means "more than one forms." In programming (python🤧), |
| 89 | +polymorphism allows a single entity (like a method, operator, or object) |
| 90 | +to take on different forms and behave differently in various scenarios, as needed. |
| 91 | +""" |
| 92 | + |
| 93 | +class Pferd: |
| 94 | + def speak(self): |
| 95 | + print("Neigh") |
| 96 | + |
| 97 | +class Eule: |
| 98 | + def speak(self): |
| 99 | + print("Hoot") |
| 100 | + |
| 101 | +def animal_speak(animal): |
| 102 | + animal.speak() |
| 103 | + |
| 104 | +# Using polymorphism |
| 105 | +pferd = Pferd() |
| 106 | +eule = Eule() |
| 107 | + |
| 108 | +animal_speak(pferd) # Neigh |
| 109 | +animal_speak(eule) # Hoot |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + ### Try Area |
| 114 | + |
| 115 | +## Basic Functions |
| 116 | +''' |
| 117 | +functions are simple, and does'nt include any aparameters or return values |
| 118 | +and performs a single task, like so...; |
| 119 | +''' |
| 120 | +def greet(): |
| 121 | + print("Hallo, Leute!") |
| 122 | + |
| 123 | + |
| 124 | +## Functions with parameters |
| 125 | +#: functions accepting parameters are more flexible and readable. |
| 126 | +def greet(name): |
| 127 | + print(f"Hallo, {name}") |
| 128 | + |
| 129 | + |
| 130 | +## Functions with Return Values |
| 131 | +''' |
| 132 | +functions return values, used instead of printing, |
| 133 | +allowing caller to use the result, return ends the code and starts outside the function(def). |
| 134 | +''' |
| 135 | +def add(a, b): |
| 136 | + return a + b |
| 137 | +result = add(3, 5) |
| 138 | +print(result) |
| 139 | + |
| 140 | + |
| 141 | +## Functions with Default Parameters |
| 142 | +#: Introducing default parameters makes the function to be more versatile. |
| 143 | +def greet(name="Welt"): |
| 144 | + print(f"Hallo, {name}!") |
| 145 | +greet() |
| 146 | +greet("Henify_") # Replaces name="..." value to Henify_ |
| 147 | + |
| 148 | + |
| 149 | +## Functions with Variable Arguments |
| 150 | +''' |
| 151 | +Using " *args and **kwargs" in python lets |
| 152 | +functions to handle variable number arguments. |
| 153 | +''' |
| 154 | + |
| 155 | +def add_all(*args): # With *args |
| 156 | + return sum(args) |
| 157 | +print(add_all(1, 2, 3, 4, 5)) |
| 158 | + |
| 159 | +def print_info(**kwargs) # With **kwargs |
| 160 | + for key, value in kwargs.items(): |
| 161 | + print(f"{key}: {value}") |
| 162 | + |
| 163 | +print_info(name="Henry", age=17) |
| 164 | + |
| 165 | + |
| 166 | +## Intermediate Functions (Higher_Order_Functions, Lambdas, and Decorators) |
| 167 | +''' |
| 168 | +Higher level functions; these functions take other functions as arguments or return them. |
| 169 | +Lambdas; Anonymous, inline functions. |
| 170 | +Decorators: Modifies behaviour of other functions |
| 171 | +''' |
| 172 | + |
| 173 | +def apply_functions(func, value): |
| 174 | + return func(value) |
| 175 | +print(apply_functions(lambda x: x**2, 5)) |
| 176 | + |
| 177 | +def decorator(func): |
| 178 | + def wrapper(): |
| 179 | + print("Before function call") |
| 180 | + func() |
| 181 | + print("After function call") |
| 182 | + return wrapper() |
| 183 | + |
| 184 | +@decorator #Decorator |
| 185 | +def say_hello(): |
| 186 | + print("Hallo ✌🏾") |
| 187 | +say_hello() |
| 188 | + |
| 189 | + |
| 190 | + |
0 commit comments