Added Homework 04 - Strings
This commit is contained in:
parent
aad4e226c5
commit
be356ccb59
24 changed files with 315 additions and 0 deletions
BIN
homework/cse160-hw04-homework04.zip
Normal file
BIN
homework/cse160-hw04-homework04.zip
Normal file
Binary file not shown.
Binary file not shown.
15
homework/homework04/fstrings/fstrings01.py
Normal file
15
homework/homework04/fstrings/fstrings01.py
Normal file
|
|
@ -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}|")
|
||||||
13
homework/homework04/fstrings/fstrings02.py
Normal file
13
homework/homework04/fstrings/fstrings02.py
Normal file
|
|
@ -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}|")
|
||||||
14
homework/homework04/fstrings/fstrings03.py
Normal file
14
homework/homework04/fstrings/fstrings03.py
Normal file
|
|
@ -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}")
|
||||||
11
homework/homework04/fstrings/fstrings04.py
Normal file
11
homework/homework04/fstrings/fstrings04.py
Normal file
|
|
@ -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}")
|
||||||
4
homework/homework04/fstrings/fstrings05.py
Normal file
4
homework/homework04/fstrings/fstrings05.py
Normal file
|
|
@ -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}')
|
||||||
5
homework/homework04/fstrings/fstrings06.py
Normal file
5
homework/homework04/fstrings/fstrings06.py
Normal file
|
|
@ -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}')
|
||||||
22
homework/homework04/fstrings/fstrings07.py
Normal file
22
homework/homework04/fstrings/fstrings07.py
Normal file
|
|
@ -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}')
|
||||||
9
homework/homework04/fstrings/fstrings08.py
Normal file
9
homework/homework04/fstrings/fstrings08.py
Normal file
|
|
@ -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}")
|
||||||
7
homework/homework04/fstrings/program01.py
Normal file
7
homework/homework04/fstrings/program01.py
Normal file
|
|
@ -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)
|
||||||
5
homework/homework04/input/input01.py
Normal file
5
homework/homework04/input/input01.py
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
|
||||||
|
data1 = input("Enter a value for data1: ")
|
||||||
|
|
||||||
|
print(f"data1 = {data1}, The type of data1 is {type(data1)}")
|
||||||
|
|
||||||
8
homework/homework04/input/input02.py
Normal file
8
homework/homework04/input/input02.py
Normal file
|
|
@ -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}")
|
||||||
8
homework/homework04/input/input03.py
Normal file
8
homework/homework04/input/input03.py
Normal file
|
|
@ -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}")
|
||||||
8
homework/homework04/input/rectangle01.py
Normal file
8
homework/homework04/input/rectangle01.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
length = 4
|
||||||
|
width = 3
|
||||||
|
|
||||||
|
area = length * width
|
||||||
|
|
||||||
|
print(f"The area is {area}")
|
||||||
|
|
||||||
7
homework/homework04/input/rectangle02.py
Normal file
7
homework/homework04/input/rectangle02.py
Normal file
|
|
@ -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}")
|
||||||
49
homework/homework04/results/autoloan01.py
Normal file
49
homework/homework04/results/autoloan01.py
Normal file
|
|
@ -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)
|
||||||
14
homework/homework04/results/rectangles001.py
Normal file
14
homework/homework04/results/rectangles001.py
Normal file
|
|
@ -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}")
|
||||||
|
|
||||||
18
homework/homework04/strings/stringConstants01.py
Normal file
18
homework/homework04/strings/stringConstants01.py
Normal file
|
|
@ -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)}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
17
homework/homework04/strings/stringToCharacters01.py
Normal file
17
homework/homework04/strings/stringToCharacters01.py
Normal file
|
|
@ -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}")
|
||||||
15
homework/homework04/strings/stringToCharacters02.py
Normal file
15
homework/homework04/strings/stringToCharacters02.py
Normal file
|
|
@ -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}")
|
||||||
31
homework/homework04/strings/stringToCharacters03.py
Normal file
31
homework/homework04/strings/stringToCharacters03.py
Normal file
|
|
@ -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)
|
||||||
21
homework/homework04/strings/stringToEmojis01.py
Normal file
21
homework/homework04/strings/stringToEmojis01.py
Normal file
|
|
@ -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}")
|
||||||
|
|
||||||
14
homework/homework04/strings/stringToEmojis02.py
Normal file
14
homework/homework04/strings/stringToEmojis02.py
Normal file
|
|
@ -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=" ")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue