How to Create a List of Numbers from n to 0 in Python?

Estimated read time 1 min read

You can use a for loop to create a list of numbers from n to 0 in Python. Here is an example code snippet:

n = 10   # change this value to the desired number
my_list = []

for i in range(n, -1, -1):
    my_list.append(i)

print(my_list)

This code starts with n and counts down to 0 by iterating backwards using the range() function. The range() function takes three arguments: the starting number (n), the ending number (-1), and the step size (-1). The append() method adds each number to the my_list list. Finally, the print() function displays the contents of the list.

Output:

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

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply