Loops Flashcards
What are “for” loops for?
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
What function would you use the make a list of names in alphabetical order? In this example use the variable= “name”
It would be the .sort function.
name.sort() print(name)
What is the code for finding the lowest number?
min_number
print(min_number)
How would you iterate over the word “Bambridge” to spell out the character individually? Variable name is company
Company= “Bambridge
for x in Company:
print(x, end - ‘ ‘)
How do you ask someone for their name and let them answer? Variable name “name
name = input(“what is your name?”)
What is a “while” loop used for?
A “while” is used to repeat code continuously until a condition is met.
What happens if a “while” loops conditions are not met?
The code will continue to run on repeat and may crash the program
What are the two ways of exiting a loop? Explain the different
“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.
What is the term given for a loop within a loop?
Nested Loop