# Sample of Python Variables
# variable of type string
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "Anagha Ashtewale"
print("name", name, type(name))
What is the variable name/key? value? type? primitive or collection, why?
name Anagha Ashtewale <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'>
# 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"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[0]", langs[0], type(langs[0]))
print("- langs[1]", langs[1], type(langs[1]))
print("- langs[2]", langs[2], type(langs[2]))
What is variable name/key? value? type? primitive or collection?
What is different about the list output?
langs ['Python', 'JavaScript', 'Java'] <class 'list'> length 3
- langs[0] Python <class 'str'>
- langs[1] JavaScript <class 'str'>
- langs[2] Java <class 'str'>
# 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"]))
print('- person["age"]', person["age"], type(person["age"]))
print('- person["score"]', person["score"], type(person["score"]))
print('- person["langs"]', person["langs"], type(person["langs"]))
What is the variable name/key? value? type? primitive or collection, why?
What is different about the dictionary output?
person {'name': 'Anagha Ashtewale', 'age': 17, 'score': 90.0, 'langs': ['Python', 'JavaScript', 'Java']} <class 'dict'> length 4
- person["name"] Anagha Ashtewale <class 'str'>
- person["age"] 17 <class 'int'>
- person["score"] 90.0 <class 'float'>
- person["langs"] ['Python', 'JavaScript', 'Java'] <class 'list'>
# Define an empty List called 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": "Anagha",
"LastName": "Ashtewale",
"DOB": "December 12",
"Residence": "San Diego",
"Email": "aashtewale@gmail.com",
"Owns_Cars": ["Audi Q5", "Toyota Camery"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Tanvi",
"LastName": "Pampati",
"DOB": "March 30",
"Residence": "San Diego",
"Email": "tanvi.pampati@gmail.com",
"Owns_Cars": ["Tesla Model 3"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Shane",
"LastName": "Lopez",
"DOB": "February 27",
"Residence": "San Diego",
"Email": "???@powayusd.com",
"Owns_Cars": ["2021-Insight"]
})
# Print the data structure
print(InfoDb)
[{'FirstName': 'Anagha', 'LastName': 'Ashtewale', 'DOB': 'December 12', 'Residence': 'San Diego', 'Email': 'aashtewale@gmail.com', 'Owns_Cars': ['Audi Q5', 'Toyota Camery']}, {'FirstName': 'Tanvi', 'LastName': 'Pampati', 'DOB': 'March 30', 'Residence': 'San Diego', 'Email': 'tanvi.pampati@gmail.com', 'Owns_Cars': ['Tesla Model 3']}, {'FirstName': 'Shane', 'LastName': 'Lopez', 'DOB': 'February 27', 'Residence': 'San Diego', 'Email': '???@powayusd.com', 'Owns_Cars': ['2021-Insight']}]
# This jupyter cell has dependencies on one or more cells above
# print function: given a dictionary of InfoDb content
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) # call to function
for_loop() # call to function
For loop output
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi Q5, Toyota Camery
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Shane Lopez
Residence: San Diego
Birth Day: February 27
Cars: 2021-Insight
While loop output
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi Q5, Toyota Camery
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Shane Lopez
Residence: San Diego
Birth Day: February 27
Cars: 2021-Insight