9 Replies - 19347 Views - Last Post: 17 October 2010 - 04:48 AM Rate Topic: -----

#1 kushanku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 kushanku  Icon User is offline

  • New D.I.C Head

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

Re: Sum numbers using for loop

Posted 15 October 2010 - 12:23 PM

View PostNallo, 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?
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 kushanku  Icon User is offline

  • New D.I.C Head

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

Re: Sum numbers using for loop

Posted 15 October 2010 - 01:41 PM

View PostNallo, on 15 October 2010 - 12:03 PM, said:

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

Great. I understand it now. thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


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
>>>


Was This Post Helpful? 0

#7 kushanku  Icon User is offline

  • New D.I.C Head

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

Re: Sum numbers using for loop

Posted 16 October 2010 - 10:00 AM

View Postc.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?
Was This Post Helpful? 0
  • +
  • -

#8 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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

Was This Post Helpful? 2
  • +
  • -

#9 Guest_c.user*


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 :)
Was This Post Helpful? 0

#10 kushanku  Icon User is offline

  • New D.I.C Head

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

Re: Sum numbers using for loop

Posted 17 October 2010 - 04:48 AM

View PostNallo, 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1