For Loops Flashcards
1
Q
What is a for loop and how is it different from a while loop? When should you use one over the other
A
A for loop is another method of iterating. Instead of doing something while something is true like while loops
while expression:
do something
for loops are like this instead:
for item in iterable:
do something
where iterable is an iterable object. You should use a while loop if you don’t know how many times you want the loop to run. You should use a for loop is you know how many times u want the loop to be ran. However all while loops can be written as for loops vise versa.
name = "Sodaba" for i in name: print(i) S o d a b a you can avoid the long ass output by putting end=" " after printing i
print(i, end=” “)
S o d a b a