numbers = [0,1,2,3,4,5,6,7,8,9,10] # Sequencing
evens = [] # Sequencing
for i in numbers: #Iteration + Sequencing
if (numbers[i] % 2 == 0): #Selection + Sequencing
evens.append(numbers[i]) #Selection + Sequencing
print(evens) # Sequencing
numbers = [0,1,2,3,4,5,6,7,8,9,10] # Sequencing
odds = [] # Sequencing
for i in numbers: #Iteration + Sequencing
if (numbers[i] % 2 != 0): #Selection + Sequencing
odds.append(numbers[i]) #Selection + Sequencing
print(evens) # Sequencing
i = 1
starString = "*"
while i <= 5:
j = 1
while j <= i:
print ("*", end= "")
j += 1
print ()
i += 1
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
a = 1, b = 7, c = 3, d = 7
- 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
- the value of hot is false, the value of cold is true
- the value of hot is true, the value of cold is false
- the value of hot is false, the value of cold is false
The answer is 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.
dogs ⟵ false
cats ⟵ true
parrots ⟵ dogs
dogs ⟵ cats
cats ⟵ parrots
- dogs: true
- cats: false
- parrots: false Dogs is true because it is set to cats, and cats at that time was true. Parrots is false because it is set to dogs, before dogs was set to cats, making it false. Cats is also false because it is set to parrots, which is false, at the very end.
numPups = 0 numCats = 40 numParrots = 10
numCats ⟵ numParrots/10 numParrots ⟵ numCats/10 numPups ⟵ numCats+numParrots numCats ⟵ numParrots/numCats numParrots ⟵ numCats+numPups
- numPups: 5
- numCats: 5
- numParrots: 10 numPups is equal to 5, because it is equal 40/10+10/10, or 5. numCats is 5 because 5/1 = 5 as well. numParrots is set to 5+5 at the very end, making it 10.
- Sequencing
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3
num2 = num1 + num3 # num2 is now the new num1 + num3
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?
Hint: var = "B" name = "SmithB" ## Test 1: "SmithB(At symbol it does not work)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)