JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

List of numbers from 1 to N in Python

Author:JIYIK Last Updated:2025/05/09 Views:

This tutorial will discuss how to create a list of numbers from 1 to some specified number.


Create a user-defined function to create a list of numbers from 1 to N

This method will take the desired number from the user and foriterate until that number using a loop. In each iteration, we will increment the value and append that number to the list.

The following code will explain this.

def createList(n):
    lst = []
    for i in range(n + 1):
        lst.append(i)
    return lst


print(createList(10))

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Use range()the function to create a list of numbers from 1 to N.

range()The function is very commonly used in Python. It returns a sequence between two numbers given in the function arguments. If not specified, it defaults to 0. It also has a stepparameter called which can specify the increment, which is 1 by default.

In the following code, we will use this function to generate a list of numbers.

lst = list(range(1, 10 + 1))
print(lst)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note list()the use of the function. It ensures that the final result appears in list form. Also, note the use of +1to ensure that the final number is also included in the list.

We can also range()use list comprehension method with function. List comprehension is a simple and clear way to create lists in Python.

The method looks like this:

lst = [i for i in range(1, 10 + 1)]
print(lst)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

numpy.arange()Create a list of numbers from 1 to N using

NumPyThe module has many useful methods to create and modify arrays. arange()The function in this module is similar to the function discussed earlier range(). The final output is a numpy array.

We will implement this functionality in the code below.

import numpy as np

lst = list(np.arange(1, 10 + 1))
print(lst)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We also use list()the function to convert the final output into list form.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

Get the index of the maximum and minimum values in a list in Python

Publish Date:2025/05/09 Views:117 Category:Python

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi

How to Find the Maximum Value in a List in Python

Publish Date:2025/05/09 Views:99 Category:Python

This tutorial will demonstrate how to find the maximum value in a list in Python. This tutorial will cover a number of scenarios and data types, from simple lists of integers to more complex structures like arrays within arrays. for Finding

Convert a list to a set in Python

Publish Date:2025/05/08 Views:145 Category:Python

This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values ​​in sequence. Python list stores comma separated values ​​between

Remove the first element from a list in Python

Publish Date:2025/05/08 Views:173 Category:Python

This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which

Convert a list to lowercase in Python

Publish Date:2025/05/08 Views:112 Category:Python

Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin

Remove all occurrences of an element from a Python list

Publish Date:2025/05/08 Views:69 Category:Python

In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial