Lesson Notes - 3.1 - 3.2
Hacks and notes on 3.1 and 3.2
3.1.1
-
Questions:
-
What exactly IS a variable?
A variable is an abstraction inside a program that holds a value, where each variable has associated data storage that represents a single value at a time
-
What is the best data type to represent someone's dog's name?
String
-
Why is it important to give variables specific names before containing values?
So you don't get them mixed up
-
What is the best way to represent someone's phone number?
String
Bonus (Not required but important to know):
-
How can we update a variable's value
(blank) = updated version
-
What function is used to recieve a user's input?
Input
-
Notes:
Strings is a series of characters (numbers, letters, etc), one example of a string is your name or your id because strings can contain both numbers and letters.
Lists are sequences of elements with each element being a variable. An example of a list can be the names of the students in this classroom.
Assign variable that coorelates to the function so less errors are made
3 different data types that support different data
* integer (numbers)
* string (or text/letters)
* Boolean (True/False statements)
Hacks: Assignment Operater: The operator used to assign a new value to a variable, property, event or indexer element. Example: X = 5 Collegeboard uses arrow symbols to assign a value to a variable. 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? The variable would display 22.
x = 312
x = 53
display(x)
3.1.2
Questions:
-
What is a list?
Lists are sequences of elements with each element being a variable. An example of a list can be the names of the students in this classroom.
-
What is an element?
A element is a fragment of computer code (which can be any piece of computer readable text)
-
What is an easy way to reference the elements in a list or string?
WorldCup = ["poland", "mexico", "usa", "qatar"]
print(WorldCup)
- What is an example of a string?
print("Hello World")
Hacks
- Create a list with indices
- Index a part of the list that you created.
- Try to index from the end
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"]
num1=input("put a number. ")
num2=input("put a number. ")
num3=input("put a number. ")
add=input("would you like to add? ")
# Add code in the space below
numlist = [int(num1), int(num2), int(num3)]
# 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(numlist)
Food = ["pizza", "burrito", "spaghetti", "apple", "burger"]
display(Food)
-
Why are using lists better for a program, rather than writing out each line of code?
It's more efficent because rather than imputing data one by one you can do it way faster using lists.
-
Make your own list the "long and slow way" then manage the complexity of the list
food1 = "Burger"
food2 = "Apple"
food3 = "Chocolate"
food4 = "Waffles"
# Inputing values one by one
print(food1, food2, food3, food4)
foods = ["Burger", "Apple", "Chocolate", "Waffles", ]
# Using a list to put values into the foods variable.
display(foods)