Variables, Assignments, and Data Abstractions Notes and Hacks
- Variables
- Types of Data
- What can we do with Variables?
- Assignments
- Data Abstraction
- Why do we Use Data Abstraction? Managing the Complexity of a Program
- Lists: Amazing for Data Abstractions
- AP Exam Use of Data Abstraction
- Homework Assignment:
- Quiz with Loops and Lists
name = "table1" #string
print(name, type(name))
number = 4 #integer
print(number, type(number))
isAbsent = False
print(isAbsent, type(isAbsent))
colors = ["red", "orange", "yellow"]
print(colors[0])
a = 1
b = 2
a = b
print(a)
a = 1
b = a
a = 2
print(b)
currentScore = 10
highScore = currentScore
currentScore = 7
print(highScore)
num1 = 5
num2 = 9
num1 = num2
print(num1)
print(num2)
num1 = 15
num2 = 25
num3 = 42
num2 = num3
num3 = num1
num1 = num2
print(num1)
print(num2)
print(num3)
num2 += num1
print(num1)
print(num2)
print(str(num1)+ str(num2))
print(num1 + num2)
Data Abstraction
- Method used in coding to represent data in a useful form, by taking away aspects of data that aren't being used in the situation
- Variables and lists are primary tools in data abstraction
- Provides a separation between the abstract properties of a data type and the concrete details of its representation
Why do we Use Data Abstraction? Managing the Complexity of a Program
- Data abstractions help manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation
- Developing a data abstraction to use in a program can result in a program that is easier to develop and maintain
Lists: Amazing for Data Abstractions
What are Lists?
- Allow for data abstraction
- Bundle variables together
- Store multiple elements
- Allows multiple related items to be treated as a single value
- Give one name to a set of memory cells
- Can keep adding elements to it as needed
- Can store elements as a single variable by using a list
3 Types of List Operations:
- Assigning values to a list at certain indices
- Creating an empty list and assigning it to a variable
- Assigning a copy of one list to another list (setting one list equal to another list)
colorsList=["pink", "yellow", "green", "blue", "orange"]
print(colorsList)
colorsList=[] # can be used if you want to create a list that can be filled with values later
def Reverse(lst): # defining variable: lst
new_lst = lst[::-1]
return new_lst
lst = ["pink", "green", "purple", "yellow", "orange", "blue", "black"]
print(Reverse(lst)) # reverse 1st
color1="green"
color2="red"
color3="pink"
color4="purple"
color5="blue"
color6="brown"
print(color1)
print(color2)
print(color3)
print(color4)
print(color5)
print(color6)
colorList=["green", "red", "pink", "purple", "blue", "brown"]
for lmao in colorList:
print (lmao)
# TAKE NOTES: FOR LOOPS ARE REALLY COOL
AP Exam Use of Data Abstraction
With the properties of the AP Exam pseudocode, lists work differently from what we've learned in python so far, here are the two major differences:
- The index does not start at 0 but 1
- There is only one method of interchanging data between lists, and that is completely overwriting previous list data with the other list \n",
# Use a dictionary for the team members
teamList = ["Jeffrey Lee", "Luke Angelini", "Aiden Hyunh", "Jagger Klein"]
# Use a dictionary for the scrum team roles
roleList = ["DevOps", "Frontend", "Swag Master", "Backend"]
# Use a dictionary for the foods
foodList = ["Thai Food", "Xhosa Food", "South Mexican Food", "American Food"]
listList = [teamList, roleList, foodList]
print ("Our Group Data: Names, Scrum Team Roles, and Favorite Foods")
for list in listList:
print (list)
QandA = [
"1. What is 3 + 3?" , "6" ,
"2. What is 20 * 6?" , "120" ,
"3. What is 10 % 3?" , "1"
]
points = 0
current = 0
questions = 1
print ("Welcome to my simple quiz")
while questions < 4:
question = input (QandA[current]) # sets the question variable to the "current" index from the dictionary
if question == QandA[current + 1]: # "current + 1" represents the answer
print(QandA[current]) # print the question first
print (question + " is correct! \n")
current += 2
points += 1
else:
print(QandA[current])
print(question + " is incorrect :( \n")
current += 2
questions = questions + 1
print("Well done, you scored " + str(points) +"/" + str(questions-1))