3.3 Video 1 Hacks

Show two examples and label which one is sequence, selection, iteration

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)
[0, 2, 4, 6, 8, 10]

Answers 1

All the steps combined are sequencing

The step "for i in numbers:" is iteration because they go through all the numbers.

"if (numbers[i] % 2 == 0)" is selection because they sort each number to find the even ones.

i = 1
starString = "#" 
while i <= 10: #this is a iteration because it repeats until it reaches 10
  a = 1 
  while a <= i: #this is a selection because it selectes what a is
    print ("#", end= "")
    a += 1
  print ()
  i += 1
#
##
###
####
#####
######
#######
########
#########
##########

Answers 2

All the steps are a sequence

"While i <= 5:" is iteration because they repeat until i reaches 5

"While j <= i:" is selection because this is where they decide what j is

3.3 Video 2 Hacks

Practice Problems

  1. 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

  • a=1, b=7, c=3, d=7
  1. 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?

  • 1. the value of hot is true, the value of cold is true
    1. the value of hot is false, the value of cold is true
    1. the value of hot is true, the value of cold is false
    1. the value of hot is false, the value of cold is false
  1. 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.
  1. Sequencing
num1 = 5
num2 = 10
num3 = 15
num4 = 20
num5 = 25

num1 = num2 + num3      # num1 is now the new num2 + num3
num5 = num2 + num4      # num5 is now the new num2 + num4
  • num1 = 25 (num2=10 + num3=15, so 10+15=25)
  • num5 = 30 (num2=10 + num4=20, so 10+20=30)
a = 1
b = 2
c = 3
d = 4
e = 5

a = b + c        #a is now b + c
d = a + e        #d is now a + e
  • a = 5 (b=2 + c=3, so 2+3=5)
  • d = 6 (a=1 + e=5, so 1+5=6)

3.3 Video 3 Hacks

3.4 Video 1 Hacks

String Homework


  • Test 1

    firstName <- "Bob" lastName <- "Smith" var <- substring(firstName, 1, 1) B name <- concat(lastName, var) SmithB email <- concat(name, " @gmail.com") SmithB@gmail.com DISPLAY(email)

  • What would the result be? SmithB@gmail.com


  • Test 2

    word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 4 length2 <- len(word2)/3 3 first <- substring(word1, 2, len1) ompu second <- substring(word2, len2+3, len2) ook newWord <- concat(first, second) DISPLAY(newWord)

  • What would the result be? ompuook


Learned/Struggles

  • I knew about most of these terms and how they work
  • len means the length of the list
  • If the arrow is facing the left it replaces the word on the left with the work on the right
  • I learned more ways to add code together ( +, <--, and append)

  • I struggled with the last two problems (the email and newWord)

  • It was not bad, but it was a lot of steps and I had to be careful

  • I did good with the problems that required you to replace a variable and then find what a varible was at the end of the code (<-- and +)