Problem

Description

Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.

Test Cases

summation(2) # returns 3
summation(8) # returns 36

Solution

My Solution

def summation(num):    
    return sum([i for i in range(1, num+1)])

Other Solutions

def summation(num):
    return sum(xrange(num + 1))
def summation(num):
    return sum(range(1,num+1))

Learning Experiences

  • List comprehension here is completely unnecessary, because range itself returns an iterable (it itself is not an iterator, there is a difference) that you can still use sum on.
  • xrange is a Python2 relic and has been removed.