Variables

  • A variable is an abstraction inside a program that holds a value
  • It organizes data through labeling and titling
  • Helps with the readability of program code and knowing which values are represented
  • Three parts: name, value, type

Screenshot 2022-11-28 112635

Types of Data

  • Integer: A number
  • Text/string: A word
  • Boolean: Data that determines if something is true or false
name = "table1" #string
print(name, type(name))

number = 4 #integer
print(number, type(number))

isAbsent = False
print(isAbsent, type(isAbsent))
table1 <class 'str'>
4 <class 'int'>
False <class 'bool'>

What can we do with Variables?

A list of data can also be stored in variables. Why is that useful?

  • print/retrieve specific values in the list without creating a lot of variables
  • easily remove/add/change items into the list
colors = ["red", "orange", "yellow"]
print(colors[0])
red

Assignments

  • The assignment operator allows a program to change the value represented by a variable
  • Used to assigning values to variables

Screenshot 2022-11-28 113028

Examples:

a = 1
b = 2
a = b
print(a)
2
a = 1
b = a
a = 2
print(b)
currentScore = 10
highScore = currentScore
currentScore = 7
print(highScore)
10

Practice Problems: Variables

num1 = 5
num2 = 9
num1 = num2

print(num1)
print(num2)
9
9
num1 = 15
num2 = 25
num3 = 42
num2 = num3
num3 = num1
num1 = num2

print(num1)
print(num2)
print(num3)
42
42
15
num2 += num1
print(num1)
print(num2)

print(str(num1)+ str(num2))
print(num1 + num2)
42
84
4284
126

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)

Examples of List Operations:

colorsList=["pink", "yellow", "green", "blue", "orange"]

print(colorsList)
['pink', 'yellow', 'green', 'blue', 'orange']
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
['black', 'blue', 'orange', 'yellow', 'purple', 'green', 'pink']

Practice Problems: Lists

color1="green"
color2="red"
color3="pink"
color4="purple"
color5="blue"
color6="brown"

print(color1)
print(color2)
print(color3)
print(color4)
print(color5)
print(color6)
green
red
pink
purple
blue
brown
colorList=["green", "red", "pink", "purple", "blue", "brown"]

for lmao in colorList:
    print (lmao)

# TAKE NOTES: FOR LOOPS ARE REALLY COOL
green
red
pink
purple
blue
brown

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",

Homework Assignment:

# 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)
Our Group Data: Names, Scrum Team Roles, and Favorite Foods
['Jeffrey Lee', 'Luke Angelini', 'Aiden Hyunh', 'Jagger Klein']
['DevOps', 'Frontend', 'Swag Master', 'Backend']
['Thai Food', 'Xhosa Food', 'South Mexican Food', 'American Food']

Quiz with Loops and Lists

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))
Welcome to my simple quiz
1. What is 3 + 3?
6 is correct! 

2. What is 20 * 6?
120 is correct! 

3. What is 10 % 3?
1 is correct! 

Well done, you scored 3/3