From cddb6466e7ab5ca728a6f638048315fd06dbe84e Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Tue, 12 May 2026 20:38:28 +0100 Subject: [PATCH 1/5] exercises double, double2 and types checking with mypy --- prep-exercises/double.py | 14 ++++++++++ prep-exercises/double2.py | 8 ++++++ prep-exercises/types_checking_with_mypy.py | 30 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 prep-exercises/double.py create mode 100644 prep-exercises/double2.py create mode 100644 prep-exercises/types_checking_with_mypy.py diff --git a/prep-exercises/double.py b/prep-exercises/double.py new file mode 100644 index 00000000..582f2e49 --- /dev/null +++ b/prep-exercises/double.py @@ -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 \ No newline at end of file diff --git a/prep-exercises/double2.py b/prep-exercises/double2.py new file mode 100644 index 00000000..dbe40a05 --- /dev/null +++ b/prep-exercises/double2.py @@ -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. \ No newline at end of file diff --git a/prep-exercises/types_checking_with_mypy.py b/prep-exercises/types_checking_with_mypy.py new file mode 100644 index 00000000..5312f8f7 --- /dev/null +++ b/prep-exercises/types_checking_with_mypy.py @@ -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}") \ No newline at end of file From 91bdeda6a79eb8c2b2a7e4abd8a61a84cce39202 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Tue, 19 May 2026 22:58:00 +0100 Subject: [PATCH 2/5] Person exercises --- prep-exercises/class_Person.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 prep-exercises/class_Person.py diff --git a/prep-exercises/class_Person.py b/prep-exercises/class_Person.py new file mode 100644 index 00000000..f7ae3a85 --- /dev/null +++ b/prep-exercises/class_Person.py @@ -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 \ No newline at end of file From 8ddccacc8256778e170896a6193daf2404dc1a2f Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Tue, 19 May 2026 23:59:06 +0100 Subject: [PATCH 3/5] other exercises --- prep-exercises/advantagesOfMethods.txt | 5 ++++ prep-exercises/class_Person_with_date.py | 38 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 prep-exercises/advantagesOfMethods.txt create mode 100644 prep-exercises/class_Person_with_date.py diff --git a/prep-exercises/advantagesOfMethods.txt b/prep-exercises/advantagesOfMethods.txt new file mode 100644 index 00000000..5577fa59 --- /dev/null +++ b/prep-exercises/advantagesOfMethods.txt @@ -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. \ No newline at end of file diff --git a/prep-exercises/class_Person_with_date.py b/prep-exercises/class_Person_with_date.py new file mode 100644 index 00000000..3f4e2fd7 --- /dev/null +++ b/prep-exercises/class_Person_with_date.py @@ -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()) \ No newline at end of file From 370025ef1bad2460e6754908c676f37fd3b91988 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 20 May 2026 00:34:13 +0100 Subject: [PATCH 4/5] dataclasses and laptopAllocation exercises --- prep-exercises/dataclasses.py | 25 ++++++++++++++++++ prep-exercises/laptopAllocated.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 prep-exercises/dataclasses.py create mode 100644 prep-exercises/laptopAllocated.py diff --git a/prep-exercises/dataclasses.py b/prep-exercises/dataclasses.py new file mode 100644 index 00000000..c51e85a2 --- /dev/null +++ b/prep-exercises/dataclasses.py @@ -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()) \ No newline at end of file diff --git a/prep-exercises/laptopAllocated.py b/prep-exercises/laptopAllocated.py new file mode 100644 index 00000000..a39736f0 --- /dev/null +++ b/prep-exercises/laptopAllocated.py @@ -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)) \ No newline at end of file From d691338d42e8c2a66593a1f6463dd2b2e847a84f Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 20 May 2026 01:00:49 +0100 Subject: [PATCH 5/5] enums and inheritance exercises --- prep-exercises/enums.py | 41 +++++++++++++++++++++++++++++++++++ prep-exercises/inheritance.py | 23 ++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 prep-exercises/enums.py create mode 100644 prep-exercises/inheritance.py diff --git a/prep-exercises/enums.py b/prep-exercises/enums.py new file mode 100644 index 00000000..52e9e7da --- /dev/null +++ b/prep-exercises/enums.py @@ -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)) \ No newline at end of file diff --git a/prep-exercises/inheritance.py b/prep-exercises/inheritance.py new file mode 100644 index 00000000..862b559d --- /dev/null +++ b/prep-exercises/inheritance.py @@ -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()) \ No newline at end of file