Lists and Dictionaries
Lists and Dictionaries
- Using Index in For Loop
- Outputing In Reverse Order
- Other Methods Performed By Lists
- QuizThat Stores In a List of Dictionaries
- New Dictionary Data Set
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": "Ethan",
"LastName": "Truong",
"DOB": "July 28",
"Residence": "4s Ranch",
"Email": "truong.ethan@yahoo.com",
"Owns_Cars": ["BMW"]
})
# 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 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)