1.1 - 1.2 hacks and notes
for week 2
Notes
variables and lists
these are an abstraction inside a program that 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.
use good names when naming variables to make the code easy to read
lists allow variables to be bundled together.
you can add or remove variables from one list to another with .append
assignment operator
this changes a variable to a certain value. on tests, "⟵" will be used for assignment. storing values is assigning them to variables
Key Vocab
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
Hacks 1.1-1.2
3 practice questions
q: which variable would you use for value of a stock?
A: stock1 string
B: stock string
C: is_stock boolean
D: stock1val integer
Ans: D
q which variable would you use for a class names?
1: John string
2: isJohn boolean
3: john integer
Ans: 1
q what variable name would you use to store whether tv is on or off?
A: ison
B: on
C: off
D: yes
Ans: A, also store as boolean
example practice problems
Consider the following code segment:
num1 ⟵ 6
num2 ⟵ 4
num3 ⟵ 10
IF num1 < num2
num1 ⟵ num2
ELSE
num3 ⟵ num2
IF num2 ≥ num3
num1 ⟵ num2 + num3
sum ⟵ num1 + num2 + num3
What is the value of sum after the code segment is executed?
18
14
16
12
ANS: 16
Consider the following code segment
num1 ⟵ 5
num2 ⟵ 10
num3 ⟵ 20
num3 ⟵ num1
num3 ⟵ num2
num1 ⟵ num3
sum ⟵ num1 + num2 + num3
what is sum?
35
30
45
25
ANS: 45
hacks
Consider the following code segment:
num1 ⟵ 4
num2 ⟵ 6
num1 ⟵ num 2
DISPLAY(num1)
DISPLAY(num2)
What is displayed after running this code segment?
4 6
6 4
4 4
6 6
ANS: 6 6
I got this one wrong because I initially thought that num1 would retain its value. I realize now that num1 was changed to num2, and that I just kind of read over that statement.
What is the difference between an integer and string of numbers?
An integer is just a set data type while a string of numbers can be changed with addition and subtraction
An integer can be letters and numbers while a string is just numbers
An integer is just numbers while a string is just words
An integer can be changed with addition and subtraction and a string is a set number or string of letters.
ANS: 4th statement I got this problem wrong because my knowledge of that vocab was weak. I initially thought it was 3rd statement, but now I know the specific definition of an integer and Strings.
INT are numbers that can undergo math statements
Strings are words that can't have their problems changed by math (addition subtraction)
Extra Binary Hacks if you changed your bits to 24
Convert the following decimal notation to binary notation:
57345
ANS: 1110000000000001
I got this one wrong because i messed up somewhere in my math
16777215
ANS: 111111111111111111111111
I got this one wrong for same reason as the previous problem
Convert the binary notation to decimal notation:
1101001000101000
53800
I also got this one wrong because of math errors. I should double check my problems on the test to ensure that I get them right.
I did all of the problems on the blog. I found that converting to decimal was a lot harder than conversions from binary to decimal.
after doing the other problems I found that most topics are easy to understand but binary and assigning variables different values is kind of difficult. the binary is difficult because I sometimes just make math errors, and the other just requires some harder thinking to avoid making some simple error.