Creating a list from 1 to n in Python can be accomplished using the range()
function and list comprehension. Here are two examples:
Example 1:
n = 5 numbers = list(range(1, n+1)) print(numbers)
Output:
[1, 2, 3, 4, 5]
Example 2:
n = 10 numbers = [i for i in range(1, n+1)] print(numbers)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]