From e376a083bec6cf3fcefbedd2c0effdc57359214a Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 09:27:37 +0530 Subject: [PATCH 01/11] ex 1.5 bouncing ball --- Work/bounce.py | 5 +++++ 1 file changed, 5 insertions(+) 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)) From 9fa646c8c3ef69c1f931a46e5fb890bda25c54f3 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 09:30:14 +0530 Subject: [PATCH 02/11] sears - a study in looping normally, I would try to find the math formula that answers this question. however, as this is a simple programming exercise, a while loop it is, as given by the instructor. --- Work/sears.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Work/sears.py 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) From a05ac7cc54bc1910e42baa85e384185db20b519c Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 09:45:06 +0530 Subject: [PATCH 03/11] learnings.md for taking notes --- Work/learnings.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Work/learnings.md diff --git a/Work/learnings.md b/Work/learnings.md new file mode 100644 index 000000000..48856126c --- /dev/null +++ b/Work/learnings.md @@ -0,0 +1,12 @@ +## if...elif + +Like bash, python uses `elif`, not `else if`. + +```python +if a > 10: + print("big") +elif a < 5: + print("not so") +else: + pass +``` From 67ff3b96c237451f5a74992b0ee6f089e4e1d166 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 09:46:02 +0530 Subject: [PATCH 04/11] ex 1.7 version of mortgage.py this is literally verbatim copy from the course --- Work/mortgage.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..41d490ea0 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,14 @@ # mortgage.py # # Exercise 1.7 + +principal = 500_000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 + +while principal > 0: + principal = principal * (1+rate/12) - payment + total_paid = total_paid + payment + +print('Total paid', total_paid) From 04ed212438ff04269e00fad3c0085bca136bde89 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 10:29:23 +0530 Subject: [PATCH 05/11] learning: division, integer division * added a note on python division and integer division * also described the pupose of learnings.md in it. --- Work/learnings.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Work/learnings.md b/Work/learnings.md index 48856126c..91b50500a 100644 --- a/Work/learnings.md +++ b/Work/learnings.md @@ -1,3 +1,7 @@ +# 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`. @@ -10,3 +14,16 @@ elif a < 5: 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 +``` From 0a9a40ffd1067b551a4e5188da41cf8e2c467f83 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 10:41:40 +0530 Subject: [PATCH 06/11] ex 1.8 version of morgage --- Work/mortgage.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 41d490ea0..acb13c7e2 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,14 +1,24 @@ # mortgage.py # -# Exercise 1.7 +# Exercise 1.8 principal = 500_000.0 rate = 0.05 -payment = 2684.11 +regular_payment = 2684.11 +extra = 1000 total_paid = 0.0 +months = 0 + while principal > 0: + if months < 12: + payment = regular_payment + extra + else: + payment = regular_payment + principal = principal * (1+rate/12) - payment total_paid = total_paid + payment + months = months + 1 print('Total paid', total_paid) +print('Months', months) From edc8901a08dbe8b3ec20a5e1a13c2e9f27264c1d Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 11:06:25 +0530 Subject: [PATCH 07/11] ex 1.9 version of mortgage --- Work/mortgage.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index acb13c7e2..9d8b558ba 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,24 +1,30 @@ # mortgage.py # -# Exercise 1.8 +# Exercise 1.9 principal = 500_000.0 rate = 0.05 regular_payment = 2684.11 -extra = 1000 + +# 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 # exclusive +extra_payment = 1000 total_paid = 0.0 +# this still has to count from zero months = 0 - while principal > 0: - if months < 12: - payment = regular_payment + extra + # 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 + if months >= extra_payment_start_month and months < extra_payment_end_month: + payment = regular_payment + extra_payment else: payment = regular_payment principal = principal * (1+rate/12) - payment total_paid = total_paid + payment - months = months + 1 print('Total paid', total_paid) print('Months', months) From b2288d634754e7095e1336b77bd0f59af8aa834f Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 11:17:08 +0530 Subject: [PATCH 08/11] ex 1.10 version of mortgage * also made the `extra_payment_end_month` inclusive. In the previous exercise this statement was given: > How much will Dave pay if he pays an extra $1000/month for 4 years starting in year 5 of the mortgage? along with a code snippet ``` extra_payment_start_month = 60 extra_payment_end_month = 108 ``` From these it's clear the start month is inclusive and end month is exclusive. However, made the end month inclusive to match the result shown in the course in this exercise. --- Work/mortgage.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 9d8b558ba..d896997c7 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,6 +1,6 @@ # mortgage.py # -# Exercise 1.9 +# Exercise 1.10 principal = 500_000.0 rate = 0.05 @@ -8,7 +8,7 @@ # 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 # exclusive +extra_payment_end_month = 108 # also inclusive. but why? extra_payment = 1000 total_paid = 0.0 @@ -18,13 +18,15 @@ # 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 - if months >= extra_payment_start_month and months < extra_payment_end_month: + if months >= extra_payment_start_month and months <= extra_payment_end_month: payment = regular_payment + extra_payment else: payment = regular_payment - principal = principal * (1+rate/12) - payment + principal = principal * (1+rate/12)- payment total_paid = total_paid + payment + print(months, round(total_paid, 2), round(principal,2)) + print('Total paid', total_paid) print('Months', months) From 75ab1e35f66165d77e7c0255b06e6415ea7f3ece Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 11:20:37 +0530 Subject: [PATCH 09/11] ex 1.11 of mortgage: fixed the over payment bug --- Work/mortgage.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index d896997c7..0faa1c997 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,6 +1,6 @@ # mortgage.py # -# Exercise 1.10 +# Exercise 1.11 principal = 500_000.0 rate = 0.05 @@ -18,12 +18,17 @@ # 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 * (1+rate/12)- payment + principal = principal - payment total_paid = total_paid + payment print(months, round(total_paid, 2), round(principal,2)) From 8369bc09751777b0be2e3bea6b8b93d43d2bbc28 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Sun, 31 May 2020 12:07:40 +0530 Subject: [PATCH 10/11] study in unicode characters --- Work/unicode.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Work/unicode.py 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}") + From b48e675fd31a94c581a427402551c6b092ff9047 Mon Sep 17 00:00:00 2001 From: Ananth P Date: Thu, 4 Jun 2020 20:28:10 +0530 Subject: [PATCH 11/11] ex 1.27 processing csv --- Work/pcost.py | 10 ++++++++++ 1 file changed, 10 insertions(+) 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)