Source Code
def main():
print("Main Function")
print("Menu")
print("E1 - Example 1")
print("Q - Quit")
choice = input("Choice: ")
match choice:
case "E1":
example1()
case "E2":
example2()
case "E3":
example3()
case "E4":
example4()
case "E5":
dict = {}
print("Before: " + str(dict))
example5(dict)
print("After: " + str(dict))
case "E6":
example6()
case "E7":
example7()
case "Q":
print("Quitting!")
# default case
case _:
print("Invalid, try again!")
# define Example 1 Function
def example1():
print("Example 1")
cs_terms = {
'Algorithm': 'A step-by-step process to solve a problem.',
'Data Structure': 'A way to organize and store data efficiently.',
'Big O Notation': 'Notation expressing algorithm complexity.',
'Recursion': 'A function calling itself to solve a problem.',
'Encapsulation': 'Bundling data and methods in a single unit.',
}
# use a loop to print the keys and values
print("Key\tValue")
for key in cs_terms:
print(key + "\t" + cs_terms[key])
# number of elements
print("Number of Elements: " + str(len(cs_terms)))
# convert keys to a list
cs_keys = list(cs_terms.keys())
print("Keys: " + str(cs_keys))
# convert values to a list
cs_values = list(cs_terms.values())
print("Values: " + str(cs_values))
# define Example 2 Function
def example2():
print("Example 2")
# create an empty dictionary
states = {}
# add a key/value pair
states["NJ"] = "New Jersey"
# print the value of states
print(states)
# add another key/value pair
states["NY"] = "New York"
print(states)
# delete a key/value pair
del states["NY"]
print(states)
# search for a key
search = input("Enter a state abbreviation: ")
if search in states:
print(search + " is an abbreviation for " + states[search])
else:
print(search + " is not in the dictionary")
def example3():
print("Example 3")
# declare gradebook
gradebook = {
'Alice' : [92, 85, 100],
'Bob' : [83, 95, 79],
'Charlie' : [97, 91, 92],
}
# initialize accumulator variables
grades_total = 0
grades_count = 0
# for loop to traverse the grades for each student
for name, grades in gradebook.items():
total = sum(grades)
print("Average for " + name + " " + str(total/len(grades)))
grades_total += total
grades_count += len(grades)
# print the class average
print("Class average is: " + str(grades_total/grades_count))
# define Example 4 Function
def example4():
print("Example 4")
towns = {
'Elizabeth': {'Population': 128885, 'County': 'Union'},
'Hoboken': {'Population': 50000, 'County': 'Hudson'},
'Asbury Park': {'Population': 15860, 'County': 'Monmouth'}
}
# print Hoboken's Population
print("Hoboken's Population: " + str(towns["Hoboken"]["Population"]))
# update Hoboken's Population
towns["Hoboken"]["Population"] = 50001
# add an element
towns["Newark"] = {'Population': 305344, 'County': 'Essex'}
# Delete an element
del towns["Asbury Park"]
# print the dictionary used nested loops
for town, details in towns.items():
print("Town: " + town)
for info, value in details.items():
print(info + ": " + str(value))
print()
# define Example 5 Function
def example5(d):
d['pass by'] = 'reference'
# define Example 6 Function
def example6():
print("Example 6")
# create a set
set1 = {'a', 'b', 'c'}
set2 = set(['c', 'd', 'e'])
# traverse a set
for s in set1:
print(s)
# set operations
print("Union: " + str(set1 | set2))
print("Intersection: " + str(set1 & set2))
print("Difference: " + str(set1 - set2))
# symmetric difference
print(set1.symmetric_difference(set2))
# define Example 7 Function
def example7():
print("Example 7")
set1 = {1, 2, 3, 4}
set2 = {3, 4}
# issubset()
print(set1.issubset(set2))
print(set2.issubset(set1))
# issuperset()
print(set1.issuperset(set2))
print(set2.issuperset(set1))
main()
Last updated