How to Create a List of Numbers in Python
To create a list of numbers in Python, you can use [] brackets, the range() function, or a for loop.
The following examples show how to create a list of numbers in Python using three different methods.
Using [] Brackets
We can use [] brackets to create a list of numbers.
Suppose we have the following data:
# Create list of numbers
list_numbers = [1, 2, 3, 4, 5, 6]
# Show list
print(list_numbers)
Output: 👇️
[1, 2, 3, 4, 5, 6]
In this example, we use [] brackets to create a list of numbers list_numbers. The output shows the list of numbers created using [] brackets.
Using range() Function
We can use the range() function to create a list of numbers.
Suppose we have the following data:
# Create list of numbers
numbers = list(range(1, 11))
# Show list
print(numbers)
Output: 👇️
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In this example, we use the range() function to create a list of numbers numbers. The output shows the list of numbers created using the range() function.
Using for Loop
We can use a for loop to create a list of numbers.
Suppose we have the following data:
# Declare empty list
numbers = []
# Add numbers to list
for i in range(1, 11):
numbers.append(i)
# Show list
print(numbers)
Output: 👇️
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In this example, we use a for loop to create a list of numbers numbers.
The output shows the list of numbers created using the for loop.
Conclusion
We can use [] brackets, the range() function, or a for loop to create a list of numbers in Python.