Unit 3 Lesson 1-2 Hacks and Notes
Variables and Assignments and Data Abstraction
- Notes:
- Hacks:
- What is a variable? (3.1.1)
- Variables (3.1.2)
- Lists (3.2.1)
- Data Abstraction with Lists (3.2.2)
- Managing Complexity with lists (3.2.3)
Notes:
- variable
- holds a value
- data types
- integer
- string
- boolean
- assignment operator
- college board uses: <-
- list index
- python lets u index from front of list and back of list
- long and short way to print lists
- short way better
- able to write code in shorter length and with less clutter
- assignming values to one variable
- uses []
name = input("What is your name?")
# use of integers
age = 15
print(f"Hey my name is {name} and I am {age} years old.")
favfoods = ["acai", "pasta", "thin mints", "coffee"]
print(favfoods[0])
print(favfoods[-2])
num1 = input("Input a number. ")
num2 = input("Input a number. ")
num3 = input("Input a number. ")
add = input("How much would you like to add? ")
# Add code in the space below
num1 = int(num1)
num2 = int(num2)
num3 = int(num3)
numlist = [num1, num2, num3]
# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.
for i in numlist:
numlist[i-1] += int(add)
print(numlist)
import getpass, sys
def question_with_response(prompt):
print("Question: " + prompt)
msg = input()
return msg
questions = 4
correct = 0
print('Hello, ' + getpass.getuser() + " running " + sys.executable)
print("You will be asked " + str(questions) + " questions.")
question_with_response("Are you ready to take a test?")
rsp = question_with_response("The purpose of lists and dictionaries are to manage the ____ of a program")
if rsp == "complexity":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("Lists are a form of data ______")
if rsp == "abstraction":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
rsp = question_with_response("Which brackets are used to assign values to a variable to make a list?")
if rsp == "[]":
print(rsp + " is correct!")
correct += 1
else:
print(rsp + " is incorrect!")
print(getpass.getuser() + " you scored " + str(correct) +"/" + str(questions))
food1 = "pizza"
food2 = "hot dog"
food3 = "sushi"
food4 = "strawberry"
food5 = "sandwich"
print(food1, food2, food3, food4, food5)
foods = ["pizza", "hot dog", "sushi", "strawberry", "sandwich")
print(foods]
- Lists are better for a program rather than writing lines of codes because it makes things written in less lines of code so that it is easier to read and less of a mess when looking at the code. Code that some would write in 10 lines can be written in 10
temp1 = 66
temp2 = 72
temp3 = 63
temp4 = 79
temp5 = 60
temp6 = 82
print(temp1, temp2, temp3, temp4, temp5, temp6)
# short way
temperature = [66, 72, 63, 79, 60, 82]
print(temperature)