In this article, we will discuss how to create a List of numbers from 1 to 100 in Python.
Table Of Contents
Method 1: Using range() function
The range() function in Python, accepts three arguments i.e. start, stop and step. It returns a sequence of integers from start (inclusive) to stop (exclusive) by given step.
To create a list of numbers from 1 to 100, we will pass start as 1, and stop as 101. Default value of step size is 1. Therefore, it will return a sequence of numbers from 1 to 100. Then we can cast that sequence to a list.
Let’s see an example,
Frequently Asked:
# Get a list of numbers from 1 to 100 (including 100) numbers = list(range(1, 101)) print(numbers)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
We created a List of numbers from 1 to 100.
Method 2: Using numpy.arange() function
The arange() function of NumPy module, accepts three arguments i.e. start, stop and step. It returns a numpy array of integers from start (inclusive) to stop (exclusive) by the given step size.
To create a list of numbers from 1 to 100, we will pass start as 1, and stop as 101. Default value of step size is 1. Therefore, it will return an array of numbers from 1 to 100. Then we can call the tolist() function of array, to convert it to a list of numbers.
Latest Python - Video Tutorial
Let’s see an example,
import numpy as np # Get a list of numbers from 1 to 100 (including 100) numbers = np.arange(1, 101).tolist() print(numbers)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
We created a List of numbers from 1 to 100.
Method 3: Using while loop
Create an empty list. Then use the while loop to iterate from 1 till 100, and for each ith number in loop, append number i to the list. This way our list will have numbers from 1 till 100. Let’s see an example,
# Create an empty list numbers = [] i = 1 # Iterate from 1 till 100 while i <= 100: numbers.append(i) i = i + 1 print(numbers)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
We creates a list of numbers from 1 till 100.
Method 4: Using List Comprehension
To create a list of numbers from 1 to 100. Pass the 1, and 101 to the range() function. It will give a sequence of numbers from 1 to 100. Then create a list from them, using List Comprehension. Let’s see an example,
# Create a list of numbers from 1 to 100 numbers = [i for i in range(1, 101)] print(numbers)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
We creates a list of numbers from 1 till 100.
Summary
We learned about four different ways to create a list of numbers from 1 till 100. Thanks.
Latest Video Tutorials