Opstap¶
for-lussen¶
Leerdoel: je kunt een begrensde for-lus lezen en voorspellen wat hij afdrukt.
Wat is de output van onderstaande programma’s? Gebruik Python Tutor of de cellen op deze pagina om je antwoorden te controleren.
Opdracht 1¶
for num in range(5):
print(num)
# controleer jouw antwoord
Opdracht 2¶
lst = [2, 4, 6, 8]
for item in lst:
print(item)
# controleer jouw antwoord
Opdracht 3¶
lst = [9, 7, 5, 3, 1]
for ix in range(len(lst)):
print(lst[ix])
# controleer jouw antwoord
Opdracht 4¶
lst = [5, 3, 1]
result = 0
for ix in range(len(lst)):
result = result + lst[1]
print(result)
# controleer jouw antwoord
Opdracht 5¶
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
result = 0
for ix in range(len(lst)):
if lst[ix] % 2 == 0:
result += lst[ix]
print(result)
# controleer jouw antwoord
Opdracht 6¶
string = "Hanze"
result = 0
# _ is een gangbare wegwerpvariabele: de luswaarde gebruiken we niet.
for _ in string:
result += 1
print(result)
# controleer jouw antwoord
Opdracht 7¶
string = "Dit is een string"
result = ""
# in controleert hier of een teken in de string met klinkers voorkomt.
for char in string:
if char in "eauoi":
result += char
print(result)
# controleer jouw antwoord
Opdracht 8¶
string = "Dit is een string"
result = ""
for ix in range(len(string)):
if string[ix - 1] == " ":
result += string[ix]
print(result)
# controleer jouw antwoord
Opdracht 9¶
lst = []
for ix in range(10):
if ix < 2:
lst += [1]
else:
lst += [lst[ix - 1] + lst[ix - 2]]
print(lst)
# controleer jouw antwoord
Opdracht 10¶
lst = []
temp = 1
# _ is een gangbare wegwerpvariabele: de luswaarde gebruiken we niet.
for _ in range(5):
if temp % 2 != 0:
lst += [2 * temp]
temp = temp + 5
else:
lst += [temp]
temp = temp / 2
print(lst)
print(temp)
# controleer jouw antwoord
while-lussen¶
Leerdoel: je kunt een onbegrensde while-lus lezen en voorspellen wanneer hij stopt.
Wat is de output van onderstaande programma’s? Noteer ook bij elk programma wanneer de lus stopt. Gebruik Python Tutor of de cellen op deze pagina om je antwoorden te controleren.
Opdracht 11¶
num = 0
while num < 5:
print(num)
num += 1
# controleer jouw antwoord
Opdracht 12¶
lst = [1, 2, 3, 4, 5]
while len(lst) > 0:
print(lst)
lst = lst[1:]
# controleer jouw antwoord
Opdracht 13¶
num = 1
while num < 6:
print(num)
if num == 3:
break
num += 1
# controleer jouw antwoord
Opdracht 14¶
num = 0
while num < 6:
num += 1
if num == 3:
continue
print(num)
# controleer jouw antwoord
Opdracht 15¶
number = 4
faculty = 1
while number > 0:
faculty = faculty * number
number -= 1
print(faculty)
# controleer jouw antwoord
Opdracht 16¶
num1 = 5
num2 = 8
while num2 > 0:
num1 = num1 + num1
num2 -= 1
print(num1)
# controleer jouw antwoord
Opdracht 17¶
string = "Dit is een test"
while string[0] != "e":
string = string[1:]
print(string)
# controleer jouw antwoord
Opdracht 18¶
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 19¶
lst = [1, 2, 3, 4, 5]
index = 0
while lst[index] != 3:
index += 1
print(index)
# controleer jouw antwoord
Opdracht 20¶
lst = []
temp = 1
while len(lst) < 10:
if temp % 2 != 0:
lst += [2 * temp]
temp = temp + 5
else:
temp = temp / 2
print(lst)
# controleer jouw antwoord