Create a List from 1 to 100 in Python

04/09/2023

Contents

In this article, you will learn how to create a list from 1 to 100 in Python.

Creating a list from 1 to 100

In Python, we can easily create a list from 1 to 100 using different methods. Here are some of the most common methods:

Using the Range Function

The range() function in Python generates a sequence of numbers. We can use the range() function to create a list of numbers from 1 to 100. The syntax for using this method is as follows:

my_list = list(range(1, 101))

Here is an example:

my_list = list(range(1, 101))
print(my_list)
# Output: [1, 2, 3, ..., 99, 100]

Using List Comprehension

List comprehension is a concise way of creating new lists in Python. We can use list comprehension to create a list of numbers from 1 to 100. The syntax for using this method is as follows:

my_list = [i for i in range(1, 101)]

Here is an example:

my_list = [i for i in range(1, 101)]
print(my_list)
# Output: [1, 2, 3, ..., 99, 100]

Using a For Loop

We can also use a for loop to create a list of numbers from 1 to 100. The syntax for using this method is as follows:

my_list = []
for i in range(1, 101):
    my_list.append(i)

Here is an example:

my_list = []
for i in range(1, 101):
    my_list.append(i)
print(my_list)
# Output: [1, 2, 3, ..., 99, 100]

Using Numpy Library

The numpy library provides a function arange() which can be used to create an array of numbers. We can use the tolist() method to convert the array to a list. The syntax for using this method is as follows:

import numpy as np
my_list = np.arange(1, 101).tolist()

Here is an example:

import numpy as np
my_list = np.arange(1, 101).tolist()
print(my_list)
# Output: [1, 2, 3, ..., 99, 100]