diff --git a/Methods.py b/Methods.py new file mode 100644 index 00000000..5d217efc --- /dev/null +++ b/Methods.py @@ -0,0 +1,38 @@ +# EXERCISE 1: Think of the advantages of using methods instead of free functions. Write them down in your notebook +# - Methods keep data and behavior together +# - Methods make code easier to read +# - Methods reduce mistakes +# - Easier to extend and maintain +# - Methods support inheritance +# - Better organization +# - Better organization +# - Methods enable polymorphism + + + +# EXERCISE 2: Change the Person class to take a date of birth (using the standard library’s datetime.date class) and store it in a field instead of age. +# Update the is_adult method to act the same as before. + + +from datetime import date +from dataclasses import dataclass + +@dataclass (frozen=True) +class Person: + name: str + date_of_birth: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.date_of_birth.year + + # Adjust if birthday hasn't happened yet this year + if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): + age -= 1 + return age >= 18 + +imran = Person("Jesus", date(1980, 1, 12), "Ubuntu") +print(imran.is_adult()) + + diff --git a/class-and-object.py b/class-and-object.py new file mode 100644 index 00000000..c7a242cf --- /dev/null +++ b/class-and-object.py @@ -0,0 +1,48 @@ +#Read the error, and make sure you understand what it’s telling you. + + + +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) + + +eliza = Person("Eliza", 34, "Arch Linux") +print(eliza.name) + +def is_adult(person: Person) -> bool: + return person.age >= 18 + +print(is_adult(imran)) + + +def address(person: Person) -> str: + return person.address + + +#EXERCISE 1: +#Add the is_adult code to the file you saved earlier. +#Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. +# Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. + +# SOLUTION: +# After run class-and-object.py, I get the following mypy errors: +# class-and-object.py:12: error: "Person" has no attribute "address" +# It indicates that the Person class does not have an attribute named address. + +#EXERCISE 2: +# Add the is_adult code to the file you saved earlier +# Run it through mypy - notice that no errors are reported - mypy understands that Person has a property named age so is happy with the function. + +# Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error. +# When running mypy, I get the following error: +# Class-and-object.py:27: error: "Person" has no attribute "address" [attr-defined] +# Found 1 error in 1 file (checked 1 source file) +# This error indicates that the Person class does not have an attribute named address. diff --git a/data-classes.py b/data-classes.py new file mode 100644 index 00000000..06d8ed8a --- /dev/null +++ b/data-classes.py @@ -0,0 +1,25 @@ + +# Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age. +# Re-add the is_adult method to it. + +from datetime import date +from dataclasses import dataclass + +@dataclass (frozen=True) +class Person: + name: str + date_of_birth: date + preferred_operating_system: str + + def is_adult(self) -> bool: + today = date.today() + age = today.year - self.date_of_birth.year + + # Adjust if birthday hasn't happened yet this year + if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day): + age -= 1 + return age >= 18 + +jesus = Person("Jesus", date(1980, 1, 12), "Ubuntu") +print(jesus.is_adult()) + diff --git a/enumeration.py b/enumeration.py new file mode 100644 index 00000000..3ab5d813 --- /dev/null +++ b/enumeration.py @@ -0,0 +1,100 @@ + +# EXERCISE 1: +# Write a program which: + +# Already has a list of Laptops that a library has to lend out. +# Accepts user input to create a new Person - it should use the input function to read a person’s name, age, and preferred operating system. +# Tells the user how many laptops the library has that have that operating system. +# If there is an operating system that has more laptops available, tells the user that if they’re willing to accept that operating system they’re more likely to get a laptop. +# You should convert the age and preferred operating system input from the user into more constrained types as quickly as possible, and should output errors to stderr and terminate the program with a non-zero exit code if the user input bad values. + +# SOLUTION: + +import sys +from dataclasses import dataclass +from enum import Enum +from typing import List + +class OperatingSystem(Enum): + MACOS = "macOS" + ARCH = "Arch Linux" + UBUNTU = "Ubuntu" + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_system: OperatingSystem + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: OperatingSystem + +# Predefined laptops +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system=OperatingSystem.ARCH), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system=OperatingSystem.UBUNTU), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system=OperatingSystem.MACOS), +] + +def count_laptops_by_os(laptops: List[Laptop]) -> dict: + os_count = {os: 0 for os in OperatingSystem} + for laptop in laptops: + os_count[laptop.operating_system] += 1 + return os_count + +def main(): + # --- Input name --- + name = input("Enter your name: ").strip() + if not name: + print("Error: Name cannot be empty.", file=sys.stderr) + sys.exit(1) + + # --- Input age --- + age_input = input("Enter your age: ").strip() + try: + age = int(age_input) + if age <= 0: + raise ValueError() + except ValueError: + print("Error: Age must be a positive integer.", file=sys.stderr) + sys.exit(1) + + # --- Input preferred OS --- + print("Choose preferred operating system:") + for os in OperatingSystem: + print(f"- {os.value}") + os_input = input("Enter OS: ").strip() + + try: + preferred_os = OperatingSystem(os_input) + except ValueError: + print("Error: Invalid operating system.", file=sys.stderr) + sys.exit(1) + + # --- Create Person --- + new_person = Person(name=name, age=age, preferred_operating_system=preferred_os) + + # --- Count laptops by OS --- + os_count = count_laptops_by_os(laptops) + + # --- Show count for user's preferred OS --- + count_for_user_os = os_count.get(new_person.preferred_operating_system, 0) + print(f"There are {count_for_user_os} laptops available with {new_person.preferred_operating_system.value}.") + + # --- Find the OS with most laptops --- + max_os = max(os_count, key=os_count.get) + max_count = os_count[max_os] + + # Suggest alternative if more laptops are available + if max_os != new_person.preferred_operating_system and max_count > count_for_user_os: + print(f"Tip: There are more laptops available with {max_os.value} ({max_count} laptops). " + f"If you're willing to accept that OS, you're more likely to get a laptop.") + +if __name__ == "__main__": + main() diff --git a/generics.py b/generics.py new file mode 100644 index 00000000..b953b482 --- /dev/null +++ b/generics.py @@ -0,0 +1,26 @@ + +#EXERCISE 1: Fix the above code so that it works. You must not change the print on line 17 +# we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children) + +# SOLUTION: + +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int = 0 + children: List["Person"] + +fatma = Person(name="Fatma", age=18, children=[]) +aisha = Person(name="Aisha", age=24, children=[]) + +imran = Person(name="Imran", age=45, children=[fatma aisha]) + +def print_family_tree(person: Person) -> None: + print(person.name) + for child in person.children: + print(f"- {child.name} ({child.age})") + +print_family_tree(imran) \ No newline at end of file diff --git a/inheritance.py b/inheritance.py new file mode 100644 index 00000000..4a431f11 --- /dev/null +++ b/inheritance.py @@ -0,0 +1,51 @@ + +#EXERCISE 1:: + +#Play computer with this code. Predict what you expect each line will do. +#Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines). + +#SOLUTION: + +# The first four prints will work fine, as the Child class inherits from the Parent class and has access to its methods. +# The last four prints will get error because the Parent class does not have the methods get_full_name and change_last_name defined. +# Fix the code we can comment out the last four lines or we can add the methods to the Parent class. + +class Parent: + def __init__(self, first_name: str, last_name: str): + self.first_name = first_name + self.last_name = last_name + + def get_name(self) -> str: + return f"{self.first_name} {self.last_name}" + + +class Child(Parent): + def __init__(self, first_name: str, last_name: str): + super().__init__(first_name, last_name) + self.previous_last_names = [] + + def change_last_name(self, last_name) -> None: + self.previous_last_names.append(self.last_name) + self.last_name = last_name + + def get_full_name(self) -> str: + suffix = "" + if len(self.previous_last_names) > 0: + suffix = f" (née {self.previous_last_names[0]})" + return f"{self.first_name} {self.last_name}{suffix}" + +person1 = Child("Elizaveta", "Alekseeva") +print(person1.get_name()) +print(person1.get_full_name()) +person1.change_last_name("Tyurina") +print(person1.get_name()) +print(person1.get_full_name()) + +""" +person2 = Parent("Elizaveta", "Alekseeva") +print(person2.get_name()) +print(person2.get_full_name()) +person2.change_last_name("Tyurina") +print(person2.get_name()) +print(person2.get_full_name()) +""" \ No newline at end of file diff --git a/type-checking.py b/type-checking.py new file mode 100644 index 00000000..097a9540 --- /dev/null +++ b/type-checking.py @@ -0,0 +1,97 @@ +# his code contains bugs related to types. They are bugs mypy can catch. + +# Read this code to understand what it’s trying to do. +# Add type annotations to the method parameters and return types of this code. +# Run the code through mypy, and fix all of the bugs that show up. +# When you’re confident all of the type annotations are correct, and the bugs are fixed, run the code and check it works. + + + +def open_account(balances, name, amount): + balances[name] = amount + +def sum_balances(accounts): + 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): + 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, +} + +# convert pounds to pence +open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence +open_account(balances, "Olya", int(7.13 * 100)) # 713 pence + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") + + + + +# When running mypy, I get the following errors: + +# type-checking.py:24: error: Missing positional argument "amount" in call to "open_account" [call-arg] +# type-checking.py:25: error: Missing positional argument "amount" in call to "open_account" [call-arg] +# type-checking.py:28: error: Name "format_pence_as_str" is not defined [name-defined] +# type-checking.py:34: error: Missing positional argument "amount" in call to "open_account" [call-arg] +# type-checking.py:35: error: Missing positional argument "amount" in call to "open_account" [call-arg] +# type-checking.py:38: error: Name "format_pence_as_str" is not defined [name-defined] + + +# To fix this code, I need to add type annotations and correct the function calls as follows: + +# wrong arguments to open_account, the function expects three arguments: balances, name, and amount. +# one call passes a string "£7.13" instead of a number. +# Wrong function name format_pence_as_str instead of format_pence_as_string. +# To keep the program consistent (Everything in pence), convert pounds to pence when opening accounts. + +# Here is the corrected code: + +""" +def open_account(balances, name, amount): + balances[name] = amount + +def sum_balances(accounts): + 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): + 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, +} + +# convert pounds to pence +open_account(balances, "Tobi", int(9.13 * 100)) # 913 pence +open_account(balances, "Olya", int(7.13 * 100)) # 713 pence + +total_pence = sum_balances(balances) +total_string = format_pence_as_string(total_pence) + +print(f"The bank accounts total {total_string}") +""" + diff --git a/type-guided.py b/type-guided.py new file mode 100644 index 00000000..46802768 --- /dev/null +++ b/type-guided.py @@ -0,0 +1,57 @@ +# EXERCISE 1: +# Try changing the type annotation of Person.preferred_operating_system from str to List[str]. +# Run mypy on the code. +# It tells us different places that our code is now wrong, because we’re passing values of the wrong type. +# We probably also want to rename our field - lists are plural. Rename the field to preferred_operating_systems. +# Run mypy again. +# Fix all of the places that mypy tells you need changing. Make sure the program works as you’d expect. + +# When we run mypy, we get the following errors: +# type-guided.py:42: error: Argument "preferred_operating_system" to "Person" has incompatible type "str"; expected "list[str]" [arg-type] +# type-guided.py:43: error: Argument "preferred_operating_system" to "Person" has incompatible type "str"; expected "list[str]" [arg-type] + +# To solve these errors, I modified the instance to pass a list of strings instead of a single string for the preferred_operating_systems field. +# Also, In order to list all possible laptops for each person, I compared the list of preferred operating systems of each person with the operating system of each laptop. + +from dataclasses import dataclass +from typing import List + +@dataclass(frozen=True) +class Person: + name: str + age: int + preferred_operating_systems: List[str] + + +@dataclass(frozen=True) +class Laptop: + id: int + manufacturer: str + model: str + screen_size_in_inches: float + operating_system: str + + +def find_possible_laptops(laptops: List[Laptop], person: Person) -> List[Laptop]: + possible_laptops = [] + for laptop in laptops: + if laptop.operating_system in person.preferred_operating_systems: + possible_laptops.append(laptop) + return possible_laptops + + +people = [ + Person(name="Imran", age=22, preferred_operating_systems=["Ubuntu"]), + Person(name="Eliza", age=34, preferred_operating_systems=["Arch Linux"]), +] + +laptops = [ + Laptop(id=1, manufacturer="Dell", model="XPS", screen_size_in_inches=13, operating_system="Arch Linux"), + Laptop(id=2, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="Ubuntu"), + Laptop(id=3, manufacturer="Dell", model="XPS", screen_size_in_inches=15, operating_system="ubuntu"), + Laptop(id=4, manufacturer="Apple", model="macBook", screen_size_in_inches=13, operating_system="macOS"), +] + +for person in people: + possible_laptops = find_possible_laptops(laptops, person) + print(f"Possible laptops for {person.name}: {possible_laptops}") \ No newline at end of file