From 33badb8c979649127adc7428c009d06606391f4c Mon Sep 17 00:00:00 2001 From: Edward Bigos Date: Tue, 18 Mar 2025 10:21:01 -0400 Subject: [PATCH] Added lists code --- lists/exciseTax01.py | 28 ++++++++++++++++++++++++++++ lists/exciseTax02.py | 31 +++++++++++++++++++++++++++++++ lists/exciseTax03.py | 30 ++++++++++++++++++++++++++++++ lists/grades01.py | 16 ++++++++++++++++ lists/grades02.py | 15 +++++++++++++++ lists/grades03.py | 23 +++++++++++++++++++++++ lists/list01.py | 17 +++++++++++++++++ lists/list02.py | 21 +++++++++++++++++++++ lists/list03.py | 11 +++++++++++ lists/list04.py | 16 ++++++++++++++++ lists/range01.py | 15 +++++++++++++++ lists/range02.py | 21 +++++++++++++++++++++ lists/range03.py | 21 +++++++++++++++++++++ 13 files changed, 265 insertions(+) create mode 100644 lists/exciseTax01.py create mode 100644 lists/exciseTax02.py create mode 100644 lists/exciseTax03.py create mode 100644 lists/grades01.py create mode 100644 lists/grades02.py create mode 100644 lists/grades03.py create mode 100644 lists/list01.py create mode 100644 lists/list02.py create mode 100644 lists/list03.py create mode 100644 lists/list04.py create mode 100644 lists/range01.py create mode 100644 lists/range02.py create mode 100644 lists/range03.py diff --git a/lists/exciseTax01.py b/lists/exciseTax01.py new file mode 100644 index 0000000..1e055fd --- /dev/null +++ b/lists/exciseTax01.py @@ -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") + diff --git a/lists/exciseTax02.py b/lists/exciseTax02.py new file mode 100644 index 0000000..fdd0b67 --- /dev/null +++ b/lists/exciseTax02.py @@ -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]}") diff --git a/lists/exciseTax03.py b/lists/exciseTax03.py new file mode 100644 index 0000000..41ca8e8 --- /dev/null +++ b/lists/exciseTax03.py @@ -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 + + + diff --git a/lists/grades01.py b/lists/grades01.py new file mode 100644 index 0000000..27de1d3 --- /dev/null +++ b/lists/grades01.py @@ -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) diff --git a/lists/grades02.py b/lists/grades02.py new file mode 100644 index 0000000..996e014 --- /dev/null +++ b/lists/grades02.py @@ -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}") diff --git a/lists/grades03.py b/lists/grades03.py new file mode 100644 index 0000000..bc248f0 --- /dev/null +++ b/lists/grades03.py @@ -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}") diff --git a/lists/list01.py b/lists/list01.py new file mode 100644 index 0000000..c520fe2 --- /dev/null +++ b/lists/list01.py @@ -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)) diff --git a/lists/list02.py b/lists/list02.py new file mode 100644 index 0000000..3e5c18b --- /dev/null +++ b/lists/list02.py @@ -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) diff --git a/lists/list03.py b/lists/list03.py new file mode 100644 index 0000000..dc1976c --- /dev/null +++ b/lists/list03.py @@ -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) diff --git a/lists/list04.py b/lists/list04.py new file mode 100644 index 0000000..433f5d5 --- /dev/null +++ b/lists/list04.py @@ -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!") diff --git a/lists/range01.py b/lists/range01.py new file mode 100644 index 0000000..11039f1 --- /dev/null +++ b/lists/range01.py @@ -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) + + diff --git a/lists/range02.py b/lists/range02.py new file mode 100644 index 0000000..5e89e6b --- /dev/null +++ b/lists/range02.py @@ -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) diff --git a/lists/range03.py b/lists/range03.py new file mode 100644 index 0000000..a77cef5 --- /dev/null +++ b/lists/range03.py @@ -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)