InfoDb = []

InfoDb.append({
    "FirstName": "Max",
    "LastName": "Wu",
    "DOB": "January 25",
    "Residence": "San Diego",
    "Email": "maxw37962@gmail.com",
    "Favorite Music Artists": ["Pink Floyd", "The Beatles", "Jay Chou", "ABBA"]
})
print(InfoDb)
[{'FirstName': 'Max', 'LastName': 'Wu', 'DOB': 'January 25', 'Residence': 'San Diego', 'Email': 'maxw37962@gmail.com', 'Favorite Music Artists': ['Pink Floyd', 'The Beatles', 'Jay Chou', 'ABBA']}]
name = "Max Wu"
print("name", name, type(name))

print()


# variable of type integer
age = 16
print("age", age, type(age))

print()

# variable of type float
score = 100.0
print("score", score, type(score))

print()

# variable of type list (many values in one variable)
langs = ["Python", "Html", "CSS", "C", "Java"]
print("langs", langs, type(langs))
print("- langs[3]", langs[4], type(langs[4]))

print()

# variable of type dictionary (a group of keys and values)
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"]))
name Max Wu <class 'str'>

age 16 <class 'int'>

score 100.0 <class 'float'>

langs ['Python', 'Html', 'CSS', 'C', 'Java'] <class 'list'>
- langs[3] Java <class 'str'>

person {'name': 'Max Wu', 'age': 16, 'score': 100.0, 'langs': ['Python', 'Html', 'CSS', 'C', 'Java']} <class 'dict'> length 4
- person["name"] Max Wu <class 'str'>

Reverse List

# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]

# reverse the order of list elements
prime_numbers.reverse()

print('\nReversed List', prime_numbers)
Reversed List [7, 5, 3, 2]

Random List

import random
print("\nHave you ever tried authentic Chinese food? Would you like me to give you a random Chinese food you can order?")
ChineseFood = ["麻婆豆腐", "夫妻肺片", "回锅肉", "梅菜扣肉", "口水鸡"]
randomWord = random.choice(ChineseFood)
print("A chinese food you can order is", randomWord + "!")
Have you ever tried authentic Chinese food? Would you like me to give you a random Chinese food you can order?
A chinese food you can order is 麻婆豆腐!

Slicing A List

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
elements = slice(4 ,10 ,1)
list2 = list1[elements]
print(list2)
[5, 6, 7, 8, 9, 10]

Perform similar operation on every element of the list

If you want to perform a set of operations on every element of a list you can do that using the map() function in Python.

mylist = [1,2,3,4,5]
def multi(x):
    return 10*x
list(map(multi, mylist))
[10, 20, 30, 40, 50]

Merging Two Lists To Form A List

Suppose you have two lists and you want to merge the two lists to form a dictionary i.e. the elements from one list will be the keys and the elements from the other lists will be the values. Using the zip() function in python we can do that task with one line of code.

items = ["footballs", "bats", "gloves"]
price = [100, 40, 80]
dictionary = dict(zip(items, price))
print(dictionary)
{'footballs': 100, 'bats': 40, 'gloves': 80}

List and Dictionaries

InfoDb = []

# Append to List a 2nd Dictionary of key/values
#added records for my gender, phone number, and age
InfoDb.append({
    "FirstName": "Max",
    "LastName": "Wu",
    "Gender": "Male",
    "DOB": "January 25",
    "Residence": "San Diego",
    "Email": "maxw37962@gmail.com",
    "PhoneNumber": "858-568-3019", 
    "Age": "16",
    "Hobbies": ["Ice Hockey", "Piano", "Listening to Music",]
})

#my partner info in dictionary 
InfoDb.append({
    "FirstName": "Nathan",
    "LastName": "Kim",
    "Gender": "Male",
    "DOB": "December 7",
    "Residence": "San Diego",
    "Email": "nathank51687@stu.powayusd.com",
    "PhoneNumber": "470-266-9499", 
    "Age": "16",
    "Hobbies": ["Grinding Sat"]
})



####User's input added to list###
FirstName = input("Enter your first name: ")
LastName = input("Enter your last name: ")
Gender = input("Enter your gender: ")
DOB = input("Enter your birthday: ")
Residence = input("Enter your city: ")
Email = input("Enter your email: ")
PhoneNumber = input("Enter your phone number (with dashes in between): " )
Age = input("Enter your age: ")

#for loop for hobbies, because who only has one hobby ;)
Hobbies=[]
max_length = 3
while len(Hobbies) < max_length: 
    userHobby = input("Enter a hobby you enjoy: ")
    Hobbies.append(userHobby)
print(Hobbies)

    
    
InfoDb.append({
    "FirstName": FirstName,
    "LastName": LastName,
    "Gender": Gender,
    "DOB": DOB,
    "Residence": Residence,
    "Email": Email,
    "PhoneNumber": PhoneNumber,
    "Age": Age, 
    "Hobbies": Hobbies, 
})



# Print the data structure
print(InfoDb)
['Piano', 'Tennis', 'Cats']
[{'FirstName': 'Max', 'LastName': 'Wu', 'Gender': 'Male', 'DOB': 'January 25', 'Residence': 'San Diego', 'Email': 'maxw37962@gmail.com', 'PhoneNumber': '858-568-3019', 'Age': '16', 'Hobbies': ['Ice Hockey', 'Piano', 'Listening to Music']}, {'FirstName': 'Nathan', 'LastName': 'Kim', 'Gender': 'Male', 'DOB': 'December 7', 'Residence': 'San Diego', 'Email': 'nathank51687@stu.powayusd.com', 'PhoneNumber': '470-266-9499', 'Age': '16', 'Hobbies': ['Grinding Sat']}, {'FirstName': 'Lilian', 'LastName': 'Wu', 'Gender': 'Female', 'DOB': 'January 2', 'Residence': 'San Diego', 'Email': 'lilianwu08@gmail.com', 'PhoneNumber': '858-568-3018', 'Age': '14', 'Hobbies': ['Piano', 'Tennis', 'Cats']}]

