1

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)
Aryan
  • 3,338
  • 4
  • 18
  • 43

2 Answers2

4

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]
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Craig
  • 4,605
  • 1
  • 18
  • 28
0

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)
kendfss
  • 435
  • 5
  • 11