# Define an empty List called InfoDb
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"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Shane",
"LastName": "Lopez",
"DOB": "February 27",
"Residence": "San Diego",
"Email": "???@powayusd.com",
"Owns_Cars": ["2021-Insight"]
})
#Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Ankit",
"LastName": "Pulivendula",
"DOB": "July 17",
"Residence": "San Diego",
"Email": "ankit.pulivendula@gmail.com",
"Owns_Cars": ["None"]
})
#Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Lionel",
"LastName": "Messi",
"DOB": "June 24",
"Residence": "Miami",
"Email": "???@gmail.com",
"Owns_Cars": ["2007 Ferrari F430 Spyder"]
})
# Print the data structure
print(InfoDb)
# This jupyter cell has dependencies on one or more cells above
# print function: given a dictionary of InfoDb content
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) # call to function
for_loop() # call to function
# This jupyter cell has dependencies on one or more cells above
# 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()
# This jupyter cell has dependencies on one or more cells above
# recursion algorithm loops incrementing on each call (n + 1) until exit condition is met
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
print("Recursive loop output\n")
recursive_loop(0)
[{'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']}, {'FirstName': 'Sunny', 'LastName': 'Naidu', 'DOB': 'August 2', 'Residence': 'Temecula', 'Email': 'snaidu@powayusd.com', 'Owns_Cars': ['4Runner']}, {'FirstName': 'Shane', 'LastName': 'Lopez', 'DOB': 'February 27', 'Residence': 'San Diego', 'Email': '???@powayusd.com', 'Owns_Cars': ['2021-Insight']}, {'FirstName': 'Ankit', 'LastName': 'Pulivendula', 'DOB': 'July 17', 'Residence': 'San Diego', 'Email': 'ankit.pulivendula@gmail.com', 'Owns_Cars': ['None']}, {'FirstName': 'Lionel', 'LastName': 'Messi', 'DOB': 'June 24', 'Residence': 'Miami', 'Email': '???@gmail.com', 'Owns_Cars': ['2007 Ferrari F430 Spyder']}]
For loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
Sunny Naidu
Residence: Temecula
Birth Day: August 2
Cars: 4Runner
Shane Lopez
Residence: San Diego
Birth Day: February 27
Cars: 2021-Insight
Ankit Pulivendula
Residence: San Diego
Birth Day: July 17
Cars: None
Lionel Messi
Residence: Miami
Birth Day: June 24
Cars: 2007 Ferrari F430 Spyder
While loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
Sunny Naidu
Residence: Temecula
Birth Day: August 2
Cars: 4Runner
Shane Lopez
Residence: San Diego
Birth Day: February 27
Cars: 2021-Insight
Ankit Pulivendula
Residence: San Diego
Birth Day: July 17
Cars: None
Lionel Messi
Residence: Miami
Birth Day: June 24
Cars: 2007 Ferrari F430 Spyder
Recursive loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
Sunny Naidu
Residence: Temecula
Birth Day: August 2
Cars: 4Runner
Shane Lopez
Residence: San Diego
Birth Day: February 27
Cars: 2021-Insight
Ankit Pulivendula
Residence: San Diego
Birth Day: July 17
Cars: None
Lionel Messi
Residence: Miami
Birth Day: June 24
Cars: 2007 Ferrari F430 Spyder