Lists
An introduction to Data Abstraction using Python Lists [] and Python Dictionaries {}.
InfoDb = [] #this is the empty dictionary that I will append things to
# 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"]
#})
#InfoDb.append({
# "FirstName": "Sunny",
# "LastName": "Naidu",
# "DOB": "August 2",
# "Residence": "Temecula",
# "Email": "snaidu@powayusd.com",
# "Owns_Cars": ["4Runner"],
#})
InfoDb.append({
"FirstName": "Soham",
"LastName": "Kamat",
"DOB": "March 9",
"Residence": "San Diego",
"Email": "sohamk10039@stu.powayusd.com",
"Hair_Color": "Black",
"Grade": "10th",
"Age": "15"
"Owns_Cars": ["None"],
})
InfoDb.append({
"FirstName": "Ryan",
"LastName": "Hakimipour",
"DOB": "June 15",
"Residence": "San Diego",
"Email": "RyanHaki@gmail.com",
"Hair_Color": "Black",
"Grade": "10th",
"Age": "15"
"Owns_Cars": ["None"],
})
InfoDb.append({
"FirstName": "Aniket",
"LastName": "Chakradeo",
"DOB": "February 20",
"Residence": "San Diego",
"Email": "sohamk10039@stu.powayusd.com",
"Hair_Color": "Black",
"Grade": "10th",
"Age": "15"
"Owns_Cars": ["None"],
})
InfoDb.append({
"FirstName": "Lucas",
"LastName": "Kamat",
"DOB": "December 16",
"Residence": "San Diego",
"Email": "lucasmoore@gmail.com",
"Hair_Color": "Blonde",
"Grade": "10th",
"Age": "15"
"Owns_Cars": ["None"],
})
#print(InfoDb) #Commented because the code below shows the information in a neater way
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", "")
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()
def recursive_loop(i):
if i < len(InfoDb):
record = InfoDb[i]
print_data(record)
recursive_loop(i + 1)
return
print("\nRecursive loop output:\n")
recursive_loop(0)
For Loop in action
CandyTypes = ["Skittles", "M&Ms", "Reeses Pieces", "Air Heads", "Dum Dums", "Hersheys", "Gummy Worms", "Twizzlers", "Jolly Ranchers", "Dots", "Nerds", "Crunch"]
# Using a for loop to iterate through list
for i in CandyTypes:
print(i)
Function that prints an input in reverse and tells you if it is a palindrome
def isPalindrome(s): #function that prints the input and prints the input backwards
print(s)
print(s[::-1])
return s == s[::-1]
s = input("Enter a word: ")
ans = isPalindrome(s)
if ans:
print(s + " is a palindrome")
else:
print(s + " is not a palindrome")
My Python Quiz
def PythonQuiz(prompt): # this is the function that prints the questions and accepts the input
global word
print ("Question: " + prompt)
word = input()
return word
questions_number = 5 #this is the number of questions
correct_answer = 0
print("Hello, you will be asked " + str(questions_number) + " short questions about common conversions.") #This is the question that starts the quiz
MyQuiz=[]
MyQuiz.append({ #These are the questions
"How many centimeters are in 12 meters": "1200",
"How many kilometers is 100,000 centimeters?": "1",
"How many yards are in 27 feet?": "9",
"How many milligrams are in 25 grams?": "25000",
"What is the unit used to measure time?": "Second",
})
# for loop checks answers with the value in the dictionary
for dict in MyQuiz:
for questions, answers in dict.items():
PythonQuiz(questions)
if word == answers:
print(input() + " is correct")
correct_answer += 1
else:
print(input() +" is incorrect")
print("You scored " + str(correct_answer) + "/" + str(questions_number) + ". Good Job!")