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": "Saavan",
    "LastName": "Gade",
    "DOB": "July 14",
    "Residence": "San Diego",
    "Email": "savvy05@gmail.com",
    "Owns_Cars": ["2015-BMW"]
})
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()
For loop output

Saavan Gade
	 Residence: San Diego
	 Birth Day: July 14
	 Cars: 2015-BMW

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()
While loop output

Saavan Gade
	 Residence: San Diego
	 Birth Day: July 14
	 Cars: 2015-BMW

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)
Recursive loop output

Saavan Gade
	 Residence: San Diego
	 Birth Day: July 14
	 Cars: 2015-BMW

"Notebook Check"

  • toc:true
  • branch: master
  • badges: true
  • comments: true
  • author: Saavan Gade
  • categories: [CSP Assignments, Week 3]