Hello Ethan,
QUOTE(EthanR @ 10 Sep, 2009 - 02:08 PM)

q = sum(range(1,h+1,1)**2)
This doesnt work, as range is a list (in Python 2.x, in Python 3.x it is a range object) and Python doesnt know what list ** 2 shall mean.
What you want is to put the elements of the list to power 2, so to write it in one line using
list comprehension:
CODE
q = sum([i ** 2 for i in range(1, h + 1)])
or more elegantly using
generator expression (Python 2.4(? not sure if it was 2.4) or later:
CODE
#in Python 2.x
q = sum(i ** 2 for i in xrange(1, h+1))
#in Python 3.x
q = sum(i ** 2 for i in range(1, h+1))
This is better than the above version, as the list is not stored in memory (for long lists that matters) before sum is called. Its elements are generated lazily, meaning only once they are needed.
This post has been edited by Nallo: 11 Sep, 2009 - 09:55 AM