Loops Flashcards
What is a loop? how do you construct a for loop?
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 does the range() function work?
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 does the range() function work in a for loop?
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.
What is a While-loop?
What is the syntax?
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 does the ‘break’ and ‘continue’ keywords affect the processing of a loop?
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.
for-else….?
The else statement can be used with a loop as folows: