InfoDb

InfoDb is a dictionary inside of a list. The example below has parts from the class repository. The dictionary information is appended into a list, making an InfoDb. I appended my own information and I also coded it so that people could input their own information.

InfoDb = []

# 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"]
})
# Appended my own info
InfoDb.append({
    "FirstName": "Tay",
    "LastName": "Kim",
    "DOB": "May 13",
    "Residence": "San Diego",
    "Email": "taykimpro@gmail.com",
    "Owns_Cars": ["None"]
})
# People can input their own info
InfoDb.append({
    "FirstName": input("name "),
    "LastName": input("last name "),
    "DOB": input("DOB "),
    "Residence": input("residence "),
    "Email": input("email "),
})

# 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': 'Tay', 'LastName': 'Kim', 'DOB': 'May 13', 'Residence': 'San Diego', 'Email': 'taykimpro@gmail.com', 'Owns_Cars': ['None']}, {'FirstName': 'Rob', 'LastName': 'Smith', 'DOB': 'September 24', 'Residence': '4S Ranch', 'Email': 'robbydobby@gmail.com'}]

For Loops and Index

Using for loops and iterating through using index in python. In this simple example, I created a list of car brands and then I used a for loop to iterate through it. Then, I printed all the values that I iterated through.

Car_Brands = ["Honda", "Hyundai", "Kia", "Tesla", "Lamborghini", "Ferrari", "Bugatti", "Toyota", "Porsche", "Mustang", "BMW", "Mercedes"]
# Using a for loop to iterate through list
for i in Car_Brands:
    print(i)
# printing all the values
Honda
Hyundai
Kia
Tesla
Lamborghini
Ferrari
Bugatti
Toyota
Porsche
Mustang
BMW
Mercedes

Outputting in Reverse Order

It is possible to output data in a reverse order. In this example, I determine if an inputted word is a palindrome. I first iterate through the inputted word the opposite way, or reverse order, and then if the reversed order is equal to the regular order, then I output that the word is a palindrome. If not, then I say the inputted word is not a palindrome.

def is_palindrome(prompt):
    print(prompt[::-1]) # iterates through the word in the reverse order
    print(prompt)
    return prompt[::-1] == prompt # returns if the reverse order is the same as the regular order

word = input("Enter a word: ")
if is_palindrome(word.lower()) is True: # If the reverse is the same as regular
    print("{0} is a palindrome!".format(word)) 
else:
    print("The provided word is not a palindrome.")
eyb
bye
The provided word is not a palindrome.

Other list methods

Lists can have a variety of methods performed on them. A couple being .append() or while loop. In this example, I use .append() and .lower() with a for loop. I first make a list that I add dangerous animals. Then, I append another value. I made sure to make all the inputs have weird capitalization. Then, I terated through the range of the length of the list. I also made all the list values lowercase and then I printed all the values.

Dangerous_Animals = ["Box Jellyfish", "African CAPE BuFfalo", "BLAck MambA", "Blue-RINGED octopus"] # made a list with dangerous animals, has weird capitalization
Dangerous_Animals.append("Cone SnAIl") # added a new list value
for i in range(len(Dangerous_Animals)): # iterates through the range of the length of the list
    Dangerous_Animals[i] = Dangerous_Animals[i].lower() # made all the values lowercase
print(Dangerous_Animals)
['box jellyfish', 'african cape buffalo', 'black mamba', 'blue-ringed octopus', 'cone snail']

Using While Loop

While loops can also be used with a list. In this example, I used the same list as before. Then, I defined i as 0. Then I used a while loop so that while i was less than the length of the list, the output was equal to the list at index of i. I printed the output and I incremented i by 1 each time.

Dangerous_Animals = ["Box Jellyfish", "African Cape Buffalo", "Black Mamba", "Blue-ringed Octopus"] # used the same list as before
i = 0 # defined a variable, i
while i < len(Dangerous_Animals): # only runs while i is less than the length of the list
    output = Dangerous_Animals[i] # output equals each value when the list is at index of i
    print(output)
    i += 1 # increments i by 1 each time
Box Jellyfish
African Cape Buffalo
Black Mamba
Blue-ringed Octopus

Adding to a Dictionary

You can also add or create new keys and values to a dictionary data set. This can also be done with input. In the example, I first created a dictionary with pokemon names as the keys and their type as values. I then added a new key and value and I also used input to add a new value and new key.

