Skip to content

Commit 9937825

Browse files
committed
complete type checking and classes exercises.
1 parent 0b0eefb commit 9937825

6 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Person:
2+
def __init__(
3+
self,
4+
name: str,
5+
age: int,
6+
preferred_operating_system: str
7+
):
8+
self.name = name
9+
self.age = age
10+
self.preferred_operating_system = preferred_operating_system
11+
12+
13+
def is_adult(person: Person) -> bool:
14+
return person.age >= 18
15+
16+
17+
def get_address(person: Person) -> str:
18+
return person.address
19+
20+
21+
imran = Person("Imran", 22, "Ubuntu")
22+
23+
print(imran.name)
24+
print(is_adult(imran))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Better organisation — behaviour related to a class stays together with that class.
2+
3+
# Easier to discover — if I have a Person, I can look at the Person class to see what it can do.
4+
5+
# More readable — person.is_adult() clearly shows that is_adult is behaviour associated with a Person.
6+
7+
# Encapsulation — other code doesn't need to know the internal details of how Person calculates something.
8+
9+
# Easier maintenance/refactoring — if the internal representation changes, such as changing age to date_of_birth, we can update the method while callers can continue using person.is_adult().
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Person:
2+
def __init__(self, name: str, age: int, preferred_operating_system: str):
3+
self.name = name
4+
self.age = age
5+
self.preferred_operating_system = preferred_operating_system
6+
7+
imran = Person("Imran", 22, "Ubuntu")
8+
print(imran.name)
9+
print(imran.address)
10+
11+
eliza = Person("Eliza", 34, "Arch Linux")
12+
print(eliza.name)
13+
print(eliza.address)
14+
15+
#The error is that the code tries to access an attribute called address, but the Person class does not define an address attribute.
16+
#Mypy knows that imran and eliza are Person objects, so it can detect that .address does not exist before we run the program.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Read the above code and write down what the bug is. How would you fix it?
2+
3+
def double(number):
4+
# return number * 3
5+
return number * 2 # I would fix it like this
6+
7+
print(double(10))
8+
9+
#Because double function is expected to double or multiply a number by two but in contrast it is multiplying by 3.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Predict what double("22") will do. Then run the code and check. Did it do what you expected? Why did it return the value it did?
2+
def half(value):
3+
return value / 2
4+
5+
def double(value):
6+
return value * 2
7+
8+
def second(value):
9+
return value[1]
10+
11+
print(double(22))
12+
13+
#So I predicted the string would be doubled(repeated) --> "2222"
14+
#As in Python, multiplying a string by an integer means repeat the string
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def open_account(
2+
balances: dict[str, int],
3+
name: str,
4+
amount: int,
5+
) -> None:
6+
balances[name] = amount
7+
8+
def sum_balances(accounts: dict[str, int]) -> int:
9+
total = 0
10+
for name, pence in accounts.items():
11+
print(f"{name} had balance {pence}")
12+
total += pence
13+
return total
14+
15+
def format_pence_as_string(total_pence: int) -> str:
16+
if total_pence < 100:
17+
return f"{total_pence}p"
18+
19+
pounds = int(total_pence / 100)
20+
pence = total_pence % 100
21+
22+
return f"£{pounds}.{pence:02d}"
23+
24+
balances = {
25+
"Sima": 700,
26+
"Linn": 545,
27+
"Georg": 831,
28+
}
29+
30+
open_account(balances, "Tobi", 913)
31+
open_account(balances, "Olya", 713)
32+
33+
total_pence = sum_balances(balances)
34+
total_string = format_pence_as_string(total_pence)
35+
36+
print(f"The bank accounts total {total_string}")
37+
38+

0 commit comments

Comments
 (0)