# Difference between revisions of "Python:Accumulate over a File"

You may find the page on accumulating variables useful:

## How many countries in Asia?

In this example we use an accumulator to count the countries in Africa.

Notice that `acc` shows up three times:

• The accumulator `acc` is set to zero at the start.
• Every time we encounter Africa we increment `acc`.
• After the loop has completed `acc` contains the value we want - so we print it.

Change the program so that it counts the countries of Asia.

[Font] [Default] [Show] [Resize] [History] [Profile]

## How many big countries?

Count the number of countries with an area of more than 1500000.

[Font] [Default] [Show] [Resize] [History] [Profile]

## Numbering countries.

Produce a numbered list of countries:

```  1 Afghanistan
2 Albania
3 Algeria
...
262 Zimbabwe
```

Use three character spaces for the number and put a single space after it.

[Font] [Default] [Show] [Resize] [History] [Profile]

## Country number 42.

Print the name of country number 42

[Font] [Default] [Show] [Resize] [History] [Profile]

## Total area

Calculate the total area of the world.

This time we add to the accumulator each time. We can use either of the statements below:

`acc = acc + area;`
`acc += area;`

[Font] [Default] [Show] [Resize] [History] [Profile]

## Total population

Calculate the total population of the world.

The answer - at a little over 6 billion will be too large for an `int`. Use a `long` instead.

[Font] [Default] [Show] [Resize] [History] [Profile]

## Total GDP of Africa

Calculate the total GDP of the countries of Africa.

[Font] [Default] [Show] [Resize] [History] [Profile]

## Total GDP of the 'West'.

Calculate the total GDP of the western world. Include the regions "North America", "Europe" and "Oceania" as the western world.

[Font] [Default] [Show] [Resize] [History] [Profile]