Dictionaries and Lists
Creating code and functions using lists and dictionaries.
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"]
})
#added lukes info
InfoDb.append({
"FirstName": "Luke",
"LastName": "Angelini",
"DOB": "July 29",
"Residence": "4S Ranch",
"Email": "ltangelini@gmail.com",
"Owns_Cars": ["None"]
})
# Allow input
InfoDb.append({
"FirstName": input(),
"LastName": input(),
"DOB": input(),
"Residence": input(),
"Email": input(),
"Owns_Cars": input()
})
# Print the data structure
print(InfoDb)
dog_list = ["German Shephard", "Labrador Retriever", "Great Dane", "Chihuahau", "Poodle", "Pitbull"]
# The i variable represents a value in the index dog_list. The for loop prints until the list is complete.
for i in dog_list:
print(i)
def backward(p):
print(p[::-1])
print(p)
return p[::-1] == p
word = input("Enter a word: ")
# A word is collected with the input function and then put into the backward function below
# .casefold() can be put after a variable to convert it to all lowercase
if backward(word.casefold()) is True:
# The p[::-1] reverses the text of the input word and the rest of the function prints the word and returns the word as equal to p
print("{0} is the same forwards and backwards".format(word))
else:
print("{0} is not spelled the same forwards and backwards".format(word))
i = 1
while i <=10:
print(i)
i += 1
# The i variable represents the number and each time the number is printed the value i is increased.
print ("Done with loop.")
New_Dict = {
"Red": "Fire",
"Blue": "Water",
"Green": "Grass"
}
print(New_Dict)
# A New key can be added to the dictionary
New_Dict["Yellow"] = "Lightning"
print(New_Dict)
def Python_Quiz(prompt):
global word
print ("Question: " + prompt)
word = input()
return word
score = 0
My_Quiz = [{
"What function is used to output text?":"print",
"What keyword is used to define a function?":"def",
"What command allows the use of previously developed functions?":"import",
"What function is used to collect user input?":"input"
}] #
for dict in My_Quiz:
for questions, answers in dict.items():
Python_Quiz(questions)
if word == answers:
print("Correct answer")
score += 1
else:
print("Incorrect answer")
print("You got" + str(score) + "/4 correct.")