# Python For GCSE For Loops

## Introduction

For loops in most languages are counting loops. They are used to repeat blocks of code using a variable to keep count of each repetition.

The for statement in Python works a little differently in general and tend to be more like the foreach statement in other languages. We can, however, get the same behaviour from those loops. The first part of this page, and the exercises in this section will focus on how to use the for statement for counting loops.

The rest of the page will describe how for loops are used with other structures which are not themselves explained until later in the section. This is so that this page remains useful as a reference.

## Counting Loops

To make a counting loop, we can use a range statement. The range represents the numbers we want to use when counting.

### N Repetitions

If we just want to repeat, we can use the range statement as follows,
```for i in range(10):
print(i)
```

In the above example, there are 10 repetitions. The variable i is our counter or stepper variable. On each repetition it has a different value. This program counts from 0 to 9, printing the value of the counter on each repetition.

When the variable has no other special meaning, it is acceptable use the letter i for iterator.

The code that is being repeated is indented. When you stop indenting, the code you write will not be repeated.

When you write the range statement with a single argument, the loop will count from 0 and will stop before it reaches the number. The most common mistake people make with their range statements is to forget this.

### From A to B

If we use two arguments in the range statement, the loop will count from the first value and stop before it reaches the second one. The following program counts from 3 to 9, printing the value of i on each repetition.

```for i in range(3, 10):
print(i)
```

### Changing The Step

If we use three arguments in the range statement, we can count from the first value, stop before reaching the second and add the third value to the counter each time. The following program counts from 10 to 90, going up in tens.

```for i in range(10, 100, 10):
print(i)
```

### Counting Backwards

If we make the first argument larger than the second and use a negative number for the step, we can count backwards. The same principle applies. The loop will stop repeating before it reaches the second value. It will add the third value to the counter each time. Since our step value is negative, the loop will count down instead of up.

```for i in range(10, -1, -1):
print(i)
```

### Break Statement

It is possible to stop a loop before it reaches the end. You use the break statement to do this.

```total = 0
count = 0
print("Enter up to 10 numbers.")
for i in range(10):
n = int(input("Enter a number or -1 to stop: "))
if n==-1:
break
else:
total = total + n
count = count + 1
print("There were", count, "numbers.")
print("The average value was", total/count)
```

In most circumstances, this technique can be avoided if you use a while loop instead of a for loop.

### Continue Statement

The continue statement is used to skip an iteration. The loop does not stop, it just skips to the next iteration.

```print("Choose numbers to add from 1 to 10.")
total = 0
for i in range(1, 11):
c = input("Include " + str(i)+ " [y/n]: ")
if c != "y":
continue
total = total + i
print(total)
```

### Nested Loops

Nested for loops are for loops inside for loops. There are occasions where you would want to do this. For example, if you are using loops to process co-ordinates. When you nest programming structures, you need to take care to keep up the indentation correctly.

```for y in range(3):
for x in range(3):
print("(", x, ",", y, ")", sep="")
print()
print("Done")
```

## Iterating Strings

A for loop can be used to process all of the characters in a string.

```for c in "elephant":
print(c)
```

In this example, the variable c is not keeping count. It is taking on the value of each of the characters in the string, one at a time.

## Iterating Lists

We can iterate through a list, the same way we did with the string.

```pets = ["cat", "dog", "hamster", "budgie"]
for p in pets:
print(p)
```

## Using The Enumerate Function

If you use the enumerate functon, you can iterate through a list whilst keep count.

```pets = ["cat", "dog", "hamster", "budgie"]
for i,p in enumerate(pets):
print(i,p)
```