**for**

loopsA

statement in Python has this form:**for**

forvariable

inrange(expression`):`

body

Consider this little program as an example.

`sum` = `0`

**for** `k` **in** `range`(`4`):

`sum` = `sum` + `k`

`print`(`sum`)

Obviously the first line associates the variable

with the number 0.
Then we have a line beginning with `sum`

, followed by one or
more indented lines — in this case just one — that constitute
the loop's **for****body**.
Notice how a “4” appears inside

:
This tells the Python system to execute the body 4 times,
once with `range`(`4`)

as 0, another time with `k`

as 1,
then with `k`

as 2, and finally with `k`

as 3.
After it has executed “`k`

”
these four times, it has completed the `sum` = `sum` + `k`

statement and
then continues to the next unindented statement:
“**for**

”.`print`(`sum`)

Of course, that statement displays the value of

.
But what is this? Of course, each execution of
“`sum`

” changes the variable `sum` = `sum` + `k`

's
value. The following table illustrates`sum`

iteration statement expression new

sumbefore loop:

sum=00 0 `= 0:`

k

sum=sum+k0 + 0 0 `= 1:`

k

sum=sum+k0 + 1 1 `= 2:`

k

sum=sum+k1 + 2 3 `= 3:`

k

sum=sum+k3 + 3 6

So when we get to “

”,
that statement will display 6.`print`(`sum`)

In our earlier example with

, we saw the statement
“`sum`

”. This means something very
different from its meaning in mathematics, where you might be
tempted to subtract `sum` = `sum` + `i`

from each side and conclude that
`sum`

is 0. Instead, in Python it means to add the current values of
`i`

and `sum`

, and change `i`

to be that number.`sum`

This type of statement is very common. In fact, it is so
common that we might go far as to give a name to such a
variable: an **accumulator variable**.
As an alternative example, suppose we want to compute the
sum of 10 positive numbers entered by the user.

`total` = `0`

**for** `i` **in** `range`(`10`):

`user_num` = `int`(`input`())

`total` = `total` + `user_num`

In this case as well, we have an initial value for

,
but `total`

is updated in terms of itself with each
iteration.`total`

Statements of the form
“**var** `=`

**var** `+`

**expression**”
are common enough that Python provides a shortcut for them:
‘`+=`

’. We can abbreviate such a statement as
“**var** `+=`

**expression**”.
(Though used less frequently, there are similar shortcuts in
‘`-=`

’,
‘`*=`

’,
‘`/=`

’,
‘`%=`

’,
‘`//=`

’, and
‘`**=`

’.)

`range`

functionAs we're using the

loop for now, it will
**for***always* contain an invocation of the

function. This function can take a variety of argument
configurations that can potentially be useful.`range`

code result `range(end)`

Counts from 0 up to but excluding `end`

`range(start, end)`

Counts from `start`

up to but excluding`end`

`range(start, end, step)`

Counts from `start`

by steps of`step`

, stopping on reaching`end`

As an example, the following little program counts down from 10 to 1.

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

`print`(`i`)

This says to execute the body with

as 10 first,
but then to add −1 so that it then goes to 9,
then 8, then 7, and so forth until it reaches 1.
The “ending” number is always excluded, so
it does not execute the body with `i`

as 0.`i`