# Sum numbers using for loop

Page 1 of 1

## 9 Replies - 26323 Views - Last Post: 17 October 2010 - 04:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195093&amp;s=cceeb3c2d4b204a5586d7a92a06cc044&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kushanku

Reputation: 0
• Posts: 15
• Joined: 09-October 10

# Sum numbers using for loop

Posted 15 October 2010 - 02:32 AM

I came across the following line of code which sums a list of numbers by converting the list into a string.
```sumDigits = 0
for c in str(1952):
sumDigits += int(c)
print sumDigits

```

I understand that line 2 converts the integers 1952 into a string but I do not really understand how the loop sums the digits and returns the correct answer?

any help on this would be great

Thanks.

Is This A Good Question/Topic? 0

## Replies To: Sum numbers using for loop

### #2 Nallo

• D.I.C Regular

Reputation: 163
• Posts: 256
• Joined: 19-July 09

## Re: Sum numbers using for loop

Posted 15 October 2010 - 03:26 AM

A string in python is a sequence (of characters). You can loop over sequences.

```>>>for c in "foo":
>>>    print c
f
o
o
>>>for c in "1234":
>>>    c_i = int(c)
>>>    print c_i + 1
2
3
4
5

```

a += b is short for a = a + b

This post has been edited by Nallo: 15 October 2010 - 03:27 AM

### #3 kushanku

Reputation: 0
• Posts: 15
• Joined: 09-October 10

## Re: Sum numbers using for loop

Posted 15 October 2010 - 12:23 PM

Nallo, on 15 October 2010 - 02:26 AM, said:

A string in python is a sequence (of characters). You can loop over sequences.

```>>>for c in "foo":
>>>    print c
f
o
o
>>>for c in "1234":
>>>    c_i = int(c)
>>>    print c_i + 1
2
3
4
5

```

a += b is short for a = a + b

Thanks. Is there any reason why 'c' is used?

### #4 Nallo

• D.I.C Regular

Reputation: 163
• Posts: 256
• Joined: 19-July 09

## Re: Sum numbers using for loop

Posted 15 October 2010 - 01:03 PM

no reason, any variable name instead of c would do.

### #5 kushanku

Reputation: 0
• Posts: 15
• Joined: 09-October 10

## Re: Sum numbers using for loop

Posted 15 October 2010 - 01:41 PM

Nallo, on 15 October 2010 - 12:03 PM, said:

no reason, any variable name instead of c would do.

Great. I understand it now. thanks!

Reputation:

## Re: Sum numbers using for loop

Posted 15 October 2010 - 07:21 PM

there is the sum function
```>>> sum(int(i) for i in str(1952))
17
>>>

```

### #7 kushanku

Reputation: 0
• Posts: 15
• Joined: 09-October 10

## Re: Sum numbers using for loop

Posted 16 October 2010 - 10:00 AM

c.user, on 15 October 2010 - 06:21 PM, said:

there is the sum function
```>>> sum(int(i) for i in str(1952))
17
>>>

```

So in essence, this line of code converts the string '1952' into an integer and then sums each individual number?

### #8 Nallo

• D.I.C Regular

Reputation: 163
• Posts: 256
• Joined: 19-July 09

## Re: Sum numbers using for loop

Posted 16 October 2010 - 01:49 PM

c.user did something I wouldnt want to expose a python beginner to

For his code you first need to understand lists and list comprehesions, like:
```>>> [i + 1 for i in [1,2,3]]
[2,3,4]
>>> [s + "a" for s in "def"]
['da', 'ea', 'fa']
>>> [int(i) for i in str(1952)]
[1, 9, 5, 2]

```

get the picture? still there?

sum() takes a list of numbers and sums its contents

If I havent lost you yet, the following might make sense:
```>>> mylist = [int(i) for i in str(1952)]
>>> sum(mylist)
17
>>> sum([int(i) for i in str(1952)])
17

```

still not lost you?

trouble is: "why did c.user omit those [] brackest denoting a list" ... sorry explainig generator expressions to beginners is above my explainig capabilites

truth is:
sum takes an iterable (not just a list) and sums the contents. An generator expression is an iterable By now I surely lost you, right?

This post has been edited by Nallo: 16 October 2010 - 01:57 PM

Reputation:

## Re: Sum numbers using for loop

Posted 16 October 2010 - 07:43 PM

I didn't learn generator expressions yet
I just tryed and this worked

### #10 kushanku

Reputation: 0
• Posts: 15
• Joined: 09-October 10

## Re: Sum numbers using for loop

Posted 17 October 2010 - 04:48 AM

Nallo, on 16 October 2010 - 12:49 PM, said:

c.user did something I wouldnt want to expose a python beginner to

For his code you first need to understand lists and list comprehesions, like:
```>>> [i + 1 for i in [1,2,3]]
[2,3,4]
>>> [s + "a" for s in "def"]
['da', 'ea', 'fa']
>>> [int(i) for i in str(1952)]
[1, 9, 5, 2]

```

get the picture? still there?

sum() takes a list of numbers and sums its contents

If I havent lost you yet, the following might make sense:
```>>> mylist = [int(i) for i in str(1952)]
>>> sum(mylist)
17
>>> sum([int(i) for i in str(1952)])
17

```

still not lost you?

trouble is: "why did c.user omit those [] brackest denoting a list" ... sorry explainig generator expressions to beginners is above my explainig capabilites

truth is:
sum takes an iterable (not just a list) and sums the contents. An generator expression is an iterable By now I surely lost you, right?

Your right, this is difficult to understand for a beginner. Although I do not understand it fully, you have given me a clearer picture. I will hit the books and try to fill in the gaps!