3.1-3.7
Notes on presentations.
- 3.1.1 - Data Types and Variables
- 3.1.2 - Variables
- 3.2.1 - List and Strings Using Variables
- 3.2.2 - Data Abstraction with Lists
- 3.2.3 - Managing Complexity with Lists
- 3.3.1 - Algorithms Using Sequences
- 3.3.2 - Mathematical Expressions
- 3.3.3 - Evaluating Mathematical Expressions
- 3.4 - Expression Manipulates Strings
- 3.5 - Expression Manipulates Strings
- 3.6 - Conditionals
- 3.7 - Nested Conditionals
3.1.1 - Data Types and Variables
Variables - abstraction that holds value
Data Types:
- integer - numbers
- string - text/letters
- boolean - true/false statements
Hacks:
3.1.2 - Variables
Storing Values:
- Collegeboard: <–
- Python: =
Hacks:
- In your own words, briefly explain by writing down what an assignment operator is.
- It is what is used to assign a certain integer or string into a variable. This is known as an equals sign (=) in Python.
- In Collegeboard pseudocode, what symbol is used to assign values to variables?
- An arrow pointing to the left (<–) is used to assign values to variables.
- 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 value outputted would be 22, since the latest assignment of x is 22.
3.2.1 - List and Strings Using Variables
Strings - numbers, letters, characters that can be printed
print("Hello World")
# Hello World
Lists - allow for multiple strings to be put together into a form that is easily accessible
list = ["red", "orange", "yelllow"]
print(list)
# ["red", "orange", "yelllow"]
List Index - allows to access one element in a list with integers
list = ["red", "orange", "yelllow"]
print(list[2])
# ["orange"]
Hacks
- What is a list?
- A list is a set of strings that are placed under the name of a single variable, leading to an easy way to store data.
- What is an element?
- An element is one of the many strings that makes up a list. These can be called out and printed, leading to easily storing a lot of data.
- What is an easy way to reference the elements in a list or string?
- An easy way to reference the elements is to print the variable, followed by an integer, which is how the elements are indexed in the list.
- What is an example of a string?
- A string is text that is defined as a variable or an element in a list. This can be directly printed with a print function.
3.2.2 - Data Abstraction with Lists
Abstraction - allows for use input to make an output
- the inputs are put into a list and then outputted with the modified values
Hacks
3.2.3 - Managing Complexity with Lists
Complexity - lists allow for a lot of data to be stored into one line of code, allowing for cleaner code and less work with creating variables
Hacks
Why are using lists better for a program, rather than writing out each line of code?
- Lists are better for a program because they allow for the programmer to store more data into one line of code and then easily find it with indexing.
3.3.1 - Algorithms Using Sequences
Algorithm - set of instructions that accomplish a task
- sequencing - creating a series of steps to be executed by code
- selection - making a decision with code (yes or no)
- iteration - loop that is triggered if a certain condition is met and stopped if it isn’t (loops until a certain action is done)
Hacks
- Sequencing: This is where the code is set up in a number of steps, which are done in a certain order in order to achieve an end process.
- Selection: One selection is in step 4, where if
number = item
, the program will displayitem found
. Another selection is where if more numbers are in the list, then the program goes back to step 2. - Iteration: An iteration is where the program makes a selection in step 4 and then if certain conditions are met, the program starts at step 2 again, up until it ends the list.
3.3.2 - Mathematical Expressions
Sequential Code Segments - code segments that are put in a certain order and execute their tasks in that certain order
Collegeboard sample code:
Num1 - 25
Num2 -32
Num3 - 42
Average - (num1 + num2 + num3)/3
DISPLAY (Average)
3.3.3 - Evaluating Mathematical Expressions
- Addition: a+b
- Subtraction: a-b
- Multiplication: a*b
- Division: a/b
- Modulus: a MOD b (a and b can be string or number)
num1 = 10
num2 = 5
num3 = 7
result = num1 + num2 + num3
print(result)
# 22
Using MOD:
num1 = 12
num2 = 25
num3 = 14
result = num1 / 4 * num3 + 9 % 2 - num3
print(result)
#29
Hacks
3 Across - Sequence 1 Down - Iteration 2 Down - Selection
3.4 - Expression Manipulates Strings
- len (str)
- returns the number of character is str
- len(“hello”) returns 5
- concat (str1, str2)
- returns a single string consisting of str1 followed by str 2
- concat(“hello”,”world”) return ‘hello world’
- substring (str1, str2, length)
- returns a substring of consecutive character from str1, starting the character at position ‘start’ and containing ‘length’ character
- substring (“hello”,”world”, 4,5) returns ‘lo wor’
Hacks
3.5 - Expression Manipulates Strings
Booleans - algebraic notation to represent a logical decision in computing
- expresses in true/false
- used to test between two situations Relational Operators - mathematical relationships
- determines true/false
- a = b, a > b, a < b, etc. Logical Operators - not, or, and, nor, nand,…
- NOT - displays opposite of data inputted, has no effect on var, mostly true/false
- AND - evaluates two conditions and whether they are both met
- OR - only outputs true if one of the conditions are met
Hacks
- NOT - displays opposite of the input (ex.
var = false --> NOT --> var = true
) - AND - displays true if both conditions are met (ex.
A = true, B = true --> A = true AND B = true --> True
) - OR - displays true if one of the two conditions are met (ex.
A = true, B = false --> A = true OR B = true --> true
)
3.6 - Conditionals
Conditionals - selection in any language
- Selection - selecting between true/false
- Algorithm - set of instructions to complete task
- Conditional Statements - if/then statements; checks whether something is true and decides what to do
Javascript Examples:
function isEven(parameter) {
if (parameter % 2 == 0) {
console.log("The number is even.");
}
else if (parameter % 2 != 0) {
console.log("The number is odd.")
}
}
isEven(4)
This shows how when a value is equal to 2 after modulus, then the output will show it’s even. If it’s not equal to 2 then the output will be odd.
3.7 - Nested Conditionals
Nested Conditionals - conditional within a conditional
- else/if statements can nest statements as well
Nested Statements:
if (x = 1)
print("1")
elif (x = 2)
print("2")
Example with numbers:
x = 2
y = 3
if x == y:
print("x and y are equal")
else:
if x > y:
print("x is bigger than y")
elif x < y:
print("x is smaller than y")
Hacks
score1 = 90
score2 = 62
start = input("Are you ready to start?")
proc = True
if start == "yes":
if score1 >= 75:
proc = True
elif score1 < 75:
print("What is your second score?")
if score2 >= 75:
proc = True
else:
proc = False
else:
print("Restart")
if proc:
print("You Passed!!!")
else:
print("You Failed.")
You Passed!!!
num = 568
guess = 243 # guess number
start = input("Are you ready to start?")
correct = False
if start == "yes":
if guess > num:
proc = False
print("too high")
elif guess < num:
if guess == num:
proc = True
else:
proc = False
print("too low")
else:
print("Restart")
too low
x = 5
y = 34
start = input("Are you ready to start?")
if start == "yes":
if x != 5:
print("incorrect")
else:
if y != 34:
print("incorrect")
else:
print("correct")
else:
print("Restart")
correct
u = 19
v = 28
w = 85 # set as incorrect value
x = 5
y = 34
z = 57
start = input("Are you ready to start?")
if start == "yes":
if u != 19:
print("u is incorrect, can't proceed")
elif v != 28:
print("v is incorrect, can't proceed")
elif w != 95:
print("w is incorrect, can't proceed")
elif x != 5:
print("x is incorrect, can't proceed")
elif y != 34:
print("y is incorrect, can't proceed")
elif z != 57:
print("z is incorrect, can't proceed")
else:
print("Restart")
w is incorrect, can't proceed
print("type in answers as 'y' or as 'n'")
math = input("Do you like math?")
writing = input("Do you like writing?")
science = input("Do you like science?")
mathc = ["Integrated 1-3", "Calculus AB", "Calculus BC", "Statistics"]
writingc = ["Honors English", "Honors Humanities", "English Language", "English Literature"]
sciencec = ["Chemistry 1-2", "Biology 1-2", "Physics 1-2", "AP Chemistry", "AP Biology", "AP Physics"]
print("Math: ", math)
print("Writing: ", writing)
print("Science: ", science)
if math == "y":
print("You should take: ", mathc)
if writing == "y":
print("You should take: ", writingc)
if science == "y":
print("You should take: ", sciencec)
elif writing == "y":
print("You should take: ", writingc)
if science == "y":
print("You should take: ", sciencec)
elif science == "y":
print("You should take: ", sciencec)
else:
print("dropout")
type in answers as 'y' or as 'n'
Math: y
Writing: y
Science: n
You should take: ['Integrated 1-3', 'Calculus AB', 'Calculus BC', 'Statistics']
You should take: ['Honors English', 'Honors Humanities', 'English Language', 'English Literature']