There are two major types of loops in Python, for loops and while loops. If you want to repeat a certain number of times (definite count), you will normally use a for loop.
For example, a for loop can be used to print all student records since the computer knows exactly how many students there are.
For Loop Syntax
for variable in range(number):
indented code goes here...
Using the range function
For loops will iterate through a sequence of numbers using the "range" function. The range function allows you to specify up to three values
range(start value, stop value, step value)
- The 'start value' is the value the for loop will start at (default value is 0)
- The 'stop value' is the value the for loop will stop at ( it will never include the stop value so if 5 is used it will stop when it gets to 5 )
- The 'stop value' is the only value that must always be delcared
- The 'step value' is the size of the increments for each loop, for example if the step value was 2 then the loop variable(iterator) would increase in value each time by 2 - (default value is 1)
Below is an example of a for loop using the range function with just the stop value set to 5 so it will repeat the code inside the loop 5 times