Loops Flashcards

1
Q

While Loop

A

This is a loop that will keep repeating itself until the while condition becomes false.

n = 1
while n < 100:
n += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For Loop

A

For loops give you more control than while loops. You can loop through a range, a list, a dictionary, or a tuple.

all_fruits = [“apple”, “banana”, “orange”]
for fruit in all_fruits:
print(fruit)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

_ in a For Loop

A

If the value your for Loop is iterating through, ex: the number in the range, or the item in the list is not needed, you can replace it with an underscore.

for _ in range(100):
#Do something 100 times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

break

A

This keyword allows you to break free if the loop. You can use it in a for or while Loop.

for s in scores:
if s > 100/:
print(“Invalid”)
break
print(s)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

continue

A

This keyword allows you to skip this iteration of the loop and go to the next. The loop will still continue, but it will start from the top.

n = 0
while n < 100:
n += 1
if n % 2 == 0:
continue
print(n)
#Prints all the odd numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly