diff --git a/sprint-five-prep/classes_objects.py b/sprint-five-prep/classes_objects.py new file mode 100644 index 000000000..84fed5977 --- /dev/null +++ b/sprint-five-prep/classes_objects.py @@ -0,0 +1,34 @@ +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 get_id(person: Person) -> int: + return person.id +# Exercise +# Save the above code to a file, and run it through mypy. +# Read the error, and make sure you understand what it’s telling you. +# Answer : "Person" has no attribute "address". both instances of "Person", and they dont have address att. + +# Exercise +# 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. + +# Answer : "Person" has no attribute "id". diff --git a/sprint-five-prep/first.py b/sprint-five-prep/first.py new file mode 100644 index 000000000..453b9c871 --- /dev/null +++ b/sprint-five-prep/first.py @@ -0,0 +1,21 @@ +# ✍️exercise +# Predict what double("22") will do. Then run the code and check. +# Did it do what you expected? no +# Why did it return the value it did? it just concatenated (repeated) the string. I expected it returns 44, but there is type issue. + + +def double(value): + return value * 2 + +print(double("22")) + +# --------------- +def double(number): + return number * 3 + +print(double(10)) + +# ✍️exercise +# Read the above code and write down what the bug is. How would you fix it? +# it will return 30, but the function name is double, so we have to change the function name to triple or change the return statement to number * 2. + diff --git a/sprint-five-prep/methods.py b/sprint-five-prep/methods.py new file mode 100644 index 000000000..6e6b4e9fe --- /dev/null +++ b/sprint-five-prep/methods.py @@ -0,0 +1,44 @@ +# ✍️exercise +# 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 +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") +eliza = Person("Eliza", date(1990, 8, 3), "Arch Linux") + +print(imran.is_adult()) +print(eliza.is_adult()) + + + + + + + + + +# ✍️exercise +# Think of the advantages of using methods instead of free functions. +# Write them down in your notebook. +# Encapsulation +# The class controls its own logic +# Example: You change from age → date_of_birth +# Only the class needs to change, External code still works:person.is_adult() +# Methods = behavior belongs to the object \ No newline at end of file diff --git a/sprint-five-prep/type_checking_mypy.py b/sprint-five-prep/type_checking_mypy.py new file mode 100644 index 000000000..b7d69856c --- /dev/null +++ b/sprint-five-prep/type_checking_mypy.py @@ -0,0 +1,38 @@ +# ✍️exercise +# Do not run the following code. +# This 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: 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, name="Tobi", amount=913) +open_account(balances, name="Olya", amount=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