3.1 & 3.2

link

Lesson

Variables

A variable is an abstraction inside a program that can hold a value.

It organizes data by labeling it with a descriptive name.

It consists of three parts: name, value, and type.

Using meaningful variables names helps with readability of program code and understanding of what values are represented by the variables.

Variables should not be specific.

Not too vague either.

Use 1-2 capitalized letters

No spaces

Types of data:

  • Integer: A number
  • Text/string: A word
  • Boolean: Data that determines if something is true or false

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

Lists & Strings

List = ordered sequence of elements

Element = individual value in a list that is assigned to a unique index

Index = a way to reference the elements in a list or string using natural numbers; each element of a string is referenced by an index

String = ordered sequence of characters (Letters, numbers, special characters)

Note: index starts at 1 for AP Exam, must be whole numbers, cannot be negative, and goes up to the number of elements in the list

Managing the Complexity of a Program through Data Abstraction

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

Using Lists as 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

  1. Assigning values to a list at certain indices

  2. Creating an empty list and assigning it to a variable

  3. 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

# copy of the list is made; the list isn't sorted in place
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)

# OR

colorList=["green", "red", "pink", "purple", "blue", "brown"]

for i in colorList:
    print(i)
green
red
pink
purple
blue
brown
green
red
pink
purple
blue
brown

Homework

questions = 3
correct = 0

# Use a dictionary for the questions
quesList = ["To be or not to be?", "What's your name?", "How was break?", "Is this homework?"]

# Use a dictionary for the correct solutions
soluList = ["idk", "mani", "fine", "yea"]

for i in quesList:
    print(i)
    
value1 = input ("Q1")
value2 = input ("Q2")
value3 = input ("Q3")
value4 = input ("Q4")

for n in soluList: 
    if value1 == n:
        correct += 1
        
for x in soluList: 
    if value2 == x:
        correct += 1
    
for z in soluList: 
    if value3 == z:
        correct += 1
    
for y in soluList: 
    if value4 == y:
        correct += 1

print("Final score: " + str(correct))
To be or not to be?
What's your name?
How was break?
Is this homework?
Final score: 4