Variables

  • Concise
  • Avoid numbers
  • No spaces or dashes
  • Capitalize letters ex: drawLine

Assignments

if a = b, then a += b is a = a + b, same pattern for -=,*=,/=, and **= (** is exponent notation)

In Class Problems:

num1 = 5
num2 = 9
num1 = num2

# num1 is 9, num2 is also 9
print("num1:",num1)
print("num2:",num2)
num1: 9
num2: 9
num1 = 15
num2 = 25
num3 = 42
num2 = num3
num3 = num1
num1 = num2

# num1 and num2 are 42, num3 is 15
print("num1:",num1)
print("num2:",num2)
print("num3:",num3)
num1: 42
num2: 42
num3: 15
num1 = 1
num2 = 4

# Both of these will show the sum:
print(num1 + num2)

num2 += num1
print(num2)
5
5

Index always starts at 1 for the AP Exam, must be whole numbers, goes up the number of elements in the list, and cannot be negative

Lists

  • list[: :-1] reverses the list
  • Lists don't need many variables, can change the number of variables easily, and can use the same math operations, making it good for managing complexity of a program
colorList = ["green", "red", "pink", "purple", "blue", "brown"]
# List starts at 0, but remember for the AP Exam it starts at 1
print(str(colorList))
['green', 'red', 'pink', 'purple', 'blue', 'brown']

AP Exam Differences

  • Index starts at 1 not 0
  • Only one method of interchanging data between lists, which is completely overwriting previous list data with the other list\n

Homework

I will be doing a quiz for the homework assignment.

questionNum = 0
score = 0

qList = ["What is 200/40?", "What is 45*2?", "What is 2^6?", "What is 1011 in binary equal to?", "What is 101110 in binary equal to?"]

qSoln = ["5", "90", "64", "22", "92"]

qAmount = len(qList)

while questionNum < qAmount:
    print("Question #", str(questionNum + 1), ":", str(qList[questionNum]))
    answer = input()
    if answer == qSoln[questionNum]:
        score += 1
        print(answer, "is correct!")
    else:
        print(answer, "is incorrect!")
    questionNum += 1

if questionNum == qAmount:
    percent = (score/qAmount)*100
    print("You Scored:", score, "/", qAmount, "(", str(percent), "%)")
    print("Here are the answers:")
    i = 0
    while i < len(qSoln):
        answerText = qSoln[i]
        print("Question #", str(i+1), ":", answerText)
        i += 1
Question # 1 : What is 200/40?
5 is correct!
Question # 2 : What is 45*2?
0 is incorrect!
Question # 3 : What is 2^6?
12 is incorrect!
Question # 4 : What is 1011 in binary equal to?
12 is incorrect!
Question # 5 : What is 101110 in binary equal to?
92 is correct!
You Scored: 2 / 5 ( 40.0 %)
Here are the answers:
Question # 1 : 5
Question # 2 : 90
Question # 3 : 64
Question # 4 : 22
Question # 5 : 92