Variables

  • Variable: An abstraction can hold a value and organizes data by assigning a name
    • Contains three parts
      • Naming
        • Make the name as general as possible
          • Not too specific
          • Capitalized letters
          • Don’t make it too vague
          • Don’t put spaces
      • Types of data
        • Integer: number
        • Text: word
        • Boolean: data - true or false
      • Store a list of data
        • Don’t create a lot of values
        • Make changes easily
  • Assignments: allows a program to change the value represented by a variable
    • See table for individual operators
    • Changing values

Examples

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'>
colors = ["red", "orange", "yellow"]
print(colors[2])
yellow
a = 1
b = 2
a = b
print(a)
2
a = 1
b = a
a = 2
print(b)
1
currentScore = 10
highScore = currentScore
currentScore = 7
print(highScore)
10

Problems to Solve

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

Which of these will show the sum?

num2 += num1
print(num1)
print(num2)

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

Data Abstraction

  • Take away aspects of data that aren’t used
    • Variables and lists are used in data abstraction

Lists & Strings

  • List - ordered sequence of elements
    • Element - individual value in a list that is assigned to a unique index
    • Index - references the elements in a list or string
    • String - ordered sequence of characters
  • Lists
    • Allow for data abstraction
    • Bundle variables together
    • Can keep adding elements
  • List operations
    • Assigning values to a list of indices
    • Create an empty list and assign it to a variable
    • Assign a copy of one list to another
  • Lists help manage complexity
    • Improves code readability
    • Don't need many variables
    • Can apply mathematical computation

List Examples

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
# 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
['black', 'blue', 'orange', 'yellow', 'purple', 'green', 'pink']

Data Abstraction Practice

Manage the complexity of the given code below using a list. Re-write the code segment in a less complex way, but with the same result.

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

Answer

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

Quiz template, if you do use it, fix the issues, and add more to it than it's current barebones state. I would recommend using it to create something related to school.

import getpass, sys
#used three variables (q1, q2, q3) for the questions. 
q1 = "Who is the author of A Good Girl's Guide To Murder?"
q2 = "What is the rating of The Silent Patient?"
q3 = "In what year were A Good Girl's Guide to Murder and The Silent Patient published?"

#defining a dictionary using the above questions and answers. 
quiz = { q1: "Holly Jackson", 
         q2: "4.2",
         q3: "2019"
        }
    

def question_and_answer(prompt):
    print("Question: " + prompt)
    msg = input()
    print("Answer: " + msg)
    
def question_with_response(prompt):
    print("Question: " + prompt)
    msg = input()
    return msg

#evaluating the answer using the get() function on the dictionary. "Python dictionary method get() returns a value for the given key. If key is not available then returns default value None."
def evaluate_answer(question, response, correct):
    if response == quiz.get(question):
      print(response + " is correct!")
      correct += 1
    else:
      print(response + " is incorrect!")
    return correct
    


questions = 3
correct = 0

print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_and_answer("Are you ready to take a test?")

rsp = question_with_response(q1)
correct = evaluate_answer(q1, rsp, correct)
print(correct)

rsp = question_with_response(q2)
correct = evaluate_answer(q2, rsp, correct)
print(correct)


rsp = question_with_response(q3)
correct = evaluate_answer(q3, rsp, correct)
print(correct)

print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
Hello, SreejaGangapuram running /usr/local/bin/python3
You will be asked 3 questions.
Question: Are you ready to take a test?
Answer: yes
Question: Who is the author of A Good Girl's Guide To Murder?
Holly Jackson is correct!
1
Question: What is the rating of The Silent Patient?
4.2 is correct!
2
Question: In what year were A Good Girl's Guide to Murder and The Silent Patient published?
2019 is correct!
3
SreejaGangapuram you scored 3/3