Loops Flashcards

1
Q

What are “for” loops for?

A

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

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

What function would you use the make a list of names in alphabetical order? In this example use the variable= “name”

A

It would be the .sort function.

 name.sort()
 print(name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the code for finding the lowest number?

A

min_number

print(min_number)

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

How would you iterate over the word “Bambridge” to spell out the character individually? Variable name is company

A

Company= “Bambridge

for x in Company:
print(x, end - ‘ ‘)

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

How do you ask someone for their name and let them answer? Variable name “name

A

name = input(“what is your name?”)

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

What is a “while” loop used for?

A

A “while” is used to repeat code continuously until a condition is met.

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

What happens if a “while” loops conditions are not met?

A

The code will continue to run on repeat and may crash the program

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

What are the two ways of exiting a loop? Explain the different

A

“Break” - a hard exit from the loop to move on to the next stretch of code if there is any

“continue” - a softer exit from loop that may be used for a item within the loop to end that repetition early and then pass onto the next item to process through the repetition.

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

What is the term given for a loop within a loop?

A

Nested Loop

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