List of Numbers Python: In this tutorial, we will see how to create a list of numbers in python.

**Contents**hide

## Introduction

The Python language does not only work with variables. They also use lists of variables. A list is a sequence of elements numbered from 0.

To generate a list of numbers or elements, there are several solutions:

- Create a list of numbers using the
**range()**function. - Create a list of numbers using the
**arange()**function. - Create a
**User-Defined**Function (*Naive Approach*). - Create a list of floats using
**linspace()**function. - Create a list of random integers.

This tutorial will explain you how to create a list of numbers simply in python.

## List of Numbers Python

### List of Numbers Using the range() Function

In python, the range() function is very often used. It allows to return a sequence between two numbers given in parameter of the function. If the first parameter is not specified, the **default value will be 0**. The function can also take a third parameter called ** step **which allows to specify the increment.

**Its default value is 1**.

To generate a list of numbers, you can proceed as follows:

```
# 1st example
l = list(range(10))
print(l)
# 2nd example
l = list(range(5, 10))
print(l)
# 3rd example
l = list(range(5, 15, 2))
print(l)
```

** Output**:

```
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[5, 7, 9, 11, 13]
```

**In the first example**, we use only the** stop parameter**, which means that the creation of numbers will start at 0 and end at 10. You can see that the value 10 does not appear in the list, this value is not included because we have to stop at the value 10.

**In the second example**, we have set the** start parameter**. We specify from which value we want to generate our numbers.

In the third example, we used the** step parameter**. This means that we have to generate a number with an increment of 2 between the interval.

It is possible to use the list comprehension method with the range( ) function as well.

```
# Using list comprehension
l = [i for i in range(10)]
print(l)
```

** Output**:

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

It is a very simple and concise way to create a list quickly in Python.

### List of Numbers Using the **arange()** Function

The arange() function in the **NumPy **module allows to create an array similar to the range() function. Here is how to use it:

```
import numpy as np
l = list(np.arange(1, 7))
print(l)
```

** Output**:

```
[1, 2, 3, 4, 5, 6]
```

Like the** range() f**unction**, arange() **function has a **step **parameter to modify the increment.

### Create a **User-Defined** Function (*Naive Approach*).

We can start from 0 and create a native function that generates numbers in an interval with the **append()** function:

```
def createList(start, stop):
if (start == stop):
return start
else:
l = []
while (start < stop + 1):
l.append(start)
start += 1
return l
start = 1
stop=10
print(createList(start, stop))
```

** Output**:

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

### Create a List of Floats Using linspace**()** Function

To generate floating numbers, you can use the linspace() function present in the NumPy module:

```
# Using linspace()
import numpy as np
l = [i for i in np.linspace(5, 15, 10)]
print(l)
```

** Output**:

```
[5.0, 6.111111111111111, 7.222222222222222, 8.333333333333334, 9.444444444444445, 10.555555555555555, 11.666666666666668, 12.777777777777779, 13.88888888888889, 15.0]
```

### Create a List of Random Integers

To generate random numbers in a list, you can use the **randint**() function of the **random **module :

```
# Generate random numbers
import random
l = [random.randint(0, 100) for i in range(10)]
print(l)
```

If you want to generate numbers between 0 and 1 you can this article :

**How to Generate a Random Number Between 0 and 1 in Python**

## Conclusion

In this article we have discussed the different methods to create a list of numbers in Python. The most used function is the range() function; it is simple to use it.

If you have any problem to use one of these functions, don’t hesitate to tell me in the comments.

See you soon for new tutorials !