Added Homework 04 - Strings
This commit is contained in:
parent
aad4e226c5
commit
be356ccb59
24 changed files with 315 additions and 0 deletions
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue