Variable of String, Integer, Float, List, and Dictionary

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"]
print("langs", langs, type(langs), "length", len(langs))
print("- langs[0]", langs[0], type(langs[0]))

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'] <class 'list'> length 3
- langs[0] Python <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']} <class 'dict'> length 4
- person["name"] John Doe <class 'str'>

Adding Records to the 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": "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"]
})

# Print the data structure
# 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": "Liav",
    "LastName": "Bar",
    "DOB": "August 9",
    "Residence": "San Diego",
    "Email": "liavbar5@gmail.com",
    "Owns_Cars": ["2020-Camry", "2017-Sorento"]
})

# Append to List a 2nd Dictionary of key/values
InfoDb.append({
    "FirstName": "Re'em",
    "LastName": "Ben-Ishai",
    "DOB": "May 11",
    "Residence": "San Diego",
    "Email": "ReemB@powayusd.com",
    "Owns_Cars": ["2021-Kona"]
})

# 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': 'Liav', 'LastName': 'Bar', 'DOB': 'August 9', 'Residence': 'San Diego', 'Email': 'liavbar5@gmail.com', 'Owns_Cars': ['2020-Camry', '2017-Sorento']}, {'FirstName': "Re'em", 'LastName': 'Ben-Ishai', 'DOB': 'May 11', 'Residence': 'San Diego', 'Email': 'ReemB@powayusd.com', 'Owns_Cars': ['2021-Kona']}]

Car Dictionary

Cars = []

# InfoDB is a data structure with expected Keys and Values

# Append to List a Dictionary of key/values related to a person and cars
Cars.append({
    "Brand": "Mercedes-Benz",
    "Nationality of Origin": "German",
    "Basic or Luxury": "Luxury",
    "Model Example": "S-Class",
})

# Append to List a 2nd Dictionary of key/values
Cars.append({
    "Brand": "Toyota",
    "Nationality of Origin": "Japan",
    "Basic or Luxury": "Basic",
    "Model Example": "Camry",
})

# Print the data structure
print(Cars)
[{'Brand': 'Mercedes-Benz', 'Nationality of Origin': 'German', 'Basic or Luxury': 'Luxury', 'Model Example': 'S-Class'}, {'Brand': 'Toyota', 'Nationality of Origin': 'Japan', 'Basic or Luxury': 'Basic', 'Model Example': 'Camry'}]

Using Index in For Loop

list = ["Mercedes","Toyota","Tesla","Honda","Hyundai","Bugatti"] 
for i in list: 
    print(i)
Mercedes
Toyota
Tesla
Honda
Hyundai
Bugatti

Outputing In Reverse Order

list = ["Mercedes","Toyota","Tesla","Honda","Hyundai","Bugatti"] 
for i in list: 
    print(i[::-1])
    print(list[::-1])
sedecreM
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
atoyoT
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
alseT
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
adnoH
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
iadnuyH
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
ittaguB
['Bugatti', 'Hyundai', 'Honda', 'Tesla', 'Toyota', 'Mercedes']
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()

# while loop algorithm contains an initial n and an index incrementing statement (n += 1)
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()
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

Liav Bar
	 Residence: San Diego
	 Birth Day: August 9
	 Cars: 2020-Camry, 2017-Sorento

Re'em Ben-Ishai
	 Residence: San Diego
	 Birth Day: May 11
	 Cars: 2021-Kona

While 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

Liav Bar
	 Residence: San Diego
	 Birth Day: August 9
	 Cars: 2020-Camry, 2017-Sorento

Re'em Ben-Ishai
	 Residence: San Diego
	 Birth Day: May 11
	 Cars: 2021-Kona

Quiz That Stores In a List of Dictionaries

def question_with_response(prompt):
    print("Question: " + prompt) 



word = " "
questions = 4 # number of questions
correct = 0


questions_answers = [{"What command is used to include other functions that were previously developed?" : "import", 
                     "What command is used to evaluate correct or incorrect response in this example?" : "if", 
                     "Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
                     "Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}] # dictionary
# questions_answers.append = ({"What command is used to include other functions that were previously developed?" : "import", 
#                      "What command is used to evaluate correct or incorrect response in this example?" : "if", 
#                      "Each 'if' command contains an '_________' to determine a true or false condition?" : "expression",
#                      "Variables for the values the function needs. Is passed as an argument when the function is called" : "parameters"}) # dictionary

for i in questions_answers:
    for question, answer in i.items():
        question_with_response(question) # printing the questions
        word = input("Answer: ") # variable that connects to the user's input
        if word == answer:  # if the answer provided is correct
            print("You got it right!!")
            correct += 1
        else:  # if the answer provided is wrong
            print("Your answer was wrong")

print(str(correct) + "/" + str(questions)) # correct/len(questions_answers)
Question: What command is used to include other functions that were previously developed?
You got it right!!
Question: What command is used to evaluate correct or incorrect response in this example?
You got it right!!
Question: Each 'if' command contains an '_________' to determine a true or false condition?
You got it right!!
Question: Variables for the values the function needs. Is passed as an argument when the function is called
You got it right!!
4/4