How to write a function that takes a positive integer N and returns a list of the first N natural numbers

StacyM

I'm taking this online Python course and trying to solve the following problem called Coding Exercise: It's Natural:

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.

Do I even need a for loop to create a list? Here's my code (which doesn't work obviously). Keep in mind, they have not taught list comprehension. I found this concept on stackoverflow.

def naturalNumbers(n):
   list = [n+1 for i in n]
   return list

Should I take another approach where I create multiple lists of 1,2,3...n and concatenate them all together like [1] + [2] + [3]....

arshajii

Do I even need a for loop to create a list?

No, you can (and in general circumstances should) use the built-in function range():

>>> range(1,5)
[1, 2, 3, 4]

i.e.

def naturalNumbers(n):
    return range(1, n + 1)

Python 3's range() is slightly different in that it returns a range object and not a list, so if you're using 3.x wrap it all in list(): list(range(1, n + 1)).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

From Dev

Matrices: Write a function in MATLAB that returns all of the positive integer numbers that satisfy the following property

From Dev

How write a function that returns a list of 'n' functions based on an input

From Dev

Function in Clojure that takes a list of n letters and returns a list

From Dev

Function in Clojure that takes a list of n letters and returns a list

From Dev

How to find the minimum positive integer in a list of lists in O(n)?

From Dev

How to find the first positive integer number that is not in the list

From Dev

Write a function that takes two lists, and returns True if the first list is the reverse of the same elements in the second list, and False otherwise

From Dev

How to Write a function that takes a word and returns the first and last letter with a * in the middle. [PYTHON]

From Dev

function to find out how many magic squares are in rectangle made of n*m, where n,m - natural numbers

From Dev

Generate a sequence of N natural numbers

From Dev

Minimal positive integer divisible by n

From Dev

A function that takes string parameter and returns integer pointer

From Dev

Python function that takes a list and returns a new list with unique elements of the first list

From Dev

Python: Creating a List of the First n Fibonacci Numbers

From Dev

Function that returns the sum of the first n places of an array

From Dev

function that takes a vector and returns negative numbers and position

From Dev

Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings

From Dev

I want to write a function that takes a list and returns in ascending order with out using the sort built in function

From Dev

Javascript: function which takes in object, and returns list of all numbers described in object

From Dev

How to write the T(n) for this function?

From Dev

Creating a function that takes a list or integer as argument (Python)

From Dev

How to return a stream of the first N Fibonacci numbers?

From Dev

how do I know what n(positive integer) is when user types ./a.out -n?

From Dev

Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

From Dev

Define the function squarefact::Int -> Int that computes for any positive integer n the squared factorial (n!)^2 == (1 * ...* n)^2

From Dev

How to find the positive numbers in a list in Prolog?

From Dev

Program that returns the sum of squares of only the positive numbers in a list

From Dev

JS/Node: A function that takes a number N and returns an array with the values [0, 1, ... N-1]

Related Related

  1. 1

    How to write a recursive method in java that takes in a positive or negative integer and returns the number of digits it has

  2. 2

    Matrices: Write a function in MATLAB that returns all of the positive integer numbers that satisfy the following property

  3. 3

    How write a function that returns a list of 'n' functions based on an input

  4. 4

    Function in Clojure that takes a list of n letters and returns a list

  5. 5

    Function in Clojure that takes a list of n letters and returns a list

  6. 6

    How to find the minimum positive integer in a list of lists in O(n)?

  7. 7

    How to find the first positive integer number that is not in the list

  8. 8

    Write a function that takes two lists, and returns True if the first list is the reverse of the same elements in the second list, and False otherwise

  9. 9

    How to Write a function that takes a word and returns the first and last letter with a * in the middle. [PYTHON]

  10. 10

    function to find out how many magic squares are in rectangle made of n*m, where n,m - natural numbers

  11. 11

    Generate a sequence of N natural numbers

  12. 12

    Minimal positive integer divisible by n

  13. 13

    A function that takes string parameter and returns integer pointer

  14. 14

    Python function that takes a list and returns a new list with unique elements of the first list

  15. 15

    Python: Creating a List of the First n Fibonacci Numbers

  16. 16

    Function that returns the sum of the first n places of an array

  17. 17

    function that takes a vector and returns negative numbers and position

  18. 18

    Write a function that takes as a parameter a list of strings and returns a list containing the lengths of each of the strings

  19. 19

    I want to write a function that takes a list and returns in ascending order with out using the sort built in function

  20. 20

    Javascript: function which takes in object, and returns list of all numbers described in object

  21. 21

    How to write the T(n) for this function?

  22. 22

    Creating a function that takes a list or integer as argument (Python)

  23. 23

    How to return a stream of the first N Fibonacci numbers?

  24. 24

    how do I know what n(positive integer) is when user types ./a.out -n?

  25. 25

    Given a positive integer, n, how can a numerical triangle of height n-1 be printed?

  26. 26

    Define the function squarefact::Int -> Int that computes for any positive integer n the squared factorial (n!)^2 == (1 * ...* n)^2

  27. 27

    How to find the positive numbers in a list in Prolog?

  28. 28

    Program that returns the sum of squares of only the positive numbers in a list

  29. 29

    JS/Node: A function that takes a number N and returns an array with the values [0, 1, ... N-1]

HotTag

Archive