3.1 & 3.2

Variables

  • Variables organize data by labeling it with a descriptive name
    • contains a name, value, and type
      • NAMING:
        • use general terms
        • keep it concise
        • use consistent notation (caps, etc...)
        • don't make it too vague
        • DON'T USE SPACES
      • TYPES:
        • Integer - a number
        • Text/string - a word or string of characters
        • Boolean - data that determines if something is true or false
      • A list of data can be stored as a variable
        • more organized
        • can be indexed

Assignments

  • Operators that allows a program to change the value represented by a variable
  • Used to assign values to variables
  • =
    • Assigns value of the right side to the left side
    • a=b
      • Outcome: b
  • +=
    • Add right side operand with left side operand and then assign to left operand
    • a+=b
      • Outcome: a+b
  • -=
    • Subtract right operand from left operand and then assign to left operand: True if both operands are equal
    • a-=b
      • Outcome: a-b
  • same thing for *= and /=

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 and 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
  • Example of a list with colors
Index Element
1 Green
2 Blue
3 Purple
4 Pink
  • At index 1, the element is green, at index 2, the element is blue, etc...

Managing Lists

  • 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)
  • Managing complexity

    • Helps improve readability
    • Reduces the need for new variables as more data is collected
    • Can easily update data
    • Can easily convert data to different forms

Practice

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

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

Homework

You will turn in a program that utilizes lists and variables as it's primary function, options could be a quiz, a sorter, database, or wherever your imagination brings you. You will be graded on how well you understood the concepts and if you used anything other than just the simplest parts.

QandA = {
    "#1) What is the study of the human body called?": "anatomy", 
    "#2) What is the main focus of Unit 3 in Comp Sci": "algorithms", 
    "#3) What is the most popular programming language": "javascript", 
    "#4) A microphone, keyboard, and mouse are all different types of what?": "input devices", 
    "#5) What is the HyperText Markup Language commonly referred to as?": "html", 
}

def qandresp(question): # display question, return inputted response
    print(question)
    resp = input()
    return resp

correct = 0 

# Setup
print("Current number of questions: " + str(len(QandA)))

# iterate over each key
for key in QandA:
    rsp = qandresp(key) # save user's response to a variable rsp
    rsp = rsp.lower() # answer is case sensitive, so match response to lowercase for answer key to work
    if rsp == QandA[key]: # check if the response is equal to the answer in the dictionary
        print(f"Correct! Your answer was {rsp}")
        correct += 1
    else:
        print(f"{rsp} is incorrect") 

percent = str(round(correct/len(QandA), 2)*100) # calculate percentage

print("You scored " + str(correct) +"/" + str(len(QandA)))
print(f"This is {percent}%") # print score and percentage
Current number of questions: 5
#1) What is the study of the human body called?
Correct! Your answer was anatomy
#2) What is the main focus of Unit 3 in Comp Sci
Correct! Your answer was algorithms
#3) What is the most popular programming language
Correct! Your answer was javascript
#4) A microphone, keyboard, and mouse are all different types of what?
Correct! Your answer was input devices
#5) What is the HyperText Markup Language commonly referred to as?
not sure is incorrect
You scored 4/5
This is 80.0%