Unit 3.1/3.2 Hacks
name = "alex"
age = 16
traits = {
"stupid": True
}
print(name, age, traits)
3.1.2
Assignment operator: assigns a value to a variable
Collegeboard pseudocode assigment operator: <-
X value: if the value of the x
variable is changed, the print
builtin would display the new value of x
Multiple ways to define a variable in JS:
// works but dont do this, pollutes global scope
a = 1
var a = 1
// reccomended
let a = 1
// prevents reassignment
const a = 1
List: a collection of data values stored sequentially
Element: a single data value within a list
Reference elements in a list/string: using an index like so--data[0]
Example of a string: "hello world!"
foods = ["ramen", "pasta", "sushi", "donuts"]
# index 3
print(foods[3])
# position 3
print(foods[2])
# negative index (last position)
print(foods[-1])
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
numlist = [int(n) for n in [num1, num2, num3]]
print("Original numbers:",numlist)
print("Adding:",add)
# The following is the code that adds the inputted addend to the other numbers. It is hidden from the user.
for i in range(len(numlist)):
numlist[i-1] += int(add)
print("New numbers:",numlist)
3.2.3
Python quiz: Simplify list:
Why are lists better for code? They are more concise and allow you to change entries in a specific location rather than changing individual lines by hand. Long way:
one = 1
two = 2
three = 3
four = 4
print(one + two)
print(two + three)
print(three + four)
Short way:
nums = [1, 2, 3, 4]
for i in range(len(nums)-1):
print(nums[i]+nums[i+1])