Tag: while
python for loop
Code can be repeated using a loop. Lines of code can be repeated N times, where N is manually configurable. In practice, it means code will be repeated until a condition is met. This condition is usually (x >=N) but it’s not the only possible condition.
Python has 3 types of loops: for loops, while loops and nested loops.
Related Course:
Python Programming Bootcamp: Go from zero to hero
For loop
We can iterate a list using a for loop#!/usr/bin/python |
Visualization of for loop:
The for loop can be used to repeat N times too:
#!/usr/bin/python |
While loop
If you are unsure how many times a code should be repeated, use a while loop.
For example,
|
Nested loops
We can combine for loops using nesting. If we want to iterate over an (x,y) field we could use:
#!/usr/bin/python |
Nesting is very useful, but it increases complexity the deeper you nest.
If you are new to Python programming, I highly recommend this book.