diff --git a/homework/cse160-hw04-homework04.zip b/homework/cse160-hw04-homework04.zip new file mode 100644 index 0000000..39cd581 Binary files /dev/null and b/homework/cse160-hw04-homework04.zip differ diff --git a/homework/homework04/documents/2022-02-10-Documentation-on-f-strings-Updated.pdf b/homework/homework04/documents/2022-02-10-Documentation-on-f-strings-Updated.pdf new file mode 100644 index 0000000..ba99d6f Binary files /dev/null and b/homework/homework04/documents/2022-02-10-Documentation-on-f-strings-Updated.pdf differ diff --git a/homework/homework04/fstrings/fstrings01.py b/homework/homework04/fstrings/fstrings01.py new file mode 100644 index 0000000..7feb41d --- /dev/null +++ b/homework/homework04/fstrings/fstrings01.py @@ -0,0 +1,15 @@ +import math +variable = math.pi + +print(f"Using Numeric {variable = }") +print(f"|{variable:25}|") +print(f"|{variable:<25}|") +print(f"|{variable:>25}|") +print(f"|{variable:^25}|\n") + +variable = "Python 3.9" +print(f"Using String {variable = }") +print(f"|{variable:25}|") +print(f"|{variable:<25}|") +print(f"|{variable:>25}|") +print(f"|{variable:^25}|") diff --git a/homework/homework04/fstrings/fstrings02.py b/homework/homework04/fstrings/fstrings02.py new file mode 100644 index 0000000..6cfd40f --- /dev/null +++ b/homework/homework04/fstrings/fstrings02.py @@ -0,0 +1,13 @@ +import math +variable = math.pi + +print(f"Using String {variable = }") +print(f"|{variable:=<25}|") +print(f"|{variable:=>25}|") +print(f"|{variable:=^25}|\n") + +variable = "Python 3.9" +print(f"Using String {variable = }") +print(f"|{variable:=<25}|") +print(f"|{variable:=>25}|") +print(f"|{variable:=^25}|") diff --git a/homework/homework04/fstrings/fstrings03.py b/homework/homework04/fstrings/fstrings03.py new file mode 100644 index 0000000..9c062fd --- /dev/null +++ b/homework/homework04/fstrings/fstrings03.py @@ -0,0 +1,14 @@ +import math + +variable = 10 +print(f"Using Numeric {variable = }") +print(f"This prints without formatting {variable}") +print(f"This prints with formatting {variable:d}") +print(f"This prints also with formatting {variable:n}") +print(f"This prints with spacing {variable:10d}\n") + +variable = math.pi +print(f"Using Numeric {variable = }") +print(f"This prints without formatting {variable}") +print(f"This prints with formatting {variable:f}") +print(f"This prints with spacing {variable:20f}") diff --git a/homework/homework04/fstrings/fstrings04.py b/homework/homework04/fstrings/fstrings04.py new file mode 100644 index 0000000..bd49029 --- /dev/null +++ b/homework/homework04/fstrings/fstrings04.py @@ -0,0 +1,11 @@ +variable = 1200356.8796 + +print(f"Using Numeric {variable = }") +print(f"With two decimal places: {variable:.2f}") +print(f"With four decimal places: {variable:.3f}") +print(f"With two decimal places and a comma: {variable:,.2f}") +print(f"With a forced plus sign: {variable:+.2f}\n") + +variable *= -1 +print(f"Using Numeric {variable = }") +print(f"With two decimal places and a comma: {variable:,.2f}") diff --git a/homework/homework04/fstrings/fstrings05.py b/homework/homework04/fstrings/fstrings05.py new file mode 100644 index 0000000..333d26a --- /dev/null +++ b/homework/homework04/fstrings/fstrings05.py @@ -0,0 +1,4 @@ +print(f'Number\t\tSquare\t\tCube') + +for x in range(1, 11): + print(f'{x:2d}\t\t\t{x*x:3d}\t\t\t{x*x*x:4d}') diff --git a/homework/homework04/fstrings/fstrings06.py b/homework/homework04/fstrings/fstrings06.py new file mode 100644 index 0000000..c2fee1f --- /dev/null +++ b/homework/homework04/fstrings/fstrings06.py @@ -0,0 +1,5 @@ +print(f'Number\t\tSquare\t\t\tCube') + +for x in range(1, 11): + x = float(x) + print(f'{x:5.2f}\t\t{x*x:6.2f}\t\t{x*x*x:8.2f}') diff --git a/homework/homework04/fstrings/fstrings07.py b/homework/homework04/fstrings/fstrings07.py new file mode 100644 index 0000000..7755c27 --- /dev/null +++ b/homework/homework04/fstrings/fstrings07.py @@ -0,0 +1,22 @@ +APPLES = .50 +BREAD = 1.50 +CHEESE = 2.25 + +numApples = 3 +numBread = 10 +numCheese = 6 + +prcApples = numApples * APPLES +prcBread = numBread* BREAD +prcCheese = numCheese * CHEESE +strApples = 'Apples' +strBread = 'Rye Bread' +strCheese = 'Cheese' +total = prcBread + prcCheese + prcApples + +print(f'{"My Grocery List":^30s}') +print(f'{"="*30}') +print(f'{strApples:10s}{numApples:10d}\t${prcApples:>5.2f}') +print(f'{strBread:10s}{numBread:10d}\t${prcBread:>5.2f}') +print(f'{strCheese:10s}{numCheese:10d}\t${prcCheese:>5.2f}') +print(f'{"Total:":>20s}\t${total:>5.2f}') diff --git a/homework/homework04/fstrings/fstrings08.py b/homework/homework04/fstrings/fstrings08.py new file mode 100644 index 0000000..667768f --- /dev/null +++ b/homework/homework04/fstrings/fstrings08.py @@ -0,0 +1,9 @@ +import math + +goldenRatio = (1+math.sqrt(5))/2 +print(f"{goldenRatio=}") + +print("\n") + +print(f"{goldenRatio = :.6f}") +print(f"{(1+math.sqrt(5))/2 = :.6f}") diff --git a/homework/homework04/fstrings/program01.py b/homework/homework04/fstrings/program01.py new file mode 100644 index 0000000..5264e16 --- /dev/null +++ b/homework/homework04/fstrings/program01.py @@ -0,0 +1,7 @@ + +val1 = float(input("Enter a value for val1 ")) +val2 = float(input("Enter a value for val2 ")) + +# using the addition operator +res = val1 + val2 +print("The sum is ",res) diff --git a/homework/homework04/input/input01.py b/homework/homework04/input/input01.py new file mode 100644 index 0000000..96cbbae --- /dev/null +++ b/homework/homework04/input/input01.py @@ -0,0 +1,5 @@ + +data1 = input("Enter a value for data1: ") + +print(f"data1 = {data1}, The type of data1 is {type(data1)}") + diff --git a/homework/homework04/input/input02.py b/homework/homework04/input/input02.py new file mode 100644 index 0000000..c4ffa8b --- /dev/null +++ b/homework/homework04/input/input02.py @@ -0,0 +1,8 @@ + +print("This program converts the input to an integer\n") +data1 = input("Enter an integer value for data1: ") + +print(f"The type of data1 is {type(data1)} at input") +data1 = int(data1) +print(f"The type of data1 is {type(data1)} after conversion to int") +print(f"The value of data1 is {data1}") diff --git a/homework/homework04/input/input03.py b/homework/homework04/input/input03.py new file mode 100644 index 0000000..6a0a8ac --- /dev/null +++ b/homework/homework04/input/input03.py @@ -0,0 +1,8 @@ + +print("This program converts the input to a float\n") +data1 = input("Enter an float value for data1: ") + +print(f"The type of data1 is {type(data1)} at input") +data1 = float(data1) +print(f"The type of data1 is {type(data1)} after conversion to float") +print(f"The value of data1 is {data1}") diff --git a/homework/homework04/input/rectangle01.py b/homework/homework04/input/rectangle01.py new file mode 100644 index 0000000..b6b9c3a --- /dev/null +++ b/homework/homework04/input/rectangle01.py @@ -0,0 +1,8 @@ + +length = 4 +width = 3 + +area = length * width + +print(f"The area is {area}") + diff --git a/homework/homework04/input/rectangle02.py b/homework/homework04/input/rectangle02.py new file mode 100644 index 0000000..e2a247f --- /dev/null +++ b/homework/homework04/input/rectangle02.py @@ -0,0 +1,7 @@ + +length = float(input("Enter a value for the length: ")) +width = float(input("Enter a value for the width: ")) + +area = length * width + +print(f"The area is {area}") diff --git a/homework/homework04/results/autoloan01.py b/homework/homework04/results/autoloan01.py new file mode 100644 index 0000000..f062eef --- /dev/null +++ b/homework/homework04/results/autoloan01.py @@ -0,0 +1,49 @@ + +''' +Auto Loan Payment Calculations +============================== + +https://www.wikihow.com/Calculate-Auto-Loan-Payments + Use the formula A = P ∗ ( r ( 1 + r ) n ) / ( ( 1 + r ) n − 1 ) + + A = the monthly payment. + P = the principal + r = the interest rate per month, which equals the annual interest rate divided by 12 + n = the total number of months + +Mass Sales Tax +============== + +The .0625 (6.25%) sales tax is paid in the first year only. + +https://www.mass.gov/guides/motor-vehicle-and-trailer-sales-and-use-tax + +Mass Excise Tax Fees +==================== + +The tax depends on the vehicle age. For this program we only calculate the +excise tax on the first year of purchase. +https://www.sec.state.ma.us/divisions/cis/tax/motor-excise.htm + +''' + +# ======================= Input Variables ================================== +principal = 15090. +rate = .07 +numberOfYears = 4 +# =========================================================================== + +rate = rate /100 +ratePerMonth = rate /12 +numberOfMonths = numberOfYears * 12 +salesTax = principal * .0625 # Mass sales tax rate +# https://www.sec.state.ma.us/divisions/cis/tax/motor-excise.htm +exciseTax = principal / 1000 * .9 * 25.00 + +monthlyPayment = principal * (ratePerMonth * (1+ratePerMonth)**(numberOfMonths))/((1+ratePerMonth)**(numberOfMonths)-1) + +print("The payment is ", monthlyPayment) +print("The sales tax is ", salesTax) +print("The excise tax is ", exciseTax) + +print("First years expense is ", monthlyPayment*12 + salesTax + exciseTax) diff --git a/homework/homework04/results/rectangles001.py b/homework/homework04/results/rectangles001.py new file mode 100644 index 0000000..3dbdf80 --- /dev/null +++ b/homework/homework04/results/rectangles001.py @@ -0,0 +1,14 @@ +length = 4 +width = 3 + +# The next line is for the column headers +print("length width area") + +# The next two lines generate multiple lengths and widths. +for length in range(1, 3): + for width in range(2, 6): + # The next two lines calculate the area then print the values. + # Modify the print statement to line up the columns aligned right. + area = length * width + print(f"{length} {width} {area}") + diff --git a/homework/homework04/strings/stringConstants01.py b/homework/homework04/strings/stringConstants01.py new file mode 100644 index 0000000..577643e --- /dev/null +++ b/homework/homework04/strings/stringConstants01.py @@ -0,0 +1,18 @@ +import string + +# See https://docs.python.org/3/library/string.html +# These are some pre-defined character variables you can use. + +print(f"string.ascii_letters = {string.ascii_letters}") +print(f"string.ascii_lowercase = {string.ascii_lowercase}") +print(f"string.ascii_uppercase = {string.ascii_uppercase}") +print(f"string.digits = {string.digits}") +print(f"string.hexdigits = {string.hexdigits}") +print(f"string.octdigits = {string.octdigits}") +print(f"string.punctuation = {string.punctuation}") +print(f"string.printable = {string.printable}") +print(f"string.whitespace = {string.whitespace}") +print(f"length of string.whitespace = {len(string.whitespace)}") + + + diff --git a/homework/homework04/strings/stringToCharacters01.py b/homework/homework04/strings/stringToCharacters01.py new file mode 100644 index 0000000..0125d84 --- /dev/null +++ b/homework/homework04/strings/stringToCharacters01.py @@ -0,0 +1,17 @@ + +import string + +message = ''' +This program prints the Ascii codes for all the letters. +Notice the lower case letters are represented by larger +integers than the upper case letters. +''' + +print(message) + +# Load the string variable +stringOfLetters = string.ascii_letters + + +for myCharacter in stringOfLetters: + print(f"{myCharacter:2s}{ord(myCharacter):3d}") \ No newline at end of file diff --git a/homework/homework04/strings/stringToCharacters02.py b/homework/homework04/strings/stringToCharacters02.py new file mode 100644 index 0000000..530cec2 --- /dev/null +++ b/homework/homework04/strings/stringToCharacters02.py @@ -0,0 +1,15 @@ + +import string +message = ''' +This program prints the Ascii codes for all the letters +in a text string entered by the user. +''' + +print(message) + +stringOfLetters = input("Enter a name or phrase: ") + +# Note: The ord() function returns the integer code for a character. + +for myCharacter in stringOfLetters: + print(f"{myCharacter:2s}{ord(myCharacter):3d}") \ No newline at end of file diff --git a/homework/homework04/strings/stringToCharacters03.py b/homework/homework04/strings/stringToCharacters03.py new file mode 100644 index 0000000..ba57f66 --- /dev/null +++ b/homework/homework04/strings/stringToCharacters03.py @@ -0,0 +1,31 @@ +import string + +message = ''' +This program prints the Ascii codes for the whitespace. +characters. These characters include the space, line return, tabs, etc. +Printing the whitespace string does not appear to generate any results. +Printing each character code is informative. +''' +message2 = ''' +Open a web browser. Go to https://www.ascii-code.com. +Look up each of the codes from this program to determine +their purpose. +For example: 32 = space +Look up the rest of the whitespace codes. +''' + +print(message) + + +print(f"string.whitespace = {string.whitespace}") +print(f"length of string.whitespace = {len(string.whitespace)}") +# Since you cannot easily see the effect of whitespace characters, +# we can print their numeric codes. + +# Convert the string to a list of letters in the string. +whiteSpaceCharactersList = list(string.whitespace) + +for myCharacter in whiteSpaceCharactersList: + print(f"{ord(myCharacter)}") + +print(message2) diff --git a/homework/homework04/strings/stringToEmojis01.py b/homework/homework04/strings/stringToEmojis01.py new file mode 100644 index 0000000..4b33726 --- /dev/null +++ b/homework/homework04/strings/stringToEmojis01.py @@ -0,0 +1,21 @@ + +# Example from https://www.geeksforgeeks.org/python-program-to-print-emojis/ + +print("grinning face ",end="") +print("\U0001f600") + +print("grinning squinting face ",end="") +print("\U0001F606") + +print("rolling on the floor laughing ",end="") +print("\U0001F923") + +print("grinning face ",end="") +print("\N{grinning face}") + +print("slightly smiling face ",end="") +print("\N{slightly smiling face}") + +print("winking face ",end="") +print("\N{winking face}") + diff --git a/homework/homework04/strings/stringToEmojis02.py b/homework/homework04/strings/stringToEmojis02.py new file mode 100644 index 0000000..c84ce4e --- /dev/null +++ b/homework/homework04/strings/stringToEmojis02.py @@ -0,0 +1,14 @@ + +message = ''' +This program prints a list of emojis by character code. +This just shows the wide range of emoji's you can +use in your programs. +''' + +for i in range(127744,128360): + print(chr(i),end=" ") + +print("Next Set") + +for i in range(128507,128709): + print(chr(i),end=" ") \ No newline at end of file