Lists and Dictionaries
Lists and Dictionaries
- Using Index in For Loop
- Outputing In Reverse Order
- Print Function and While Loop/Recursion (More List Methods)
- QuizThat Stores In a List of Dictionaries
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "John Doe"
print("name", name, type(name))
print()
# variable of type integer
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
age = 18
print("age", age, type(age))
print()
# variable of type float
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
score = 90.0
print("score", score, type(score))
print()
# variable of type list (many values in one variable)
print("What is variable name/key?", "value?", "type?", "primitive or collection?")
print("What is different about the list output?")
langs = ["Python", "JavaScript", "Java"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[0]", langs[0], type(langs[0]))
print()
# variable of type dictionary (a group of keys and values)
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
print("What is different about the dictionary output?")
person = {
"name": name,
"age": age,
"score": score,
"langs": langs
}
print("person", person, type(person), "length", len(person))
print('- person["name"]', person["name"], type(person["name"]))
InfoDb = []
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "John",
"LastName": "Mortensen",
"DOB": "October 21",
"Residence": "San Diego",
"Email": "jmortensen@powayusd.com",
"Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Sunny",
"LastName": "Naidu",
"DOB": "August 2",
"Residence": "Temecula",
"Email": "snaidu@powayusd.com",
"Owns_Cars": ["4Runner"]
})
# Print the data structure
# InfoDB is a data structure with expected Keys and Values
# Append to List a Dictionary of key/values related to a person and cars
InfoDb.append({
"FirstName": "Liav",
"LastName": "Bar",
"DOB": "August 9",
"Residence": "San Diego",
"Email": "liavbar5@gmail.com",
"Owns_Cars": ["2020-Camry", "2017-Sorento"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Re'em",
"LastName": "Ben-Ishai",
"DOB": "May 11",
"Residence": "San Diego",
"Email": "ReemB@powayusd.com",
"Owns_Cars": ["2021-Kona"]
})
# Print the data structure
print(InfoDb)
list = ["Mercedes","Toyota","Tesla","Honda","Hyundai","Bugatti"]
for i in list:
print(i)
list = ["Mercedes","Toyota","Tesla","Honda","Hyundai","Bugatti"]
for i in list:
print(i[::-1])
print(list[::-1])
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Cars: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print()
# for loop algorithm iterates on length of InfoDb
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
# while loop algorithm contains an initial n and an index incrementing statement (n += 1)
def while_loop():
print("While loop output\n")
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
def question_with_response(prompt):
print("Question: " + prompt)
word = " "
questions = 4 # number of questions
correct = 0
questions_answers = [{"What command is used to include other functions that were previously developed?" : "import",
"What command is used to evaluate correct or incorrect response in this example?" : "if",
"Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
"Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}] # dictionary
# questions_answers.append = ({"What command is used to include other functions that were previously developed?" : "import",
# "What command is used to evaluate correct or incorrect response in this example?" : "if",
# "Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
# "Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}) # dictionary
for i in questions_answers:
for question, answer in i.items():
question_with_response(question) # printing the questions
word = input("Answer: ") # variable that connects to the user's input
if word == answer: # if the answer provided is correct
print("You got it right!!")
correct += 1
else: # if the answer provided is wrong
print("Your answer was wrong")
print(str(correct) + "/" + str(questions)) # correct/len(questions_answers)