Python Tech Talk
Python lists, dictionaries, and iterations hacks.
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"]
})
# Append a 3rd dictionary
InfoDb.append({
"FirstName": "Kaiden",
"LastName": "Do",
"DOB": "September 10",
"Residence": "San Diego",
"Email": "kaidend@powayusd.com",
"Owns_Cars": ["Nissan Cube"]
})
# Append a 4th dictionary
InfoDb.append({
"FirstName": "Samit",
"LastName": "Poojary",
"DOB": "February 24",
"Residence": "San Diego",
"Email": "samitp@powayusd.com",
"Owns_Cars": ["Nissan Altima"]
})
import getpass, sys
FirstName = input("Enter your first name: ")
LastName = input("Enter your last name: ")
DOB = input("Enter your date of birth: ")
Residence = input("Enter your city: ")
Email = input("Enter your email: ")
Owns_Cars = input("Enter your car or dream car: ")
# Append a 5th dictionary with inputted values
InfoDb.append({
"FirstName": FirstName,
"LastName": LastName,
"DOB": DOB,
"Residence": Residence,
"Email": Email,
"Owns_Cars": Owns_Cars
})
# Print the data structure
print(InfoDb)
Fruits = []
# append apple dictionary
Fruits.append({
"Name": "Apple",
"Color": "Red",
"Size": "Normal"
})
# append orange dictionary
Fruits.append({
"Name": "Orange",
"Color": "Orange",
"Size": "Normal"
})
# append pineapple dictionary
Fruits.append({
"Name": "Pineapple",
"Color": "Yellow",
"Size": "Large"
})
# append pear dictionary
Fruits.append({
"Name": "Pear",
"Color": "Green",
"Size": "Normal"
})
# append blueberry dictionary
Fruits.append({
"Name": "Blueberry",
"Color": "Blue",
"Size": "Small"
})
# append grape dictionary
Fruits.append({
"Name": "Grape",
"Color": "Purple",
"Size": "Small"
})
# prints the whole list
print(Fruits)
def print_data(d_rec):
print(d_rec["Name"])
print("\t", "Color: ", d_rec["Color"])
print("\t", "Size: ", d_rec["Size"])
print()
def for_loop():
print("For Loop\n")
for record in Fruits: # loops for everything in Fruits
print_data(record) # calls the print formatter
for_loop() # calls the function
def rev_for_loop():
print("For Loop with Reversed Outputs\n")
for record in reversed(Fruits): # reverses the list
print_data(record)
rev_for_loop() # calls the function
import random
shuffledFruits = random.sample(Fruits, len(Fruits)) # randomly shuffles the list
def while_loop():
print("While Loop\n")
i = 0
while i < len(shuffledFruits): # while i is less than the length of the list, it loops
record = shuffledFruits[i]
print_data(record)
i += 1 # adds 1 to i each loop
return
while_loop() # calls the function
def recursive_loop(x): # keeps on looping if x is there
if x < len(Fruits): # if x is less than the length of the list, it will do the following
# if x is greater it will stop
record = Fruits[x]
print_data(record)
recursive_loop(x + 1) # makes it loop again
return
# printing fruits in order using a recursive loop
print("Recursive Loop\n")
recursive_loop(0) # calls the function
questions = 6
correct = 0
def question_and_answer(prompt, answer): # function that has the prompt and answer as parameters
print("Question: " + prompt) # displays the prompt
msg = input()
print("Answer: " + msg) # displays the user's answer
if answer == msg.lower(): # compares the user's answer to correct answer, no regard to case
print("Correct Answer") # if correct, prints that it is correct
global correct
correct += 1
else:
print ("Incorrect Answer") # if not, prints that it is incorrect
return msg # returns the user's answer
# these add the user's answers to a list
Q1 = question_and_answer("What is 2+2?", "4")
Q2 = question_and_answer("What is the capital of California?", "sacramento")
Q3 = question_and_answer("What continent is India in?", "asia")
Q4 = question_and_answer("When is Christmas?", "december 25")
Q5 = question_and_answer("What number is Aaron Donald?", "99")
Q6 = question_and_answer("What color is the sky", "blue")
print(correct, "answers correct or", correct*100/questions,"%") # calculates the user's score
Quiz = {
"Q1": Q1,
"Q2": Q2,
"Q3": Q3,
"Q4": Q4,
"Q5": Q5,
"Q6": Q6
}
print("Your answers:",Quiz) # prints the user's answers