# adding a number in a list

Page 1 of 1

## 4 Replies - 4326 Views - Last Post: 25 February 2011 - 07:58 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=216469&amp;s=e54f9eeb1c62ea31faae294e9e20c356&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 epis

Reputation: 0
• Posts: 5
• Joined: 16-February 11

# adding a number in a list

Posted 16 February 2011 - 01:09 AM

Hello there,
Can you give me an example of how to define a function that sum all the numbers in a list using for loop?

Thanks
Is This A Good Question/Topic? 0

## Replies To: adding a number in a list

### #2 Dogstopper

Reputation: 2972
• Posts: 11,223
• Joined: 15-July 08

## Re: adding a number in a list

Posted 16 February 2011 - 01:30 AM

In this case, Google was probably the best thing to go to. However, I did your Googling for you. Remember that Python's loop can work like a "foreach" construct in other languages. Say "for each number in the list", "add the numbers"

```li = [1,2,3]
sum = 0
for num in list:
sum += num

```

### #3 epis

Reputation: 0
• Posts: 5
• Joined: 16-February 11

## Re: adding a number in a list

Posted 16 February 2011 - 11:02 PM

epis, on 16 February 2011 - 01:09 AM, said:

Hello there,
Can you give me an example of how to define a function that sum all the numbers in a list using for loop?

Thanks

This is a great Help
Thanks

### #4 CheckersW

Reputation: 12
• Posts: 198
• Joined: 04-April 09

## Re: adding a number in a list

Posted 23 February 2011 - 07:49 PM

Wow just browsing the threads here, and that was a brilliant way of describing it, dogstopper. That's actually fundamentally answered a question I was about to ask. Thanks

### #5 atraub

• Pythoneer

Reputation: 829
• Posts: 2,250
• Joined: 23-December 08

## Re: adding a number in a list

Posted 25 February 2011 - 07:58 AM

just a little side note, sum is a key word in python, so you probably should call it something else.