Formatted Output of Dictionary

def print_data(d_rec):
    print(d_rec["FirstName"], d_rec["LastName"])  # using comma puts space between values
    print("\t", "Gender:", d_rec["Gender"])
    print("\t", "Birth Day:", d_rec["DOB"])
    print("\t", "Residence:", d_rec["Residence"]) # \t is a tab indent
    print("\t", "Email:", d_rec["Email"]) 
    print("\t", "Phone Number:", d_rec["PhoneNumber"]) 
    print("\t", "Age:", d_rec["Age"]) 
    print("\t", "Hobbies: ", end="") 
    print(", ".join(d_rec["Hobbies"]))
    print()


# for loop iterates on length of InfoDb
def for_loop():
    print("Formatted for loop output\n")
    for record in InfoDb:
        print_data(record)

for_loop()
Formatted for loop output

Max Wu
	 Gender: Male
	 Birth Day: January 25
	 Residence: San Diego
	 Email: maxw37962@gmail.com
	 Phone Number: 858-568-3019
	 Age: 16
	 Hobbies: Ice Hockey, Piano, Listening to Music

Nathan Kim
	 Gender: Male
	 Birth Day: December 7
	 Residence: San Diego
	 Email: nathank51687@stu.powayusd.com
	 Phone Number: 470-266-9499
	 Age: 16
	 Hobbies: Grinding Sat

Lilian Wu
	 Gender: Female
	 Birth Day: January 2
	 Residence: San Diego
	 Email: lilianwu08@gmail.com
	 Phone Number: 858-568-3018
	 Age: 14
	 Hobbies: Piano, Tennis, Cats

While Loop

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()
While loop output

Max Wu
	 Gender: Male
	 Birth Day: January 25
	 Residence: San Diego
	 Email: maxw37962@gmail.com
	 Phone Number: 858-568-3019
	 Age: 16
	 Hobbies: Ice Hockey, Piano, Listening to Music

Nathan Kim
	 Gender: Male
	 Birth Day: December 7
	 Residence: San Diego
	 Email: nathank51687@stu.powayusd.com
	 Phone Number: 470-266-9499
	 Age: 16
	 Hobbies: Grinding Sat

Lilian Wu
	 Gender: Female
	 Birth Day: January 2
	 Residence: San Diego
	 Email: lilianwu08@gmail.com
	 Phone Number: 858-568-3018
	 Age: 14
	 Hobbies: Piano, Tennis, Cats

Recursion

def recursive_loop(i):
    if i < len(InfoDb):
        record = InfoDb[i]
        print_data(record)
        recursive_loop(i + 1)
    return
    
print("Recursive loop output\n")
recursive_loop(0)
Recursive loop output

Max Wu
	 Gender: Male
	 Birth Day: January 25
	 Residence: San Diego
	 Email: maxw37962@gmail.com
	 Phone Number: 858-568-3019
	 Age: 16
	 Hobbies: Ice Hockey, Piano, Listening to Music

Nathan Kim
	 Gender: Male
	 Birth Day: December 7
	 Residence: San Diego
	 Email: nathank51687@stu.powayusd.com
	 Phone Number: 470-266-9499
	 Age: 16
	 Hobbies: Grinding Sat

Lilian Wu
	 Gender: Female
	 Birth Day: January 2
	 Residence: San Diego
	 Email: lilianwu08@gmail.com
	 Phone Number: 858-568-3018
	 Age: 14
	 Hobbies: Piano, Tennis, Cats

Quiz Using Dictionary

from re import S


questions = 5 #number of quiz questions
correct = 0  
user_input = 0


print("This is a 5 question trivia quiz, good luck!")


def question_and_answer(prompt, answer):
    global correct
    print("Question: " + prompt) 
    user_input = input() #takes user's input as variable msg 
    print("Answer: " + user_input) 
    
    user_input = user_input.lower() #convert user's input to lowercase
    answer = answer.lower() # Force answer to lowercase as well

    if answer == user_input: 
        print("Correct Answer")
        correct += 1 
    else: 
        print ("Incorrect Answer")
        #print ("Answer : " + answer)
        #print ("User Input is : " + user_input)
    return user_input 

question_1 = question_and_answer("How many bones are in the human body?", "206")
question_2 = question_and_answer("What is the hardest natural substance on Earth?", "Diamond")
question_3 = question_and_answer("What is the most abundant gas in the Earth's atmosphere?", "Nitrogen")
question_4 = question_and_answer("What is the biggest planet in our solar system?", "Jupiter")
question_5 = question_and_answer("Which famous British physicist wrote A Brief History of Time?", "Stephen Hawking")

if correct < 3:
    print(f'You scored {correct} correct answers out of 5, better luck next time!')
elif correct < 5:
    print(f'You scored {correct} correct answers out of 5, nice try!')
else: 
    print(f'You scored {correct} correct answers out of 5, nice going you trivia star!')
This is a 5 question trivia quiz, good luck!
Question: How many bones are in the human body?
Answer: 206
Correct Answer
Question: What is the hardest natural substance on Earth?
Answer: diamond
Correct Answer
Question: What is the most abundant gas in the Earth's atmosphere?
Answer: nitrogen
Correct Answer
Question: What is the biggest planet in our solar system?
Answer: jupiter
Correct Answer
Question: Which famous British physicist wrote A Brief History of Time?
Answer: stephen hawking
Correct Answer
You scored 5 correct answers out of 5, nice going you trivia star!