print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "Luke Riggins"
print("name", name, type(name))

print()


# variable of type integer
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
age = 17
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", "Bash"] # *I added bash as a language*
print("langs", langs, type(langs), "length", len(langs))
print("- langs[3]", langs[3], type(langs[3])) # *I changed the index from 0 to 3 so that the print command will print Bash instead of Python*

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"]))
What is the variable name/key? value? type? primitive or collection, why?
name Luke Riggins <class 'str'>

What is the variable name/key? value? type? primitive or collection, why?
age 17 <class 'int'>

What is the variable name/key? value? type? primitive or collection, why?
score 90.0 <class 'float'>

What is variable name/key? value? type? primitive or collection?
What is different about the list output?
langs ['Python', 'JavaScript', 'Java', 'Bash'] <class 'list'> length 4
- langs[3] Bash <class 'str'>

What is the variable name/key? value? type? primitive or collection, why?
What is different about the dictionary output?
person {'name': 'Luke Riggins', 'age': 17, 'score': 90.0, 'langs': ['Python', 'JavaScript', 'Java', 'Bash']} <class 'dict'> length 4
- person["name"] Luke Riggins <class 'str'>
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": "Ethan",
    "LastName": "Nyguen",
    "DOB": "October 3",
    "Residence": "San Diego",
    "Email": "EthanN45233@stu.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": "Luke",
    "LastName": "Riggins",
    "DOB": "December 2",
    "Residence": "San Diego",
    "Email": "luker56233@powayusd.com",
    "Owns_Cars": ["Volkswagon Passat"]
})

# PUTTING MY INFORMATION (HACK!)
InfoDb.append({
    "FirstName": "Luke",
    "LastName": "Riggins",
    "DOB": "December 2",
    "Residence": "Rancho Bernardo",
    "Email": "luke120204@gmail.com",
    "Owns_Cars": ["Toyota"]
})

# PUTTING MY PARTER'S INFORMATION (HACK!)
InfoDb.append({
    "FirstName": "Jeffery",
    "LastName": "Lee",
    "DOB": "December 27",
    "Residence": "4S Ranch",
    "Email": "jefferyl56783@stu.powayusd.com",
    "Owns_Cars": ["Honda Accord"]
})

# Print the data structure
print(InfoDb)
[{'FirstName': 'Ethan', 'LastName': 'Nyguen', 'DOB': 'October 3', 'Residence': 'San Diego', 'Email': 'EthanN45233@stu.powayusd.com', 'Owns_Cars': ['2015-Fusion', '2011-Ranger', '2003-Excursion', '1997-F350', '1969-Cadillac']}, {'FirstName': 'Luke', 'LastName': 'Riggins', 'DOB': 'December 2', 'Residence': 'San Diego', 'Email': 'luker56233@powayusd.com', 'Owns_Cars': ['Volkswagon Passat']}, {'FirstName': 'Luke', 'LastName': 'Riggins', 'DOB': 'December 2', 'Residence': 'Rancho Bernardo', 'Email': 'luke120204@gmail.com', 'Owns_Cars': ['Toyota']}, {'FirstName': 'Jeffery', 'LastName': 'Lee', 'DOB': 'December 27', 'Residence': '4S Ranch', 'Email': 'jefferyl56783@stu.powayusd.com', 'Owns_Cars': ['Honda Accord']}]
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

Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Formatted output of List/Dictionary - for loop

  • Prepare a function to format the data

  • Prepare a function to iterate through the list

  • Activate your function

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

Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Alternate Methods for iteration

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

Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Recursion

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

Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Creating a For Loop with Index

for indx, record in enumerate(InfoDb):
    print("Record #" + str(indx+1) + ": ", end = "")
    print_data(record)
Record #1: Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Record #2: Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Record #3: Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Record #4: Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Reverse Order

for record in reversed(InfoDb):
    print_data(record)
Jeffery Lee
	 Residence: 4S Ranch
	 Birth Day: December 27
	 Cars: Honda Accord

Luke Riggins
	 Residence: Rancho Bernardo
	 Birth Day: December 2
	 Cars: Toyota

Luke Riggins
	 Residence: San Diego
	 Birth Day: December 2
	 Cars: Volkswagon Passat

Ethan Nyguen
	 Residence: San Diego
	 Birth Day: October 3
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac