Python Loop

 


For Loop

This loop can be used to iterate over any iterable object in python e.g. list, tuple, string, etc.






Using range function

If you do need to iterate over a sequence of numbers, the built-in function comes in handy. It generates arithmetic progressions:



To iterate over the indices of a sequence



Below are some examples for using the range function

>>> list(range(5, 10))
[5, 6, 7, 8, 9]

>>> list(range(0, 10, 3))
[0, 3, 6, 9]

>>> list(range(-10, -100, -30))
[-10, -40, -70]

















Comments

Popular posts from this blog

Get first non-repeating Letter

Cypress run command

Docker commands