Hacks Unit 3.1.1

name = "Grace"
#Uses an integer
grade = 11
#Uses a list
classes = ["APUSH", "APEL", "CSP", "AP Calculus"]

print(f"Hi, my name is {name}. I am in {grade}th grade.")
print("I am taking the classes")
print(*classes, sep = ", ")
Hi, my name is Grace. I am in 11th grade.
I am taking the classes
APUSH, APEL, CSP, AP Calculus

Hacks Unit 3.1.2

  • an assignment operator is a symbol that assigns a value to a variable
  • in College Board, the assignment variable is ->
  • A variable, x, is initially given a value of 15. Later on, the value for x is changed to 22. If you print x, would the command display 15 or 22? Answer: 22

Hacks Unit 3.2.1

  • a list is a sequence of finite strings that surrounded by square brackets and separated by commas
  • an element is the individual strings in a list
  • the easiest way to reference an element is to index the list or string, which is done by list[0]
  • an example of a string is "hello world"
foods = ["pizza", "watermelon", "sushi", "fried chicken"]

#index each element in the list
for i in range(len(foods)):
    print(foods[i])
pizza
watermelon
sushi
fried chicken

Hacks 3.2.2

num1 = input("Input a number. ")
num2 = input("Input a number. ")
num3 = input("Input a number. ")
add = input("How much would you like to add? ")

# Add code in the space below
num1 = int(num1)
num2 = int(num2)
num3 = int(num3)

numlist = [num1, num2, num3]

# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.

for i in numlist:
    numlist[i-1] += int(add)

print(numlist)
[7, 8, 9]

Hacks 3.2.3

import getpass, sys

def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input()
    return msg

questions = 4
correct = 0

print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_with_response("Are you ready to take a test?")

rsp = question_with_response("The purpose of lists and dictionaries are to manage the ____ of a program")
if rsp == "complexity":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Lists are a form of data ______")
if rsp == "abstraction":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!")

rsp = question_with_response("Which brackets are used to assign values to a variable to make a list?")
if rsp == "[]":
    print(rsp + " is correct!")
    correct += 1
else:
    print(rsp + " is incorrect!") 

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Hello, gracewang running /opt/homebrew/opt/python@3.10/bin/python3.10
You will be asked 4 questions.
Question: Are you ready to take a test?
Question: The purpose of lists and dictionaries are to manage the ____ of a program
complexity is correct!
Question: Lists are a form of data ______
abstraction is correct!
Question: Which brackets are used to assign values to a variable to make a list?
[] is correct!
gracewang you scored 3/4
foods = ["pizza", "hot dog", "sushi", "strawberry", "sandwich"]
print(*foods, sep=" ")
pizza hot dog sushi strawberry sandwich
  • lists are better to use than strings in some cases because it reduces clutter, for example, if you wanted to print all the items in a list, typing the previous example out would require 6 lines while the simplified line only requires 2
class1 = "CSP"
class2 = "APEL"
class3 = "APUSH"
class4 = "AP Calc BC"
class5 = "AP Physics"
print(class1, class2, class3, class4, class5)

#short way
classes = ["CSP", "APEL", "APUSH", "AP Calc BC", "AP Physics"]
print(*classes, sep=" ")
CSP APEL APUSH AP Calc BC AP Physics
CSP APEL APUSH AP Calc BC AP Physics

Notes

Must Knows:

  • Variable: abstraction inside a program that holds a value, where each variable has associated data storage that represents a single value at a time
    • typically have meaningful names that helps with the overall organization of the code and understanding of what is being represented by the variables
  • there are a variety of methods to represent data, called data types, which are referenced using variables
    • Booleans
    • integers
    • lists
    • strings
  • Collegeboard uses <– as the assignment operator (useful for MC)
    • this is = in Python
s = "string"
integer = 5
boolean = True
list = ["banana", "strawberry", "apples"]
#index list

print("The command List[1] prints: " + list[1])
print("The command List[-1] prints: " + list[-1])
The command List[1] prints: strawberry
The command List[-1] prints: apples