Hacks for 8/30
The three hacks for August 30 on the lesson for lists, dictionaries, iterations, etc.
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": "Paaras",
"LastName": "Purohit",
"DOB": "August 1",
"Residence": "San Diego",
"Email": "paarasp64844@stu.powayusd.com",
"Owns_Cars": ["MercedesBenz", "NissanLeaf"]
})
# Print the data structure
print(InfoDb)
# arrays and indexes. I would have defintely done much more complex programs, but I
# couldn't think of any on the spot that did not require me to import any heavy libraries.
# However, this gets the job done, and is a base for much more complex programs.
def find_even_numbers(array):
nums = []
for i in array:
if i % 2 == 0:
nums.append(i)
else:
continue
return nums
numbers = [5, 19, 3, 24, 4, 15, 9, 10, 6, 11]
print(find_even_numbers(numbers))
-
Would it be possible to output data in reverse order?
Yes, there are two efficient ways to do it. The first is using the reverse() method, or sometimes the reversed() method. both the job done, but the syntax is different. reversed() is used the same way one would use len() or sum() methods. Below is an example of the reverse() method.
nums = [1, 2, 3, 4, 5]
print(list(reversed(nums)))
# The list() method puts it back in list format. Had I not used that method, the output
# would not have been a reversed list in the format we want.
Another way of reversing an output is by using the insert() method. Below is an example of this being done.
list = [10, 11, 12, 13, 14, 15]
result = []
for i in list:
result.insert(0, i)
print(result)
-
Are there other methods that can be performed on lists?
There are so many methods that can be applied to lists. There are too many for just one page, but they usually help with gathering data from lists, and using that data to solve problems, return certain things, and more.
- Could you create new or add to dictionary data set? Could you do it with input? Make a quiz that stores in a List of Dictionaries.
score = 0
colors = {
"red": "blue",
"yellow": "green",
"white": "black",
"purple": "pink",
}
def ask_question(message, dict_item):
global score
question = input(message)
if question == colors[dict_item]:
score += 1
else:
score -= 1
ask_question("What is the opposite of red?", "red")
ask_question("What is the opposite of yellow?", "yellow")
ask_question("What is the opposite of white?", "white")
ask_question("What is the opposite of purple?", "purple")
print("You got " + str(score) + " out of 4")