(Day 1) Unit 3 Sections 1-2 - Variables, Assignments, and Data Abstractions
- Variables
- Examples
- Problems to Solve
- Data Abstraction
- Lists & Strings
- List Examples
- Data Abstraction Practice
- Homework
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
- Make the name as general as possible
- 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
- Naming
- Contains three parts
-
Assignments: allows a program to change the value represented by a variable
- See table for individual operators
- Changing values
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[2])
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)
Which of these will show the sum?
num2 += num1
print(num1)
print(num2)
print(str(num1)+ str(num2))
print(num1 + num2)
-
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
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)
Answer
colorList=["green", "red", "pink", "purple", "blue", "brown"]
for i in colorList:
print (i)
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))