Dictionary
Dictionary
- Explanation of what the code does:
- While loop
- Recursive loop
- Reverse database
- Add your own record to the database
- Learning
Variables belong to a certain data type. Examples that we have seen before include: int, string, and bool.
Lists and dictionaries are also types.
Explanation of what the code does:
I first added three entries into the dictionary. Keys include FirstName
, LastName
, and some others that I created, such as Hobbies
and Sports played
.
I used a for loop to print the records from the dictionary. The for loop ranges from 0 to the length of the dictionary, in this example, 2. I then printed each record by printing the index of the dictionary (InfoDb[i]), starting from 0, then 1 and finally 2.
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def for_loop():
for i in range(len(InfoDb)):
record = InfoDb[i]
print_data(record)
return
for_loop()
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def for_loop():
for record in InfoDb:
print_data(record)
for_loop()
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def while_loop():
i = 0
while i < len(InfoDb):
record = InfoDb[i]
print_data(record)
i += 1
return
while_loop()
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
recursive_loop(0)
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def for_loop_reverse():
for i in reversed(range(len(InfoDb))):
record = InfoDb[i]
print_data(record)
return
for_loop_reverse()
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": "Lily",
"LastName": "Wu",
"DOB": "July 24",
"Favorite books": ["Salt to the Sea", "Sherlock Holmes"],
"Favorite food": "Shrimp",
"Hobbies": ["Reading"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "Giannina",
"LastName": "Ortega Rico",
"DOB": "March 9",
"Favorite books": ["none"],
"Favorite food": "none",
"Hobbies": ["Reading", "watching TV"],
"Sports played": ["none"]
})
InfoDb.append({
"FirstName": "John",
"LastName": "Doe",
"DOB": "January 1",
"Favorite books": ["none"],
"Favorite food": "Rice",
"Hobbies": ["Youtube", "Wasting time 🤪"],
"Sports played": ["none"]
})
print("Enter your info below!")
firstName = input("First name?")
print("First name? " + firstName)
lastName = input("Last name?")
print("Last name? " + lastName)
bday = input("Birthday?")
print("Birthday? " + bday)
book = input("Favorite books?")
print("Favorite books? " + book)
food = input("Favorite foods?")
print("Favorite foods? " + food)
hobby = input("Hobbies?")
print("Hobbies? " + hobby)
sport = input("Sports played?")
print("Sports played" + sport)
InfoDb.append({
"FirstName": firstName,
"LastName": lastName,
"DOB": bday,
"Favorite books": [book],
"Favorite food": food,
"Hobbies": [hobby],
"Sports played": [sport]
})
def print_data(d_rec):
print(d_rec["FirstName"], d_rec["LastName"]) # using comma puts space between values
print("\t", "Birthday 🥳:", d_rec["DOB"]) # \t is a tab indent
print("\t", "Favorite books: ", end="")
print(", ".join(d_rec["Favorite books"]))
print("\t", "Favorite food:", d_rec["Favorite food"])
print("\t", "Hobbies: ", end="")
print(", ".join(d_rec["Hobbies"]))
print("\t", "Sports played: ", end="")
print(", ".join(d_rec["Sports played"]))
def for_loop():
for i in range(len(InfoDb)):
record = InfoDb[i]
print_data(record)
return
for_loop()
Learning
The purpose of lists and dictionaries are to collect information.
Add to dictionaries with .append()
.
Dictionaries consist of key/value pairs. For example, "FirstName"
is a key, while "Lily"
is a value.
Application Programming Interface: Also known as API, it is the process in which data is shared between programs. Lists and dictionaries are the first step to learning about databases.
Code that I learned
d_rec
: Stands for dictionary record. You can use it to call the specific information in the dictionary.
For example, print(d_rec["FirstName"])
would print the specific value in the dictionary of the FirstName
key.