I wanted to find a way i can the first n negative integers so if i put in 7 it would produce [-1,-2,-3,-4,-5,-6,-7]
import stdio
import sys
n = int(input(sys.argv[0]))
arr= list(range(-1,n))
print(arr)
I wanted to find a way i can the first n negative integers so if i put in 7 it would produce [-1,-2,-3,-4,-5,-6,-7]
import stdio
import sys
n = int(input(sys.argv[0]))
arr= list(range(-1,n))
print(arr)
In Python, range()
defaults to counting up by 1. You can change this by providing a negative value for step, which is the third argument to range()
. You will also need to provide the correct stop value, which in your case is -n-1
.
import stdio
import sys
n = int(input(sys.argv[0]))
arr = list(range(-1,-n-1,-1))
print(arr)
[-1, -2, -3, -4, -5, -6, -7]
Another option is to use a "List Comprehension"
import stdio
import sys
n = int(input(sys.argv[0]+'\nPick a number, any number\n'))
arr= [-i for i in range(1,n+1)]
print(arr)