Opstap#

for-lussen#

Leerdoel: het lezen van (eindige) for-lussen

Wat is de output van onderstaande programma’s? Gebruik Python Tutor of dit notebook om je antwoorden te controleren.

Opdracht 1#

for i in range(5):
    print(i)
# controleer jouw antwoord

Opdracht 2#

L = [2, 4, 6, 8]

for x in L:
    print(x)
# controleer jouw antwoord

Opdracht 3#

L = [9, 7, 5, 3, 1]

for i in range(len(L)):
    print(L[i])
# controleer jouw antwoord

Opdracht 4#

L = [5, 3, 1]
result = 0

for i in range(len(L)):
    result = result + L[1]

print(result)
# controleer jouw antwoord

Opdracht 5#

L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
result = 0

for i in range(len(L)):
    if L[i] % 2 == 0:
        result += L[i]

print(result)
# controleer jouw antwoord

Opdracht 6#

s = "Hanze"
result = 0

for _ in s:
    result += 1

print(result)
# controleer jouw antwoord

Opdracht 7#

s = "Dit is een string"
result = ""

for x in s:
    if x in "eauoi":
        result += x

print(result)
# controleer jouw antwoord

Opdracht 8#

s = "Dit is een string"
result = ""

for i in range(len(s)):
    if s[i- 1] == " ":
        result += s[i]

print(result)
# controleer jouw antwoord

Opdracht 9#

L = []

for i in range(10):
    if i < 2:
        L += [1]
    else:
        L += [L[i-1] + L[i-2]]

print(L)
# controleer jouw antwoord

Opdracht 10#

L = []
temp = 1

for i in range(5):
    if(temp % 2 != 0):
        L += [2 * temp]
        temp =  temp + 5
    else:
        L += [temp]
        temp = temp / 2

print(L)
print(temp)
# controleer jouw antwoord

while-lussen#

Leerdoel: het lezen van (oneindige) while-lussen

Wat is de output van onderstaande programma’s? Gebruik Python Tutor of dit notebook om je antwoorden te controleren.

Opdracht 1#

n = 0

while n < 5:
    print(n)
    n += 1
# controleer jouw antwoord

Opdracht 2#

L = [1, 2, 3, 4, 5]

while len(L) > 0:
    print(L)
    L = L[1:]
# controleer jouw antwoord

Opdracht 3#

i = 1

while i < 6:
    print(i)
    
    if i == 3:
        break
        
    i += 1
# controleer jouw antwoord

Opdracht 4#

i = 0

while i < 6:
    i += 1
    
    if i == 3:
        continue

    print(i)
# controleer jouw antwoord

Opdracht 5#

number = 4
faculty = 1

while number  > 0:
    faculty = faculty * number
    number -= 1

print(faculty)
# controleer jouw antwoord

Opdracht 6#

num1 = 5
num2 = 8

while num2 > 0:
    num1 = num1 + num1
    num2 -= 1

print(num1)

# controleer jouw antwoord

Opdracht 7#

s = "Dit is een test"

while s[0] != "e":
    s = s[1:]

print(s)
# controleer jouw antwoord

Opdracht 8#

total = 0
number = int(input("Enter a number: "))

while number != 0:
    total += number
    number = int(input("Enter a number: "))

print("total =", total)
# controleer jouw antwoord

Opdracht 9#

L = [1, 2, 3, 4, 5]
index = 0

while L[index] != 3 :
    index += 1

print(index)
# controleer jouw antwoord

Opdracht 10#

L = []
temp = 1

while len(L) < 10:
    if(temp % 2 != 0):
        L += [2 * temp]
        temp =  temp + 5
    else:
        temp = temp / 2

print(lijst)
# controleer jouw antwoord