Video 3.1-2 Hacks
3.1.1 Notes
Variables and Assignments
Essential Knowledge:
- A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
- Using meaningful variable names helps with readability of program code and understanding of what values are represented by the variables.
- Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
- Some values are better suited to representation using one type of datum rather than another.
What is a variable?
- An abstraction inside a program that can hold a value
An example naming variables
- You want to store the highest score you scored in a game. So, you want to call this variable highScore
- You want to store a name, so you can call the variable firstName
- It is also possible to store true or false in a variable. For example, the variable could be called isSunny, where true and false are stored in the variable.
- phoneNumber could be a variable that stores phone numbers.
- These examples are short and convenient as opposed to a long description
- However, they give insight to the basic goal of the variable.
- Spaces also cause a problem because it is not allowed
- Dashes and numbers are also less ideal.
Data Type examples
- highScore --> an integer
- probably doing addition or subtraction, save it as an integer or numbers.
- firstName --> text(or string)
- since a name is text, it should be saved as a string
- isSunny --> boolean
- only gives two options: true or false within variable
- phoneNumber --> text(string)
- no math involved just a string of numbers
Practice
Question: What would be the best variable name and data type to store a user's age in a program?
- Answer: name: age data type: integer
Q: What would be the best variable name and data type to store the number of students in your class?
- A: name: numStudents data type: integer to add and subtract
Q: What would be the best variable name and data type to store the title of a movie?
- A: name: movieTitle data type: text(or string)
Q: What would be the best variable name and data type to store if someone's pet is a dog.
- A: name: isDog data type: boolean
3.1.2 Notes
What will you learn?
You will learn how to determine the value of a variable as a result of an assignment.
Essential Knowledge
- The assignment operater allows a program to change the value represented to a variable.
- The exam reference sheet uses the "⟵" to use for assignment. For example, Text: a ⟵ expression Then the code would display a block text of "a ⟵ expression". The code will evaluate expression and then assigns a copy of the result to the variable a.
-
The value stored in a variable will be the most recent value assigned. For example,
- a ⟵ 1
- a ⟵ b
-
a ⟵ 2
display(b)
The code will display 1.
Storing Values
When storing values, you assign them to a variable.
Examples:
- highScore ⟵ 100
- The highScore is my assignment operator and it is storing the value of 100 inside itself as a variable. This will set my highScore to 100.
- firstName ⟵ "Ashley"
- The firstName is the variable and the text we are storing inside is "Ashley." We know that the name is in text because of the quotation marks around the assignment.
- isRaining ⟵ true
- The isRaining variable has the intention to be a Boolean variable which checks true or false, but it's been assigned to store the value true.
- phoneNumber ⟵ "555-0101"
- The phoneNumber variable and the number we are storing is "555-0101" The number is written within text because we are not planning to do math with this number.
Additional Vocabulary
Elements: individual value in a list that is assigned a unique index. And elements are referenced by an index.
Index: referencing elements in a list or string using natural numbers
3.2.2 Notes
Lists
Lists allow for data abstraction
- Variables like strings, numbers, characters, and more can be bundled together
- It could be empty from the start and you can add more variables as needed
- If you set one list equal to another list, or transfer data from one list to another, the data will be completely replaced.
- You can also append data from one list to another, keeping the old data and adding the new data.
Key Vocabulary
- list: a sequence of several variables grouped together
- variable: a way of storing information in a computer program, which could later be changed, referenced, and used
- data types: a set of values and operations on those values
- abstract data types: a data type whose internal representation is hidden from the client
- client: a program that uses a data type
- objects: a structure that can take on a data-type value
- Applications programming interface (API): which is a list of constructors and instance methods or operations, used to specify the behavior of an abstract data type
Big Ideas
- Lists can store any types of elements.
- In all lists operations, if a list index is less than 1 or greater than the length of the list, an error message is made and the program will terminate.
- Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.
3.1.1 Hacks
Question 1:
- The answer is 1 because it is counting a number that is changing so it is integer.
Question 2:
- The answer is 3 because it is a true or false question.
Question 3:
- The answer is 1 because it is a number that is not changing.
Question 4:
- The answer is 2 because it is too long.
Question 5:
- The answer is 1 because it is true or false.
Question 6:
- The answer is 4 because it is true statements.
Practice Questions I made:
- To check if the weather is good or not, what should it be? It is weatherGood and boolean
- To record the highest test score, what should it be? It is highTestScore and string
- To record the color of the phone, what should it be? It is phoneColor and string
3.1.2 Hacks
I got the last question wrong, and that's because I forgot which order it went and that it does not repeat at all. I now know that the correct answer is because the variables have values thanks to the previous five statements. In the sixth sentence, b is given the value of x + b, which is 40. The seventh sentence gives a the value of x + 1, or 21. The value of c + d / 2 is assigned to d in the ninth sentence. Division takes priority over addition in the order of operations. The number 50 is given to d since c is 30 and d / 2 is 20. The values of a, b, c, and d are shown in the final four assertions.
Practice Questions I made:
- Question 1
num1 <- 8
num2 <- 3
num2 <- num1 + num2
DISPLAY(num2) = 11
- Question 2
A <- 4
B <- 5
X <- A
DISPLAY(B) = 5
- Question 3
Y <- 1
Z <- 2
W <- Y
DISPLAY(W) = 1
- Question 4
T <- 45
D <- 12
X <- T + D
DISPLAY(X) = 57
- Question 5
testA <- 40
testB <- 50
testC <- 60
testAll <- testA + testB + testC
DISPLAY(testAll) = 150
- Question 6
labD <- 1
labE <- 2
labD <- labE
DISPLAY(B) = 1