7

I've decided not to waste my summer and start learning python. I figured I'd start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate the numbers 1 - 10.

This is what I have:

def generateNumber(num):
    i=0 
for i in range(num):
    return i
return i

and the code doesn't work. I want to get an output in a list like this:

>>> generateNumber(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
hypadr1v3
  • 543
  • 4
  • 26
xtheking
  • 575
  • 2
  • 8
  • 31

5 Answers5

7

Trying to be consistent with what you first tried, you could do something like this

def generateNumber(num):
    mylist = []
    for i in range(num+1):
         mylist.append(i)
    return mylist


x = generateNumber(10)

but, you could, instead just say,

x = range(10+1)  # gives a generator that will make a list

or

x = list(range(10+1))  # if you want a real list

In general though, you should keep this list based on inputting the number 10 so it is [0...9] and not [0...10].

tom10
  • 67,082
  • 10
  • 127
  • 137
3

It might help to implement this with the ability to specify a range:

def generateNumber(low, high):
    '''returns a list with integers between low and high inclusive
    example: generateNumber(2,10) --> [2,3,4,5,6,7,8,9,10]
    '''
    return range(low, high+1)

This can also be done with the built-in range function:

range(10)   --> [0,1,2,3,4,5,6,7,8,9]   #note the "off by one"
range(11)   --> [0,1,2,3,4,5,6,7,8,9,10]
range(2,11) --> [2,3,4,5,6,7,8,9,10]

More about range: http://docs.python.org/2/library/functions.html#range

  • Why `[x for x in xrange(low, high+1)]` instead of just `range(low, high+1)` or `list(xrange(low, high+1))`? – tom10 Jun 18 '13 at 21:06
  • Just updated, thanks. There was no reason to use the list comp. The range function is much faster by itself -- it's highly optimized for this type of usage. – codeprogress Jun 18 '13 at 22:10
2

By default, range(n) produces list of numbers [0, 1, ..., n-1].

If you want a list of numbers from a to b inclusive, you should call:

range(a, b + 1)

Which in your case is:

range(1, 11)
pkacprzak
  • 5,537
  • 1
  • 17
  • 37
1

The range function already does what you are setting out to do.

If you're in Python 2, range(10) returns 0 through 9, or in Python 3 it's list(range(10)).

TankorSmash
  • 12,186
  • 6
  • 68
  • 106
0

You probably want to create a list in your function, use the yield keyword, or use the built in list function.

def generateNumberList(num):
    myList = []
    for i in range(num):
        myList.append(i)
    #Notice that your return the list you've created rather 
    #than each individaul integer
    return myList

print generateNumberList(10)

def generateNumberList2(num):
    for i in range(10):
        yield i

for i in generateNumberList2(10):
    print i

def generateNumberList3(num):
    return list(range(num))

print generateNumberList3(10)
Community
  • 1
  • 1
Steven Wexler
  • 16,589
  • 8
  • 53
  • 80