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:
    1. Float: This is data for numerical values that may have a floating decimal point and need to be printed.
    2. 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.
    3. List: Lists of multiple data values.
    4. Dictionary: An assortment of key-value pairs that are stored in a dictionary.
    5. 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))
Name: Vardaan Sinha <class 'str'>
Age: 15 <class 'int'>
APEL Grade: 75 <class 'int'>
Classes: ['AP Physics', 'AP Stats', 'AP CSP', 'APEL', 'US History'] <class 'list'>
Period 3 Class: AP CSP <class 'str'>

Reversed Langs:
JavaScript, HTML, Java, Python, C

Random Class Order:
AP CSP, APEL, US History, AP Physics, AP Stats

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)
[{'FirstName': 'Samit', 'LastName': 'Poojary', 'DOB': 'February 24th, 2006', 'Email': 'samitpoojary@gmail.com', 'Height': "5'7", 'Hobbies': ['Playing Soccer', 'Fantasy Football', 'Programming']}, {'FirstName': 'Akshat', 'LastName': 'Parikh', 'DOB': 'December 28th, 2005', 'Email': 'akshat1228@gmail.com', 'Height': "6'", 'Hobbies': ['Playing Basketball', 'Trading', 'Programming']}]

Printing InfoDb Data:

Using the print function, the data for the dictionary keys and values are formatted in a particular way to avoid clutter.

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])
Name: Akshat Parikh
Birthday: December 28th, 2005
Email: akshat1228@gmail.com
Height: 6'
Hobbies: ['Playing Basketball', 'Trading', 'Programming']

Loops:

As per the name, loops are used to execute statement(s) more than once.

For loops are loops for an iterable object, or an object that can be looped over such as a list, dictionary, or string. These loops can perform the exact same action for each iterable object.

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()
Name: Samit Poojary
Birthday: February 24th, 2006
Email: samitpoojary@gmail.com
Height: 5'7
Hobbies: ['Playing Soccer', 'Fantasy Football', 'Programming']

Name: Akshat Parikh
Birthday: December 28th, 2005
Email: akshat1228@gmail.com
Height: 6'
Hobbies: ['Playing Basketball', 'Trading', 'Programming']

For Loop

Name: Samit Poojary
Birthday: February 24th, 2006
Email: samitpoojary@gmail.com
Height: 5'7
Hobbies: ['Playing Soccer', 'Fantasy Football', 'Programming']

Name: Akshat Parikh
Birthday: December 28th, 2005
Email: akshat1228@gmail.com
Height: 6'
Hobbies: ['Playing Basketball', 'Trading', 'Programming']

While Loops:

These are loops that repeat a particular statement when a specified condition is met (true).

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()
While Loop

Name: Samit Poojary
Birthday: February 24th, 2006
Email: samitpoojary@gmail.com
Height: 5'7
Hobbies: ['Playing Soccer', 'Fantasy Football', 'Programming']

Name: Akshat Parikh
Birthday: December 28th, 2005
Email: akshat1228@gmail.com
Height: 6'
Hobbies: ['Playing Basketball', 'Trading', 'Programming']

Recursive Loops:

These are loops where a function can call itself. It keeps doing so until a particular condition is no longer met. A prime example of this is shown in the below code.

def recursive(i):
    if i >= len(InfoDb):
        return
    print_data(InfoDb[i])
    return recursive(i + 1)
# Printing the 2nd dictionary term.
recursive(1)
Name: Akshat Parikh
Birthday: December 28th, 2005
Email: akshat1228@gmail.com
Height: 6'
Hobbies: ['Playing Basketball', 'Trading', 'Programming']

Dictionary Quiz:

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)
This is a Python-centric quiz that will allow you to test your knowledge of the AP CSP content thus far.
Question: Which type of loop is used to call repeatedly?
Answer: while
Incorrect!
Question: A Python ___ stores associated keys and values.
Answer: dictionary
Correct!
Question: What type of variable can contain numbers with 'floating' decimal values?
Answer: float
Correct!
You got only 2/3 correct.
vardaan that's a 66.66666666666666%
The answers in order were 'recursive', 'dictionary', and 'float' for next time.
Your recorded answers for review: {'Q1': 'while', 'Q2': 'dictionary', 'Q3': 'float'}