1,105,644 Community Members

function to return a list of numbers from 0 to argument

Member Avatar
pwolf
Junior Poster
147 posts since Dec 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Create a function generateNumbers(num) that takes in a positive number as argument and returns a list of number from 0 to that number inclusive. Note: The function range(5) will return a list of number [0, 1, 2, 3, 4].

Examples

>>> generateNumber(1)
    [0, 1]
    >>> generateNumber(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> generateNumber(0)
    [0]

i dont know how to do this. i tried;

def num(num):
	for x in range(num):
		   lis = []
		   lis.append(x)
		   y = x + 1
		   lis.append(y)
		   return lis

but it return the following;

num(10)
[0, 1]

previous attempts returned some weird results...
anyway, can you please advise me on this, or give me hints so i can advance my knowledge? would be much appreciated.

Member Avatar
M.S.
Junior Poster
118 posts since Jul 2011
Reputation Points: 53 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 2 [?]
 
0
 
def generateNumber(num):
    result = []
    for i in range(num+1):
        result.append(i)
    return result
Member Avatar
pwolf
Junior Poster
147 posts since Dec 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
def generateNumber(num):
    result = []
    for i in range(num+1):
        result.append(i)
    return result

thanks, makes sense.

Question Answered as of 2 Years Ago by M.S.
Member Avatar
snippsat
Veteran Poster
1,041 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 382 [?]
Skill Endorsements: 10 [?]
 
0
 

With list comprehension it can look like this.
Just a little thing capitale letters should only be used in class name.
Not in function name or method name.

def gen_numbers(num):
    return [i for i in range(num+1)]
Member Avatar
pwolf
Junior Poster
147 posts since Dec 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

With list comprehension it can look like this.
Just a little thing capitale letters should only be used in class name.
Not in function name or method name.

def gen_numbers(num):
    return [i for i in range(num+1)]

nice and clean,i like it.

p.s thanks

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Actually even more so:

def gen_numbers(num):
    return list(range(num+1))
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: