Class notes

A type is a string or list.

# variable of type string
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
name = "John Doe"
print("name", name, type(name))

print()


# variable of type integer
print("What is the variable name/key?", "value?", "type?", "primitive or collection, why?")
age = 18
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"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[2]", langs[2], type(langs[2]))

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 John Doe <class 'str'>

What is the variable name/key? value? type? primitive or collection, why?
age 18 <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[2] Java <class 'str'>

What is the variable name/key? value? type? primitive or collection, why?
What is different about the dictionary output?
person {'name': 'John Doe', 'age': 18, 'score': 90.0, 'langs': ['Python', 'JavaScript', 'Java', 'Bash']} <class 'dict'> length 4
- person["name"] John Doe <class 'str'>

In this code, the key is name, the value is John Doe, and the type is string in the second line, the key is age, the value is 18, and the type is integer in the third lin, the key is score, the value is 90.0, and the type is floating in the fourth line, the type is list. a list uses brackets in the fifth line, the type is dictionary. a dictionary uses curly brackets

.append adds to a list. ex: langs.append ("bash") would add bash to the langs list

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": "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"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Sunny",
    "LastName": "Naidu",
    "DOB": "August 2",
    "Residence": "Temecula",
    "Email": "snaidu@powayusd.com",
    "Owns_Cars": ["4Runner"]
})

InfoDb.append({
    "FirstName": "AJ",
    "LastName": "Ruiz",
    "DOB": "May 18",
    "Residence": "San Diego",
    "Email": "ajruizrocks2006@gmail.com",
    "Own_Cars": ["None", "None"]
})

# Print the data structure
print(InfoDb)
[{'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']}, {'FirstName': 'Sunny', 'LastName': 'Naidu', 'DOB': 'August 2', 'Residence': 'Temecula', 'Email': 'snaidu@powayusd.com', 'Owns_Cars': ['4Runner']}, {'FirstName': 'AJ', 'LastName': 'Ruiz', 'DOB': 'May 18', 'Residence': 'San Diego', 'Email': 'ajruizrocks2006@gmail.com', 'Own_Cars': ['None', 'None']}]
# 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)

for_loop()
For loop output

John Mortensen
	 Residence: San Diego
	 Birth Day: October 21
	 Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac

Sunny Naidu
	 Residence: Temecula
	 Birth Day: August 2
	 Cars: 4Runner

AJ Ruiz
	 Residence: San Diego
	 Birth Day: May 18
	 Cars: 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb Cell 5 in <cell line: 19>()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a>     for record in InfoDb:
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a>         print_data(record)
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=18'>19</a> for_loop()

/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb Cell 5 in for_loop()
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> print("For loop output\n")
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a> for record in InfoDb:
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a>     print_data(record)

/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb Cell 5 in print_data(d_rec)
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> print("\t", "Birth Day:", d_rec["DOB"])
      <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> print("\t", "Cars: ", end="")  # end="" make sure no return occurs
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> print(", ".join(d_rec["Owns_Cars"]))  # join allows printing a string list with separator
     <a href='vscode-notebook-cell://wsl%2Bubuntu/home/kkcbal/vscode/madeacopy/_notebooks/2022-08-30-pwut.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=9'>10</a> print()

KeyError: 'Owns_Cars'

My code

msg = ("message")
List = ["men", 14, msg]
listTwo = ["list1", "list2", "list3"]
print(List)
listTwo.append ("list4")
print(listTwo)
print(listTwo[0])

print(listTwo[1] + " contains strings, " + listTwo[0] + " contains variables and numbers too.")

UserInput = input("Add ___ to list1: ")
List.append (UserInput)
print(List)
['men', 14, 'message']
['list1', 'list2', 'list3', 'list4']
list1
list2 contains strings, list1 contains variables and numbers too.
['men', 14, 'message', 'My input']