Loops in Python – Summary
Keywords: for loop, counting loop, loops
Why use loops in Python?
When programming, you often want to perform the same operation several times. In order to avoid programming the same thing several times, it is used instead of so-called iteration sets also called loops.
- Loops are very useful in programming and we will use them a lot as we create different parts / functions in our programs.
- We have learned the while loop that repeats one or more operations until a condition is met.
- We have also learned the for loop that is performing an operation a specific number of times.
Summary: While loop in Python
A while loop in Java is a so-called condition loop. This means repeating a code sequence, over and over again, until a condition is met. Instead of having to rewrite your code several times, we can instead repeat a code block several times. If the number of iterations not is fixed, it’s recommended to use a while loop.
Syntax: While loop in Python
while condition:
code executed within the loop
Flowchart: While loop in Python
Summary: For loop in Python
The for loop in Python is a so-called counting loop that repeats a code sequence a predetermined number of times. Hence, the for loop is best suited when you know the number of iterations that the loop will need to do
Syntax: For loop in Python
for i in range(value):
code executed within the for loop
Flowchart: For loop in Python