Unit 3.1.1 Hacks

  • Uses variables
  • Show your understanding of different variable data types by using at least 2 different types in your code
  • Uses meaningful names to prevent confusion
drink = "water"
volume = 20
print("There are",volume,"liters of", drink)
There are 20 liters of water

Unit 3.1.2 Hacks

  • In your own words, briefly explain by writing down what an assignment operator is
    • An assignment operator is an equal sign that assigns a value to a variable
  • In Collegeboard pseudocode, what symbol is used to assign values to variables?
    • An arrow <-
  • A variable, x, is initially given a value of 15. Later on, the value for x is changed to 22. If you print x, would the command display 15 or 22?

    • It would display 22
  • Bonus: multiple ways to define a variable

var age = 1;
const b = 3

Unit 3.2.1 Hacks

Questions

  • What is a list? A list is a sequence of elements with each element being a variable
  • What is an element An element is one of the things in a list
  • What is an easy way to reference the elements in a list or string? An easy way to reference elements in a list or string is by using index
  • What is an example of a string? A string is a series of characters, for example a person's name

Create an index of your favorite foods Tips: Index starts at 1, Strings are ordered sequences of characters

Extra work: Try to create an index that lists your favorite food and print the element at index 3. More work: Create a list of your favorite foods and create an index to access them.

marks = ["food1"]

favFoods = ["pizza","steak","ribs","hamburger","fried chicken"]
print(favFoods[3])
print(favFoods[0])
print(favFoods[4])
print(favFoods[-1])
hamburger
pizza
fried chicken
fried chicken

Unit 3.2.2 Hacks

The following code is incomplete. Its intended purpose is to increase three numbers, all of which ask for user input, by an amount specified the user. The input code is abstracted, but the actual logic isn’t connected to the abstraction.

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(num1),int(num2),int(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)
Original numbers: [9, 101, -40]
Adding: 15
New numbers: [24, 116, -25]

Unit 3.2.3 Hacks

On a single markdown file:

  • Insert a screenshot of your score on the python quiz

  • Insert a screenshot of your simplifying of the food list

  • Why are using lists better for a program, rather than writing out each line of code?

    • Using lists are better for a program because it reduces redundancy and makes the program more organized and simpler. Also the code is easier to read and faster to write.
  • Make your own list the “long and slow way” then manage the complexity of the list
    • Long and Slow Way
fruit1 = "apple"
fruit2 = "banana"
fruit3 = "orange"
fruit4 = "mango"
fruit5 = "kiwi"
fruit6 = "lemon"
fruit7 = "lime"
print(fruit1, fruit2, fruit3, fruit4, fruit5, fruit6, fruit7)
apple banana orange mango kiwi lemon lime

Better Way

fruits = ["apple", "banana", "orange", "mango", "kiwi", "lemon", "lime"]
print(fruits)
['apple', 'banana', 'orange', 'mango', 'kiwi', 'lemon', 'lime']