Infodb and Lists
Learning the process
Here I start to list a dictionary that assigns keys according to a word. So for example my first key is First Name and the value is Jagger. To print out the list I used the function print (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": "Jagger",
"LastName": "Klein",
"DOB": "September 18 2204",
"Residence": "Residence",
"Email": "jagger.klein@icloud.com",
"Owns_Cars": ["2020-Mazda3", "2022-Ferrari", "2003-Hyundai", "2016-Audi A3", "1969-Volvo"]
})
# Print the data structure
print(InfoDb)
Below I output the loop data by using the function print_data. But to use this you need a parameter that defines the data d_rec. The function of actually making the list is for_loop, to activate the data.
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 generally does the same thing as above but recursive loop activates the function untill it fails.
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 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)