Loops

for Statements

words = ['cat', 'mouse', 'dog'] for w in words: print(w)

The range() Function: If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy.

for i in range(5): print(i)

You can combine range() and len() as follows:

words = ['cat', 'mouse', 'dog'] for i in range(len(words)): print(i, words[i])

break and continue Statements

break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the “for” or “while” statement

words = ['cat', 'mouse', 'dog', 'panda', 'bird'] for i in range(len(words)): if words[i] == 'dog': print('Dog here!') break print(words[i])
words = ['cat', 'mouse', 'dog', 'panda', 'bird'] for i in range(len(words)): if words[i] == 'dog': continue print(words[i])