Source Code

import csv

print("Unit 5")

def main():
   done = False
   while not done:
        print("Welcome to the 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":
                example5()
            case "E6":
                example6()
            case "E7":
                example7()
            case "E8":
                example8()
            case "E9":
                example9()
            case "Q":
                print("Quitting!")
                done = True
            # default case
            case _:
                print("Invalid, try again!")

# define Example 1 Function
def example1():
    print("Example 1")
    numbers = [32, 14, 15, 10, 2]
    # for loop v1
    print("Index\tValue")
    for i in range(len(numbers)):
        print(str(i) + "\t" + str(numbers[i]))
    # for loop v2
    print("Elements")
    for n in numbers:
        print(n)
    # print min
    print("Min: " + str(min(numbers)))
    # print max
    print("Max: " + str(max(numbers)))
    # print sum
    print("Sum: " + str(sum(numbers)))
    # in operator
    num = int(input("Number to Search: "))
    if num in numbers:
        print(str(num) + " is in the list")
    else:
        print(str(num) + " is not in the list")

# define Example 2 Function
def example2():
    print("Example 2")
    fruits = ['apple', 'banana', 'cherry']
    print(fruits)
    fruits[0] = "guava"
    print(fruits)
    fruits.append('orange')
    print(fruits)
    fruits.insert(0, 'dragonfruit')
    print(fruits)
    fruits.insert(1, 'grapefruit')
    print(fruits)

# define Example 3 Function
def example3():
    print("Example 3")
    numbers = [x for x in range(1, 11)]
    print(numbers)
    numbers.pop()
    print(numbers)
    numbers.pop(1)
    print(numbers)
    numbers.remove(5)
    print(numbers)
    numbers.index(3)

# define Example 4 Function
def example4():
    print("Example 4")
    numbers = [i for i in range(1, 101)]

    # first 10
    first10 = numbers[:10]
    print("First 10: " + str(first10))

    # last 10
    last10 = numbers[-10:]
    print("Last 10: " + str(last10))

    # middle 20
    middle = numbers[40:60]
    print("Middle 20: " + str(middle))

# define Example 5 Function
def example5():
    print("Example 5")
    # create a tuple with one element
    single_tuple = (100, )
    print(single_tuple)

    # print the first element
    print(single_tuple[0])

def example6():
    # create a tuple
    t = ('a', 'b', 'c')

    # loop through elements v1
    for x in t:
        print(x)

    # loop through elements v2
    for i in range(len(t)):
        print(str(i) + "\t" + str(t[i]))

# example7 function definition
def example7():
    # create a tuple
    t = (1, 2, 3)
    # Convert tuple to list
    lt = list(t)
    # Modify the list
    lt.append(4)
    t = tuple(lt)
    # Convert back to tuple
    print(t)

# define Example 8 Function
def example8():
    print("Example 8")
    # open the csv file
    file = open('scores.csv', 'r')
    # create a csv reader
    csv_reader = csv.reader(file)
    # read the header
    header = next(csv_reader)
    print(header[0] + "\t" + header[1] + "\t" + header[2] + "\t" + header[3])

    #Iterate over the remaining rows
    for row in csv_reader:
        print(row[0] + '\t' + row[1] + '\t' + row[2] + '\t' + row[3])

    # Close the file
    file.close()

# define Example 9 Function
def example9():
    # two parallel lists
    students = ["Alice", "Bob", "Charlie", "Diana"]
    grades = [90, 85, 78, 92]

    # print each student's grade
    for i in range(len(students)):
        print(students[i], "scored", grades[i])


main()

Last updated