# Function to return a list of natural numbers

Page 1 of 1

## 3 Replies - 5380 Views - Last Post: 27 May 2013 - 01:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316641&amp;s=f61333de1d3ad4de30fd86955257f0cc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Brilliantwarrior

Reputation: 0
• Posts: 11
• Joined: 05-December 12

# Function to return a list of natural numbers

Posted 25 March 2013 - 07:34 AM

I am currently learning Python 3, independently. I'm having difficulty with creating an algorithm to write the function, once I know what I need to do, then I can do it.
Task- Write a function naturalNumbers which takes a positive integer n as input, and returns a list [1, 2, …] consisting of the first n natural numbers.
```def naturalNumbers(n):
d = n+1
for a in range(1,d):
b = [d-a]
print(B)/>

```

I'm supposed to use: a For Statement, List Repetition and/or List Concatenation.
A few pointers in the right direction would be greatly appreciated, thanks.

I'm not sure why the b in "print(" is capitalizing, or adding a forward slash and bracket.

Is This A Good Question/Topic? 0

## Replies To: Function to return a list of natural numbers

### #2 tlhIn`toq

• Left site for a while

Reputation: 5927
• Posts: 12,924
• Joined: 02-June 10

## Re: Function to return a list of natural numbers

Posted 25 March 2013 - 07:42 AM

Its a bug in the syntax highlighter thinking it is a the sunglasses smiley. If you use a variable other than B it won't do that. But

B ) is the sunglass smiley

Or add a space between them

### #3 jon.kiparsky

• Pancakes!

Reputation: 8473
• Posts: 14,661
• Joined: 19-March 11

## Re: Function to return a list of natural numbers

Posted 25 March 2013 - 09:25 AM

You want to return a list of numbers, not print them out.

```def f(n):
return range(1,n+1)

```

will do the job.

There are more convoluted solutions, for example this one with a listcomp:

```def g(n):
return [i+1 for i in range(n)]

```

or this one, which is just annoying:

```deg h(n):
l = []
i = 1
while i<= n:
l.append(i)
i+=1
return l

```

But the first is probably the simplest. There isn't really an algorithm to find here...

EDIT: sorry, didn't see your requirements. I guess the first one is out, even though it's the right answer. The last one should never be written in python, but it's basically a for loop. (if you replace the while with a for i in range(n) then it's really, really embarrassing, no?)

I don't know what a "list repetition" is, but the list comprehension in the second example is in a sense equivalent to a for loop, so that's probably your best bet.
As an interesting exercise, try to work out in what ways a listcomp is and is not equivalent to a for loop. This is not extremely difficult to do, but there are lots of people who don't get this, so you'll be well ahead of the pack if you understand this and can provide examples that prove the point.

This post has been edited by jon.kiparsky: 25 March 2013 - 09:38 AM

Reputation: 1
• Posts: 1
• Joined: 27-May 13

## Re: Function to return a list of natural numbers

Posted 27 May 2013 - 01:50 AM

I myself was intrigued with this problem... took me two days to solve it the way it is meant to be solved (for loop, List Creation or Concatenation), but eventually find out that solution is easier than anything

So, here it is:

```def naturalNumbers(n):
mylist = []
for x in range(1, n+1):
mylist = mylist + [x]
return mylist
```