Unit 3.1-3.2
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 = ", ")
foods = ["pizza", "watermelon", "sushi", "fried chicken"]
#index each element in the list
for i in range(len(foods)):
print(foods[i])
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)
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))
foods = ["pizza", "hot dog", "sushi", "strawberry", "sandwich"]
print(*foods, sep=" ")
- 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=" ")
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])