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
5 changes: 5 additions & 0 deletions prep-exercises/advantagesOfMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Methods make the code more organised because everything stays inside the class.
It also looks cleaner and easier to read.
imran.is_adult() looks better than is_adult(imran)
Its easier to edit later because you only change the code in one place.
Methods also make more sense because the function belongs to the object itself.
25 changes: 25 additions & 0 deletions prep-exercises/class_Person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Person:
def __init__(self, name: str, age: int, preferred_operating_system: str):
self.name = name
self.age = age
self.preferred_operating_system = preferred_operating_system

imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
print(imran.address)

eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
print(eliza.address)

# -------------

def is_adult(person: Person) -> bool:
return person.age >= 18

print(is_adult(imran))

# -------------

def birthday(person: Person) -> str:
return person.birthday
38 changes: 38 additions & 0 deletions prep-exercises/class_Person_with_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from datetime import date


class Person:
def __init__(
self,
name: str,
date_of_birth: date,
preferred_operating_system: str,
):
self.name = name
self.date_of_birth = date_of_birth
self.preferred_operating_system = preferred_operating_system

def is_adult(self) -> bool:
today = date.today()

age = (
today.year
- self.date_of_birth.year
)

if (
(today.month, today.day)
< (self.date_of_birth.month, self.date_of_birth.day)
):
age -= 1

return age >= 18


imran = Person(
"Imran",
date(2002, 5, 10),
"Ubuntu"
)

print(imran.is_adult())
25 changes: 25 additions & 0 deletions prep-exercises/dataclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from dataclasses import dataclass
from datetime import date


@dataclass
class Person:
name: str
date_of_birth: date
preferred_operating_system: str

def is_adult(self):
today = date.today()

age = today.year - self.date_of_birth.year

if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
age -= 1

return age >= 18


imran = Person("Imran", date(2002, 5, 10), "Ubuntu")

print(imran)
print(imran.is_adult())
14 changes: 14 additions & 0 deletions prep-exercises/double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def half(value):
return value / 2

def double(value):
return value * 2

def second(value):
return value[1]

print(double("22"))

# My prediction is that double("22") will return 44
# When running the code, it did not return what I expected. instead, the output is '2222'
# It returned '2222' because the input ( 22 ) is a text not a number
8 changes: 8 additions & 0 deletions prep-exercises/double2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def double(number):
return number * 3

print(double(10))

# The but in this code is that the parameter type of the variable number is not
# specified, the function takes whatever passed and multiplies it by 3, so if we
# pass a string it will concatenate the string 3 times instead of multiplying the number by 3.
41 changes: 41 additions & 0 deletions prep-exercises/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from enum import Enum
from dataclasses import dataclass


class OS(Enum):
UBUNTU = "Ubuntu"
MAC = "Mac"
WINDOWS = "Windows"


@dataclass
class Laptop:
model: str
os: OS


@dataclass
class Person:
name: str
age: int
preferred_os: OS


laptops = [
Laptop("Dell", OS.UBUNTU),
Laptop("MacBook", OS.MAC),
Laptop("HP", OS.WINDOWS),
]

person = Person("Ali", 22, OS.UBUNTU)


def match(laptops, person):
result = []
for l in laptops:
if l.os == person.preferred_os:
result.append(l)
return result


print(match(laptops, person))
23 changes: 23 additions & 0 deletions prep-exercises/inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
return "some sound"


class Dog(Animal):
def speak(self):
return "Woof!"


class Cat(Animal):
def speak(self):
return "Meow!"


dog = Dog("Rex")
cat = Cat("Milo")

print(dog.name, dog.speak())
print(cat.name, cat.speak())
43 changes: 43 additions & 0 deletions prep-exercises/laptopAllocated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dataclasses import dataclass
from typing import List
from enum import Enum


class OperatingSystem(Enum):
UBUNTU = "Ubuntu"
MAC = "Mac"
WINDOWS = "Windows"


@dataclass
class Person:
name: str
age: int
preferred_os: OperatingSystem


@dataclass
class Laptop:
model: str
os: OperatingSystem


def get_matching_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]:
matches = []

for laptop in laptops:
if laptop.os == person.preferred_os:
matches.append(laptop)

return matches


laptops = [
Laptop("Dell", OperatingSystem.UBUNTU),
Laptop("MacBook", OperatingSystem.MAC),
Laptop("HP", OperatingSystem.WINDOWS),
]

person = Person("Ali", 22, OperatingSystem.UBUNTU)

print(get_matching_laptops(laptops, person))
30 changes: 30 additions & 0 deletions prep-exercises/types_checking_with_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def open_account(balances : dict[str, int], name : str, amount : int) -> None:
balances[name] = amount

def sum_balances(accounts : dict[str, int]) -> int:
total = 0
for name, pence in accounts.items():
print(f"{name} had balance {pence}")
total += pence
return total

def format_pence_as_string(total_pence : int) -> str:
if total_pence < 100:
return f"{total_pence}p"
pounds = int(total_pence / 100)
pence = total_pence % 100
return f"£{pounds}.{pence:02d}"

balances = {
"Sima": 700,
"Linn": 545,
"Georg": 831,
}

open_account(balances, "Tobi", 913)
open_account(balances, "Olya", 713)

total_pence = sum_balances(balances)
total_string = format_pence_as_string(total_pence)

print(f"The bank accounts total {total_string}")
Loading