diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..3cfd4ea1b 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,8 @@ # bounce.py # # Exercise 1.5 + +height = 100 +for i in range(10): + height = height * 3 / 5 + print(round(height,4)) diff --git a/Work/learnings.md b/Work/learnings.md new file mode 100644 index 000000000..91b50500a --- /dev/null +++ b/Work/learnings.md @@ -0,0 +1,29 @@ +# Learnings + +These are certain *miscellaneous* notes on python. The aim is not to capture everything learned in the course, but to note down certain curious aspects that I wouldn't notice or remember otherwise. + +## if...elif + +Like bash, python uses `elif`, not `else if`. + +```python +if a > 10: + print("big") +elif a < 5: + print("not so") +else: + pass +``` + +## division, integer division + +python (atleast recent versions) divides numbers correctly and produces floats even when dividing integers. `10/3` produces `3.33333`. Unlike in some other languages, we don't have to pass it as `10.0/3` to get the correct result. This makes sense since a dynamically typed language like python should be transparent about the number types. It's justified if a statically typed language returns a rounded (floored) int as a result of division, and the developer is responsible to know that, but not in a dynamically typed language. + +That said, python also has a *Floor Divide* operator `//` + +``` +>>> 10/3 +3.3333333333333335 +>>> 10//3 +3 +``` diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..0faa1c997 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,37 @@ # mortgage.py # -# Exercise 1.7 +# Exercise 1.11 + +principal = 500_000.0 +rate = 0.05 +regular_payment = 2684.11 + +# months are counted from 1, i.e., the first payment happens in month 1 +extra_payment_start_month = 60 # inclusive +extra_payment_end_month = 108 # also inclusive. but why? +extra_payment = 1000 +total_paid = 0.0 + +# this still has to count from zero +months = 0 +while principal > 0: + # We increment months at the beginning so the logic looks natural. + # Think that the month has arrived, then we proceed with payment. + months = months + 1 + principal = principal * (1+rate/12) + + if months >= extra_payment_start_month and months <= extra_payment_end_month: + payment = regular_payment + extra_payment + else: + payment = regular_payment + + if principal < payment: + payment = principal + + principal = principal - payment + total_paid = total_paid + payment + + print(months, round(total_paid, 2), round(principal,2)) + +print('Total paid', total_paid) +print('Months', months) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..3f3378639 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,13 @@ # pcost.py # # Exercise 1.27 + +f = open('Data/portfolio.csv', 'rt') +headers = next(f) + +total_cost = 0 +for line in f: + row = line.strip().split(',') + total_cost = total_cost + int(row[1]) * float(row[2]) + +print('Cost of the portfolio is ', total_cost) diff --git a/Work/sears.py b/Work/sears.py new file mode 100644 index 000000000..ee061ae6c --- /dev/null +++ b/Work/sears.py @@ -0,0 +1,13 @@ +bill_thickness = 0.11 * 0.001 # Meters (0.11 mm) +sears_height = 442 # Height (meters) +num_bills = 1 +day = 1 + +while num_bills * bill_thickness < sears_height: + print(day, num_bills, num_bills * bill_thickness) + day = day + 1 + num_bills = num_bills * 2 + +print('Number of days', day) +print('Number of bills', num_bills) +print('Final height', num_bills * bill_thickness) diff --git a/Work/unicode.py b/Work/unicode.py new file mode 100644 index 000000000..1d9e496b9 --- /dev/null +++ b/Work/unicode.py @@ -0,0 +1,13 @@ +# In python 3 all strings are unicode. +# We can enter not-easily-typable unicode chars in a variety of ways +print('Using unicode chars in python') +print('* Use slash-u followed by unicode number/code') +print('\t', '\\u0101 =>', '\u0101') +print('\t', 'S\\u0101dhakam =>', 'S\u0101dhakam') +print('\t', '\\u00bd =>', '\u00bd') +print('\t', 'or, \\xbd =>', '\xbd') +print() +print("* Use the charactor's name with \\N") +print('\t', "\\N{TAMIL FRACTION ONE QUARTER} =>", "\N{TAMIL FRACTION ONE QUARTER}") +print('\t', "\\N{VULGAR FRACTION ONE HALF} =>", "\N{VULGAR FRACTION ONE HALF}") +