Lists, Dictionaries, Iterations
A thorough demonstration of InfoDB, looping, and other Python components, such as dictionaries and lists.
- Types of Integers/Strings/Variables:
- InfoDb Demonstration:
- Printing InfoDb Data:
- Loops:
- While Loops:
- Recursive Loops:
- Dictionary Quiz:
Types of Integers/Strings/Variables:
In this demonstration, we are going to look at a few types of strings and variables:
- String: There are many different types of Python strings. There are two types of Python strings: single-line and multi-line. Strings can be assigned to variables.
-
Data Types:
- Float: This is data for numerical values that may have a floating decimal point and need to be printed.
- Integer: These are whole numbers that can be defined and printed. In the below demonstration, you will see many integers being assigned to variables, such as my age and APEL grade.
- List: Lists of multiple data values.
- Dictionary: An assortment of key-value pairs that are stored in a dictionary.
- Strings: (explained above)
import random
# type string
name = "Vardaan Sinha"
print("Name:", name, type(name))
# type integer
age = 15
print("Age:", age, type(age))
# float variable
apelgrade = 75
print("APEL Grade:", apelgrade, type(apelgrade))
# type list
classes = ["AP Physics", "AP Stats", "AP CSP", "APEL", "US History"]
print("Classes:", classes, type(classes))
print("Period 3 Class:", classes[2], type(classes[2]))
# reversed list
langs = ["C", "Python", "Java", "HTML", "JavaScript"]
langs.reverse()
print('\nReversed Langs:'),
print(', '.join(langs))
# randomized order with random module
random.shuffle(classes)
print('\nRandom Class Order:'),
print(', '.join(classes))
InfoDb Demonstration:
InfoDb is a way to gather dictionary values into a single database. In the following demonstration, you will see that Samit and I have gathered each other's personal information, such as first name, last name, email, and many other components, and stored them as dictionary values using InfoDb.
Using the .append() method can be used to add multiple inputs.
InfoDb = []
# first dictionary value
InfoDb.append({
"FirstName": "Samit",
"LastName": "Poojary",
"DOB": "February 24th, 2006",
"Email": "samitpoojary@gmail.com",
"Height": "5'7",
"Hobbies": ["Playing Soccer", "Fantasy Football", "Programming"]
})
# adding second dictionary value
InfoDb.append({
"FirstName": "Akshat",
"LastName": "Parikh",
"DOB": "December 28th, 2005",
"Email": "akshat1228@gmail.com",
"Height": "6'",
"Hobbies": ["Playing Basketball", "Trading", "Programming"]
})
# This prints the dictionary keys and corresponding values.
print(InfoDb)
def print_data(homie):
print("Name:", homie["FirstName"], homie["LastName"])
print("Birthday:", homie["DOB"])
print("Email:", homie["Email"])
print("Height:", homie["Height"])
print("Hobbies:", homie["Hobbies"])
print()
# This prints the data for the SECOND dictionary value, as Python always starts from 0 instead of 1.
print_data(InfoDb[1])
def for_loop():
for homie in InfoDb:
print_data(homie)
# Prints the data from InfoDb based on the index falling in the specified range.
def indexed_for_loop():
print("For Loop\n")
for i in range(len(InfoDb)):
print_data(InfoDb[i])
for_loop()
indexed_for_loop()
def while_loops():
# This sets the initial value for the index at 0.
i = 0
# When i < dictionary length, the data from InfoDb will be printed.
while i < len(InfoDb):
print_data(InfoDb[i])
i = i + 1
# Printing the while loop's output
print("While Loop\n")
while_loops()
def recursive(i):
if i >= len(InfoDb):
return
print_data(InfoDb[i])
return recursive(i + 1)
# Printing the 2nd dictionary term.
recursive(1)
import getpass
# Setting INTEGER variables: setting the correct answers to START at 0.
questions = 3
correct = 0
print("This is a Python-centric quiz that will allow you to test your knowledge of the AP CSP content thus far.")
# Setting 'q' and 'a' parameters
def question_answer(q, a):
# prints q
print("Question: " + q)
# This is the user's input/response to the question.
resp = input()
print("Answer: " + resp)
# The correct +=1 adds a correct answer.
if a == resp.lower():
print("Correct!")
global correct
correct +=1
else:
print("Incorrect!")
return resp
q1 = question_answer("Which type of loop is used to call repeatedly?", "recursive")
q2 = question_answer("A Python ___ stores associated keys and values.", "dictionary")
q3 = question_answer("What type of variable can contain numbers with 'floating' decimal values?", "float")
# If statement based on the amount of questions that user gets correct
if correct < 3:
print(f'You got only {correct}/3 correct.')
# getpass module called to get username to tell the user their score and percentage.
print (getpass.getuser() + " that's a " + str((correct / questions)* 100)+ "%")
print("The answers in order were 'recursive', 'dictionary', and 'float' for next time.")
else:
print(f'Great job! You got a 100%.')
# DICTIONARY COMPONENT
# This allows for the user's answers to be recorded and given back to them after they complete the quiz.
PyQuiz = {
"Q1": q1,
"Q2": q2,
"Q3": q3
}
print("Your recorded answers for review:", PyQuiz)