# Sample of Python Variables
# variable of type string
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "Tanvi Pampati"
print("name", name, type(name))
What is the variable name/key? value? type? primitive or collection, why?
name Tanvi Pampati <class 'str'>
What is the variable name/key? value? type? primitive or collection, why?
age 16 <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': 'Tanvi Pampati', 'age': 16, 'score': 90.0, 'langs': ['Python', 'JavaScript', 'Java']} <class 'dict'> length 4
- person["name"] Tanvi Pampati <class 'str'>
- person["age"] 16 <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": "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": "Anagha",
"LastName": "Ashtewale",
"DOB": "December 12",
"Residence": "San Diego",
"Email": "aashtewale@gmail.com",
"Owns_Cars": ["Audi", "Toyota"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Drishya",
"LastName": "Mody",
"DOB": "July 15",
"Residence": "San Diego",
"Email": "modydrishya@gmail.com",
"Owns_Cars": ["Mazda", "Tesla"]
})
# Append to List a 2nd Dictionary of key/values
InfoDb.append({
"FirstName": "Miheer",
"LastName": "Purandare",
"DOB": "August 3",
"Residence": "San Diego",
"Email": "miheerpurandare07@gmail.com",
"Owns_Cars": ["Acura", "Tesla"]
})
# Print the data structure
print(InfoDb)
[{'FirstName': 'Tanvi', 'LastName': 'Pampati', 'DOB': 'March 30', 'Residence': 'San Diego', 'Email': 'tanvi.pampati@gmail.com', 'Owns_Cars': ['Tesla Model 3']}, {'FirstName': 'Anagha', 'LastName': 'Ashtewale', 'DOB': 'December 12', 'Residence': 'San Diego', 'Email': 'aashtewale@gmail.com', 'Owns_Cars': ['Audi', 'Toyota']}, {'FirstName': 'Drishya', 'LastName': 'Mody', 'DOB': 'July 15', 'Residence': 'San Diego', 'Email': 'modydrishya@gmail.com', 'Owns_Cars': ['Mazda', 'Tesla']}, {'FirstName': 'Miheer', 'LastName': 'Purandare', 'DOB': 'August 3', 'Residence': 'San Diego', 'Email': 'miheerpurandare07@gmail.com', 'Owns_Cars': ['Acura', 'Tesla']}]
# 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
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi, Toyota
Drishya Mody
Residence: San Diego
Birth Day: July 15
Cars: Mazda, Tesla
Miheer Purandare
Residence: San Diego
Birth Day: August 3
Cars: Acura, Tesla
While loop output
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi, Toyota
Drishya Mody
Residence: San Diego
Birth Day: July 15
Cars: Mazda, Tesla
Miheer Purandare
Residence: San Diego
Birth Day: August 3
Cars: Acura, Tesla
Recursive loop output
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi, Toyota
Drishya Mody
Residence: San Diego
Birth Day: July 15
Cars: Mazda, Tesla
Miheer Purandare
Residence: San Diego
Birth Day: August 3
Cars: Acura, Tesla
# 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="" makes sure no return occurs
print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
print()
# for loop algorithm iterates through InfoDb using iteration
def for_loop_with_index():
print("For loop with index output\n")
index = 0
for record in InfoDb:
print(f"Record {index + 1}:")
print_data(record) # call to function
index += 1
for_loop_with_index() # call to function
For loop with index output
Record 1:
Tanvi Pampati
Residence: San Diego
Birth Day: March 30
Cars: Tesla Model 3
Record 2:
Anagha Ashtewale
Residence: San Diego
Birth Day: December 12
Cars: Audi, Toyota
Record 3:
Drishya Mody
Residence: San Diego
Birth Day: July 15
Cars: Mazda, Tesla
Record 4:
Miheer Purandare
Residence: San Diego
Birth Day: August 3
Cars: Acura, Tesla