Variables, Assignments, and Data Abstractions
Hacks and notes on 3.1 and 3.2
3.1.1
Blog:
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)
-
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
-
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 = 22
x = 15
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 fragment of computer code (can be any piece of computer readable text)
-
What is an easy way to reference the elements in a list or string?
WorldCup = ["argentina", "portugal", "france", "spain"]
print(WorldCup)
- What is an example of a string?
print("Hello Universe")
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)]
# 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", "hot dog", "sushi", "strawberry", "sandwich"]
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
country1 = "Iran"
country2 = "Switzerland"
country3 = "Japan"
country4 = "Ghana"
# Inputing values one by one
print(country1, country2, country3, country4)
countries = ["Iran", "Switzerland", "Japan", "Ghana", ]
# Using a list to put values into the countries variable.
display(countries)