Lists and Dictionaries Notes
Taking notes with execution.
name = "Drew Reed"
print("name", name, type(name))
print("The variable key is 'name'. The value is 'Drew Reed'. It is a string.")
print()
age = 16
print("age", age, type(age))
print("The variable key is 'age'. Its value is '16'. It is an integer.")
print()
score = 90.0
print("score", score, type(score))
print("The variable key is 'score'. Its value is '90.0'. It is a float, a.k.a. a floating point number.")
print()
print("What is variable name/key?", "value?", "type?", "primitive or collection?")
print("What is different about the list output?")
langs = ["Python", "JavaScript", "Java", "Bash"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[3]", langs[3], type(langs[3]))
print("The variable")
print()
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"]))
print("It comes out with little squiggly brackets.")
InfoDb = []
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"]
})
InfoDb.append({
"FirstName": "Sunny",
"LastName": "Naidu",
"DOB": "August 2",
"Residence": "Temecula",
"Email": "snaidu@powayusd.com",
"Owns_Cars": ["4Runner"]
})
InfoDb.append({
"FirstName": "Drew",
"LastName": "Reed",
"DOB": "November 7",
"Residence": "San Diego",
"Email": "drewdafox@gmail.com",
"Owns_Cars": ["Toyota Camry"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "Residence:", d_rec["Residence"])
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Cars: ", end="")
print(", ".join(d_rec["Owns_Cars"]))
print()
def for_loop():
print("For loop output\n")
for record in InfoDb:
print_data(record)
for_loop()
This can be a little bit confusing, so let's put this in simpler terms.
When defining print_data
, the different data in the Info.Db
is called upon based on its Dictionary definition and placed in a visually appealing way using \t
and spacing with comma.
Let's look at how to loop the output of the info just enough to get it out.
InfoDb = []
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"]
})
InfoDb.append({
"FirstName": "Sunny",
"LastName": "Naidu",
"DOB": "August 2",
"Residence": "Temecula",
"Email": "snaidu@powayusd.com",
"Owns_Cars": ["4Runner"]
})
InfoDb.append({
"FirstName": "Drew",
"LastName": "Reed",
"DOB": "November 7",
"Residence": "Temecula",
"Email": "drewdafox@gmail.com",
"Owns_Cars": ["Toyota Camry"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"])
print("\t", "Residence:", d_rec["Residence"])
print("\t", "Birth Day:", d_rec["DOB"])
print("\t", "Cars: ", end="") # end="" make sure no return occurs
print(", ".join(d_rec["Owns_Cars"]))
print()
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)
While loops and recursive loops work very similarly, just making use of slightly different commands. Both compare how much content is contained within InfoDb
with how much has already been output (indicated by the i
value).