Skip to content

Commit 1f8dc1c

Browse files
committed
Chapter 5
1 parent 9e0ce01 commit 1f8dc1c

16 files changed

Lines changed: 192 additions & 0 deletions

Ch5/class_attributes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Nutrimatic:
2+
output = "Something almost, but not quite, entirely unlike tea."
3+
4+
def request(self, beverage):
5+
return self.output
6+
7+
8+
machine = Nutrimatic()
9+
mug = machine.request("Tea")
10+
print(mug)
11+
12+
print(machine.output)
13+
print(Nutrimatic.output)

Ch5/coercion.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print(42.5) # coerces to a string
2+
x = 5 + 1.5 # coerces to a float (6.5)
3+
y = 5 + True # coerces to an int (6)...and is also considered a bad idea

Ch5/conversion.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
life_universe_everything = "42"
2+
3+
answer = float(life_universe_everything)
4+
5+
print(type(answer))
6+
print(answer)

Ch5/duck_typing_feels_better.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def calculate_age(birth_year, current_year):
2+
return (current_year - birth_year)
3+
4+
5+
def calculate_third_age_year(current_age, current_year):
6+
return int(current_year - (current_age / 3))
7+
8+
9+
birth_year = "1985" # get from user, assume data validation
10+
birth_year = int(birth_year)
11+
12+
current_year = "2010" # get from system
13+
current_year = int(current_year)
14+
15+
current_age = calculate_age(birth_year, current_year)
16+
third_age_year = calculate_third_age_year(current_age, current_year)
17+
print(third_age_year)

Ch5/evils_of_system_hungarian.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def calculate_age(intBirthYear, intCurrentYear):
2+
intAge = intCurrentYear - intBirthYear
3+
return intAge
4+
5+
6+
def calculate_third_age_year(intCurrentAge, intCurrentYear):
7+
floatThirdAge = intCurrentAge / 3
8+
floatCurrentYear = float(intCurrentYear)
9+
floatThirdAgeYear = floatCurrentYear - floatThirdAge
10+
intThirdAgeYear = int(floatThirdAgeYear)
11+
return intThirdAgeYear
12+
13+
14+
strBirthYear = "1985" # get from user, assume data validation
15+
intBirthYear = int(strBirthYear)
16+
17+
strCurrentYear = "2010" # get from system
18+
intCurrentYear = int(strCurrentYear)
19+
20+
intCurrentAge = calculate_age(intBirthYear, intCurrentYear)
21+
intThirdAgeYear = calculate_third_age_year(intCurrentAge, intCurrentYear)
22+
print(intThirdAgeYear)

Ch5/global_scope.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
high_score = 10
2+
3+
4+
def score():
5+
global high_score
6+
new_score = 465 # SCORING LOGIC HERE
7+
if new_score > high_score:
8+
print("New high score")
9+
high_score = new_score
10+
11+
12+
score()
13+
print(high_score) # prints 465

Ch5/global_scope_gotcha.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
current_score = 0
2+
3+
4+
def score():
5+
global current_score
6+
new_score = 465 # SCORING LOGIC HERE
7+
current_score = new_score
8+
9+
10+
score()
11+
print(current_score) # prints 465

Ch5/immutable_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eggs = 12
2+
carton = eggs
3+
print(eggs is carton) # prints True
4+
eggs += 1
5+
print(eggs is carton) # prints False
6+
print(eggs) # prints 13
7+
print(carton) # prints 12

Ch5/local_scope.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def spam():
2+
message = "Spam"
3+
word = "spam"
4+
for _ in range(100):
5+
separator = ", "
6+
message = separator + word
7+
message += separator
8+
message += "spam!"
9+
10+
return message
11+
12+
13+
# print(message) # NameError: name 'message' is not defined
14+
15+
output = spam()
16+
print(output)

Ch5/lowest_temp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def find_lowest(temperatures):
2+
# temperatures.sort()
3+
# print(temperatures[0])
4+
sorted_temps = sorted(temperatures) # sorted returns a new list
5+
print(sorted_temps[0])
6+
7+
8+
temps = [85, 76, 79, 72, 81]
9+
find_lowest(temps)
10+
print(temps)

0 commit comments

Comments
 (0)