Added lists code

main
Edward Bigos 2025-03-18 10:21:01 -04:00
parent a9b392d3db
commit 33badb8c97
13 changed files with 265 additions and 0 deletions

28
lists/exciseTax01.py Normal file
View File

@ -0,0 +1,28 @@
message01 = '''
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
Vehicle Age Percentage of List Price
In the year preceding the model year
(brand new car released before model year) 50%
In the model year 90%
In the second year 60%
In the third year 40%
In the fourth year 25%
In the fifth and succeeding years 10%
'''
print(message01)
# Tax table for excise tax by year.
exciseTaxRateTable = [.5, .9, .6, .4, .25, .10, .10, .10, .10, .10]
year = -1 # Indicates a date prior to the model year; i.e you bought a model year 2024 in year 2023.
for exciseTaxRate in exciseTaxRateTable:
print(f"{year:2d} {exciseTaxRate:.2f} {exciseTaxRate:.0%}")
year += 1 # Shorthand for year = year + 1
print("Access excise tax rate by index")

31
lists/exciseTax02.py Normal file
View File

@ -0,0 +1,31 @@
message01 = '''
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
Vehicle Age Percentage of List Price
In the year preceding the model year
(brand new car released before model year) 50%
In the model year 90%
In the second year 60%
In the third year 40%
In the fourth year 25%
In the fifth and succeeding years 10%
'''
print(message01)
# Tax table for excise tax by year.
exciseTaxRateTable = [.5, .9, .6, .4, .25, .10, .10, .10, .10, .10]
print("Access excise tax rate by index")
yearIndex = 2
print(f"year = {yearIndex} rate = {exciseTaxRateTable[yearIndex]}")
yearIndex = 4
print(f"year = {yearIndex} rate = {exciseTaxRateTable[yearIndex]}")
yearIndex = 7 # set to 9 or more and the program will fault
print(f"year = {yearIndex} rate = {exciseTaxRateTable[yearIndex]}")

30
lists/exciseTax03.py Normal file
View File

@ -0,0 +1,30 @@
message01 = '''
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
Vehicle Age Percentage of List Price
In the year preceding the model year
(brand new car released before model year) 50%
In the model year 90%
In the second year 60%
In the third year 40%
In the fourth year 25%
In the fifth and succeeding years 10%
'''
print(message01)
# Tax table for excise tax by year.
exciseTaxRateTable = [.5, .9, .6, .4, .25, .10, .10, .10, .10, .10]
yearIndex = -1 # Indicates a date prior to the model year; i.e you bought a model year 2024 in year 2023.
# Change the 8 to 10. The program will fault.
while yearIndex <= 8:
print(f"{yearIndex:2d} {exciseTaxRateTable[yearIndex]}")
yearIndex += 1 # Shorthand for yearIndex = yearIndex + 1

16
lists/grades01.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/python3
# grades01
# List element manipulation.
scores = [75, 95, 90, 87, 99, 100, 97]
# This is unnecessary and just here for debugging.
print(scores)
# We will sum the scores. Initialize sum value to zero then add each value to the sum.
sum = 0
for score in scores:
sum = sum + score
print("The sum of the seven scores is ",sum)
print("The average of the seven scores is ",sum/7)

15
lists/grades02.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/python3
# grades02
# List element manipulation.
scores = [75, 95, 90, 87, 99, 100, 97, 100]
numberOfScores = len(scores)
# We will sum the scores. Initialize sum value to zero then add each value to the sum.
sum = 0
for score in scores:
sum = sum + score
print(f"The sum of the {numberOfScores} scores is {sum:.2f}")
print(f"The average of the {numberOfScores} scores is {sum/numberOfScores:.2f}")

23
lists/grades03.py Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/python3
# grades03
# List element manipulation.
numberOfScores = int(input("How many scores do you want to enter? "))
counter = 0 # Start counter at 0
scores = [] # Create an empty list
while(counter < numberOfScores):
nextScore = float(input("Enter the next value: "))
scores.append(nextScore)
counter += 1
# This is unnecessary and just here for debugging.
print(scores)
# We will sum the scores. Initialize sum value to zero then add each value to the sum.
sum = 0
for score in scores:
sum = sum + score
print(f"The sum of the {numberOfScores} scores is {sum:.2f}")
print(f"The average of the {numberOfScores} scores is {sum/numberOfScores:.2f}")

17
lists/list01.py Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/python3
# Program: list01
# Defining and printing list lengths.
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Lists of numbers. These could be integers or floats.
intList = [7, 2, 12, 18, 16, 21]
# Print the list and the length
print("Print the length of the lists.")
print (dogNames," length = ",len(dogNames))
print(intList," length = ",len(intList))

21
lists/list02.py Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/python3
# Program: list02
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
firstNames = ["edward","david", "Bob","mary","alice"]
# Print the list and the length
print("Print the length of the lists.")
print (dogNames," length = ",len(dogNames))
print(firstNames," length = ",len(firstNames))
print("\n")
# Another way to think of the for loop is to read it as
# "Select the next element from the list, put it into the variable
# item, the do whatever you like with that one value in item".
# The for loop ends when the entire list has been processed.
for item in dogNames:
print(item)

11
lists/list03.py Normal file
View File

@ -0,0 +1,11 @@
#!/usr/bin/python3
# Program: list03
# Run this code in the visualizer in a web browser at
# https://pythontutor.com/visualize.html#mode=edit
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy", "Gimli"]
for item in dogNames:
print(item)

16
lists/list04.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/python3
# Program: list04
# Lists of text values
dogNames = [ "Harry", "Shenanigans","Katrina","Tanner","Yukon Jack", "Colorado","Mandy"]
# Print the list and the length
print("Print the length of the list.")
print (dogNames," length = ",len(dogNames),"Type =",type(dogNames))
print("\n")
for item in dogNames:
print(f"{item:14s} {type(item)}")
print("\nDone!")

15
lists/range01.py Normal file
View File

@ -0,0 +1,15 @@
# This code is based on: https://www.w3schools.com/python/ref_func_range.asp
x = range(3, 6)
print(f"x = {x}, type = {type(x)}")
# Another way to think of the for loop is to read it as
# "Select the next element from the list or range, put it into the variable
# item, the do whatever you like with that one value in item".
# The for loop ends when the entire list has been processed.
for item in x:
print(item)

21
lists/range02.py Normal file
View File

@ -0,0 +1,21 @@
# This code is based on: https://www.w3schools.com/python/ref_func_range.asp
x = range(3, 6)
# A range can be converted to a list.
y = list(x)
# Note:
# What is the difference betweeen using range() and an explicit list?
# The main difference is that range calculates the value on the fly. This saves memory
# at the expense of processing speed. The list has each object stored in memory. This saves
# the processing time at the expense of memory usage.
print(f"x = {x}, type = {type(x)}")
for item in x:
print(item)
print(f"y = {y}, type = {type(y)}")
for item in y:
print(item)

21
lists/range03.py Normal file
View File

@ -0,0 +1,21 @@
# This code is based on: https://www.w3schools.com/python/ref_func_range.asp
# Create a range from 2 to 10 with steps of two
x = range(2, 10, 2)
y = list(x)
# Note:
# What is the difference betweeen using range() and an explicit list?
# The main difference is that range calculates the value on the fly. This saves memory
# at the expense of processing speed. The list has each object stored in memory. This saves
# the processing time at the expense of memory usage.
print(f"x = {x}, type = {type(x)}, length = {len(x)}")
for item in x:
print(item)
print(f"y = {y}, type = {type(y)}, length = {len(y)}")
for item in y:
print(item)