Loops Flashcards

1
Q

What is a loop? how do you construct a for loop?

A

A loop allows us to perform an action multiple times without explicitly writing it each time.

Note: In the sample code 0-9 will be printed.

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

How does the range() function work?

A

The range function takes a single input and returns a range object. The range object can be converted into a list by using the list() function.

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

How does the range() function work in a for loop?

A

The range function when used with a for loop generally uses two parameters a start value and end value which is exclusive. An optional third argument can be used the ‘step’ which determines how much to add to ‘i’ in each iteration of the loop. using a negative value for the step input will cause the iteration to go backwards.

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

What is a While-loop?
What is the syntax?

A

A while loop performs a given set of instructions as long as a given condition is true.

while True:
  do something

Note: The condition must eventully evaluate to false to exit the loop.

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

How does the ‘break’ and ‘continue’ keywords affect the processing of a loop?

A

When the python interpreter hits the ‘break’ statement it will exit the loop.
In the case of the ‘continue’ keyword the interpreter will skip to the next iteration cycle.

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

for-else….?

A

The else statement can be used with a loop as folows:

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