List and Dictionary purpose

  • List is used to collect many
  • Dictionary is used to define data patterns.
  • Iteration is often used to process through lists.
  • A 'list' data type has the method '.append(expression)' that allows you to add to the list
  • The function '.append(expression)' of a 'list' data type lets you to add to the list.

So I will use the InfoDb command to sort the important dates for class.

InfoDb = []

# Append to List a Dictionary
InfoDb.append({
    "ClassName": "AP Chemistry",
    "Period": "2",
    "TypeOfTest": "FRQ",
    "Date": "9/7/22",
    "StudyMaterial": "AP Style Test Problems",
    "TeacherEmail": "kozuna@powayusd.com",
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "ClassName": "AP Calculus",
    "Period": "3",
    "TypeOfTest": "MC and FRQ",
    "Date": "9/9/22",
    "StudyMaterial": "Review Homework",
    "TeacherEmail": "jbuehler@powayusd.com",
})

# Append to List a 3rd Dictionary of key/values
InfoDb.append({
    "ClassName": "AP Studio Art",
    "Period": "1",
    "TypeOfTest": "Submission of Art",
    "Date": "9/1/22",
    "StudyMaterial": "Finish First Art Piece",
    "TeacherEmail": "pcoleman@powayusd.com",
})

# Append to List a 4th Dictionary of key/values
InfoDb.append({
    "ClassName": "AP CompSci",
    "Period": "4",
    "TypeOfTest": "Submission of Hacks",
    "Date": "9/5/22",
    "StudyMaterial": "Finish Hacks at bottom page",
    "TeacherEmail": "syeung@powayusd.com",
})

# Print the data structure
print(InfoDb)
[{'ClassName': 'AP Chemistry', 'Period': '2', 'TypeOfTest': 'FRQ', 'Date': '9/7/22', 'StudyMaterial': 'AP Style Test Problems', 'TeacherEmail': 'kozuna@powayusd.com'}, {'ClassName': 'AP Calculus', 'Period': '3', 'TypeOfTest': 'MC and FRQ', 'Date': '9/9/22', 'StudyMaterial': 'Review Homework', 'TeacherEmail': 'jbuehler@powayusd.com'}, {'ClassName': 'AP Studio Art', 'Period': '1', 'TypeOfTest': 'Submission of Art', 'Date': '9/1/22', 'StudyMaterial': 'Finish First Art Piece', 'TeacherEmail': 'pcoleman@powayusd.com'}, {'ClassName': 'AP CompSci', 'Period': '4', 'TypeOfTest': 'Submission of Hacks', 'Date': '9/5/22', 'StudyMaterial': 'Finish Hacks at bottom page', 'TeacherEmail': 'syeung@powayusd.com'}]

This is the for loop, which is a loop that can be used for lists. I personally find this loop the easiest to use.

def print_data(d_rec):
    print(d_rec["ClassName"], "Period:", d_rec["Period"])  # using comma puts space between values
    print("\t", "Type Of Test:", d_rec["TypeOfTest"]) # \t is a tab indent
    print("\t", "Date:", d_rec["Date"])
    print("\t", "Study Material:", d_rec["StudyMaterial"])
    print("\t", "Teacher Email:", d_rec["TeacherEmail"])
    print()


# for loop iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
For loop output

AP Chemistry Period: 2
	 Type Of Test: FRQ
	 Date: 9/7/22
	 Study Material: AP Style Test Problems
	 Teacher Email: kozuna@powayusd.com

AP Calculus Period: 3
	 Type Of Test: MC and FRQ
	 Date: 9/9/22
	 Study Material: Review Homework
	 Teacher Email: jbuehler@powayusd.com

AP Studio Art Period: 1
	 Type Of Test: Submission of Art
	 Date: 9/1/22
	 Study Material: Finish First Art Piece
	 Teacher Email: pcoleman@powayusd.com

AP CompSci Period: 4
	 Type Of Test: Submission of Hacks
	 Date: 9/5/22
	 Study Material: Finish Hacks at bottom page
	 Teacher Email: syeung@powayusd.com

Using this information, I will make a quiz that stores in a List of Dictionaries and asking questions about you!

InfoDb = []

print("Hi! This computer wants to know more stuff about you!")
rsp = input ("Are you ready?")
if rsp == "yes":
    print ("Thanks!\n")
if rsp == "no":
    print ("Oh no.\n")

print ("What is your name?")
name = input ('What is your name?\n')

print ("Hello " + name + "! How old are you?")
age = input ('How old are you?\n')

print ("Wow you're " + age + "! What grade are you in?")
grade = input ('What grade are you in\n')

print (grade + "th grade! What is your favorite class right now?")
favoriteClass = input ('What is your favorite class right now\n')

print (favoriteClass +"! That is my favorite, too! Thanks for taking this quiz, have a nice day!")

InfoDb.append({
    "Name" : name,
    "Age" : age,
    "Grade" : grade,
    "Favorite Class" : favoriteClass,
})

def print_data(d_rec):
    print("Name:", d_rec["Name"])
    print("Age:", d_rec["Age"])
    print("Grade:", d_rec["Grade"])
    print("Favorite Class:", d_rec["Favorite Class"])
    print()


# for loop iterates on length of InfoDb
def for_loop():
    print("For loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
     
Hi! This computer wants to know more stuff about you!
What is your name?
Hello Haeryn! How old are you?
Wow you're 15! What grade are you in?
9th grade! What is your favorite class right now?
AP Calc! That is my favorite, too! Thanks for taking this quiz, have a nice day!
For loop output

Name: Haeryn
Age: 15
Grade: 9
Favorite Class: AP Calc