My_Dict = {
    "Pikachu": "Electric-type",
    "Charizard": "Fire-type",
    "Blastoise": "Water-type",
    "Venusaur": "Grass-type",
    "Snorlax": "Normal-type",
    "Garchomp": "Dragon-type",
    "Lucario": "Fighting-type",
    "Darkrai": "Dark-type",
    "Arceus": "Normal-type",
} # defined a new dictionary
print(My_Dict)
My_Dict["Reshiram"] = "Dragon-type" #Added new key and value into dictionary
print(My_Dict)
My_Dict["Articuno"] = input() # Added new value with input
print(My_Dict)
{'Pikachu': 'Electric-type', 'Charizard': 'Fire-type', 'Blastoise': 'Water-type', 'Venusaur': 'Grass-type', 'Snorlax': 'Normal-type', 'Garchomp': 'Dragon-type', 'Lucario': 'Fighting-type', 'Darkrai': 'Dark-type', 'Arceus': 'Normal-type'}
{'Pikachu': 'Electric-type', 'Charizard': 'Fire-type', 'Blastoise': 'Water-type', 'Venusaur': 'Grass-type', 'Snorlax': 'Normal-type', 'Garchomp': 'Dragon-type', 'Lucario': 'Fighting-type', 'Darkrai': 'Dark-type', 'Arceus': 'Normal-type', 'Reshiram': 'Dragon-type'}
{'Pikachu': 'Electric-type', 'Charizard': 'Fire-type', 'Blastoise': 'Water-type', 'Venusaur': 'Grass-type', 'Snorlax': 'Normal-type', 'Garchomp': 'Dragon-type', 'Lucario': 'Fighting-type', 'Darkrai': 'Dark-type', 'Arceus': 'Normal-type', 'Reshiram': 'Dragon-type', 'Articuno': 'Ice-type'}

Python Quiz

I made a quiz using the InfoDb, list of dictionary. In this example, I defined a function with prompt as a parameter. I also defined a variable word, so that it was equal to the user's input. I also added 2 new variables, that defined the number of correct answers and the number of questions. I then printed a short intro that said "Hello, you will be asked (the amount of questions) short questions." I then made a list of dictionaries (InfoDb) with the questions as the keys and the answers as the values. I then used a for loop to iterate through all the dictionary keys and values. If the word (user's input) for each question was equal to its respectable answer, then it printed "Answer is correct" and added the number of correct answers by 1. If the word wasn't equal, then an incorrect answer resopnse was pinted. At the end, I printed the amount of questions the user got correct out of the total questions.

def Python_Quiz(prompt): # Defines function that takes 'prompt' as a parameter
    global word # so that word can be called outside function
    print ("Question: " + prompt) # setup for each question
    word = input() # the variable word is the user's input
    return word

questions_number = 5
correct_answer = 0
# 2 variables for the number of questions and number of correct answers

print("Hello, you will be asked " + str(questions_number) + " short questions")
# short intro

My_Quiz = [{
    "What is the answer to this math problem: What is f(4) if f(x) = 3x/x-3": "12",
    "What is the most popular religion in the world?": "Christianity",
    "What is the capital of the U.S?": "Washington D.C.",
    "What is the molar mass of Carbon-12?": "12 g",
    "How many moles of water are in a 3.0 gram sample?": "0.17",
}] # makes a list of dictionaries with questions as the keys and the answers as the values

for dict in My_Quiz: # for all dictionary stuff in the InfoDb
    for questions, answers in dict.items(): # both keys and values
        Python_Quiz(questions) # the prompt is the questions
        if word == answers: # if the user's answer is equal to the correct answer
            print("Answer is correct")
            correct_answer += 1  
        else:
            print("Answer is incorrect")

print("You scored " + str(correct_answer) + "/" + str(questions_number))
# prints how many correct answers out of total questions
Hello, you will be asked 5 short questions
Question: What is the answer to this math problem: What is f(4) if f(x) = 3x/x-3
Answer is correct
Question: What is the most popular religion in the world?
Answer is correct
Question: What is the capital of the U.S?
Answer is correct
Question: What is the molar mass of Carbon-12?
Answer is correct
Question: How many moles of water are in a 3.0 gram sample?
Answer is incorrect
You scored 4/5