diff --git a/README.md b/README.md index 0827612..83fe12b 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,6 @@ ### This is a third header +## This is a new header level 2 + Temp repository diff --git a/if/if01.py b/if/if01.py new file mode 100644 index 0000000..f3d834f --- /dev/null +++ b/if/if01.py @@ -0,0 +1,32 @@ + +program01 = ''' + +Program: if01 +This program demonstrates the conditional tests used in Python. + +''' + +print(program01) + +a = 4 +b = 7 +c = 7 + +print(f"a = {a}\nb = {b}\nc = {c}\n") + +print(f"a == b {a==b}") +print(f"a != b {a!=b}") +print(f"a < b {a b {a>b}") +print(f"a >= b {a>=b}") + +print("\n") + +print(f"c == b {c==b}") +print(f"c != b {c!=b}") +print(f"c < b {c b {c>b}") +print(f"c >= b {c>=b}") + diff --git a/if/if02.py b/if/if02.py new file mode 100644 index 0000000..b5f131c --- /dev/null +++ b/if/if02.py @@ -0,0 +1,39 @@ + +# if statement example +if 22 > 7: + print("22 is greater than 7") + +print("A better method to define the code ...") + +# Use variables in the if statement and print +data1 = 22 + + +if data1 > 7: + print(f"{data1} is greater than 7") + + +# Or this form +print("And another method to define the code ...") + + +data1 = 22 +data2 = 7 + +if data1 > data2: + print(f"{data1} is greater than {data2}") + + + + +data1 = 6 +data2 = 7 + +if data1 > data2: + print(f"{data1} is greater than {data2}") +else: + print(f"{data1} is less than or equal {data2}") + + +print("Program ended") + diff --git a/if/if03.py b/if/if03.py new file mode 100644 index 0000000..577511b --- /dev/null +++ b/if/if03.py @@ -0,0 +1,16 @@ + +# if..else statement example +x = 7 +print(f"{x} is equal to 7?") +if x == 7: + print("Yes") +else: + print("No") + +# if..else statement example +x = 5 +print(f"{x} is equal to 7?") +if x == 7: + print("Yes") +else: + print("No") diff --git a/if/if04.py b/if/if04.py new file mode 100644 index 0000000..00ae972 --- /dev/null +++ b/if/if04.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 +# Program: if02 + +# Lists of numbers. These could be integers or floats. +intList = [7, 2, 16, 18, 12, 21] + +# Print the list and the length +print(intList," length = ",len(intList)) + +print(f"Print each value in the list") +counter = 0 +for item in intList: + print(f"intList[{counter}] = {item:2d}") + counter += 1 + +cutoff = 13 +print(f"Print each value in the list >= {cutoff}") +counter = 0 +for item in intList: + if(item >= cutoff): + print(f"intList[{counter}] = {item:2d}") + counter += 1 + diff --git a/if/if05.py b/if/if05.py new file mode 100644 index 0000000..fc31f8a --- /dev/null +++ b/if/if05.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 +# Program: if03 + +# Lists of numbers. These could be integers or floats. +intList = [7, 2, 16, 18, 12, 21] + +# Print the list and the length +print(intList," length = ",len(intList)) + +print(f"Print each value in the list") +counter = 0 +for item in intList: + print(f"intList[{counter}] = {item:2d}") + counter += 1 + +cutoff = 13 +print(f"Print each value in the list >= {cutoff}") +counter = 0 +for item in intList: + if(item >= cutoff): + print(f"intList[{counter}] = {item:2d}") + else: + print(f"========> intList[{counter}] = {item:2d} is less than {cutoff}") + counter += 1 + diff --git a/if/if06.py b/if/if06.py new file mode 100644 index 0000000..bf9b1f5 --- /dev/null +++ b/if/if06.py @@ -0,0 +1,7 @@ + +# consider the following line from the autoloan program: + +principal = float(input("Enter a value for the principal: ")) + + +print(f"You entered the value {principal}") \ No newline at end of file diff --git a/if/if07.py b/if/if07.py new file mode 100644 index 0000000..8ce43f5 --- /dev/null +++ b/if/if07.py @@ -0,0 +1,16 @@ + +# consider the following line from the autoloan program: + +principal = float(input("Enter a value for the principal: ")) +if(principal < 5000.) or (principal >120000.00): + print(f"The loan value {principal} is outside the allowed limits.") + exit(-1) + +rate = float(input("Enter a value for the interest rate (7 for 7%): ")) + +numberOfYears = float(input("Enter the number of years for the loan: ")) + +print(f"You entered the principal value {principal}") +print(f"You entered the rate value {rate}%") +print(f"You entered the loan term value {numberOfYears} years") + 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)