Lesson 3/4 Hacks
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers:
if (numbers[i] % 2 == 0):
evens.append(numbers[i])
print(evens)
The cell as a whole is a sequence.
"for i in numbers" is an iteration.
if (numbers[i] % 2 == 0): is a selection.
3.3 Video 2 Hacks
Practice Problems
- given the following code segment below:
a ⟵ 7
b ⟵ 1
c ⟵ 3
d ⟵ 4
a ⟵ b
b ⟵ c + d
d ⟵ b
find the value for a, b, c, d
My answer: a = 1 (a changes from 7 to 1) b = 7 (b changes from 1, to 3+4) c = 3 (c does not change) d = 7 (d changes from 4 to 7, due to b changing)
- consider the following code segment:
hot ⟵ true
cold ⟵ false
cold ⟵ hot
hot ⟵ cold
what are the values of hot and cold after executing the code segment?
- the value of hot is true, the value of cold is true
My answer:
Cold changes to hot, and hot stays true
- Make TWO of your own code segments that contain at least 5 defined variables, then provide the answer and EXPLAIN why your answer is correct.
Segment 1:
red ⟵ yes
orange ⟵ no
green ⟵ almost
blue ⟵ never
orange ⟵ blue
purple ⟵ absolutely
red ⟵ orange
What is red?
never
Segment 2:
a ⟵ 8
b ⟵ 3
c ⟵ 7
d ⟵ 9
a ⟵ 2
b ⟵ a + c
c ⟵ b
Find the value for c.
c = 15
- Sequencing
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3
num2 = num1 + num3
What is the value of num1 and num2?
num1 = 6
num2 + num 3 = 6
num2 = 11
num1 + num3 = 6 + 5 = 11
3.4 Video 1 Hacks
String Homework
-
Test 1
firstName <- "Bob" lastName <- "Smith" var <- substring(firstName, 1, 1) name <- concat(lastName, var) email <- concat(name, "@gmail.com") DISPLAY(email)
-
What would the result be?
substring individualized the letter B, then name is defined as SmithB, the email then adds the address to become:
SmithB@gmail.com
-
Test 2
word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 length2 <- len(word2)/3 first <- substring(word1, 2, len1) second <- substring(word2, len2+3, len2) newWord <- concat(first, second) DISPLAY(newWord)
ompuook