+ 2
Range(-9) is it possible??
As we are printing sequence of numbers with range() fun..is it possible to print -ve series with this function with out using operator( -) externally (not in range.) ex: numbers = list(range(10)) print(numbers) numbers = list(range(-10)) print(numbers) o/p: [1,2,3,4,5,6,7,8,9] [] # why is this happening for -ve 's first list is printed but for second iam getting error.
3 odpowiedzi
+ 3
You may need range(0, -10, -1)
Actually 'range' takes three arguments.
First one determine start value(inclusive), second one determine end value(exclusive) and third one dtermine increment size.
Third argument can be omitted, then default value 1 is used.
range also takes a single argument.
In this case, start value is 0, end value is the given, and increment is 1.
So range(10) is equivalent to range(0, 10, 1).
Likewise, range(-10) is same as range(0, -10, 1).
It is empty because start value is greater than end value despite of positive increment.
If you want to get 0, -1, ..., -9, use range(0, -10, -1) instead.
It starts from zero, increments -1 (decrements 1) and ends just before -10.
0
range(start, stop[, step])
range(-9,0)
Gives this output:
-9
-8
-7
-6
-5
-4
-3
-2
-1
https://docs.python.org/3/library/functions.html#func-range
numbers = list(range(10))
print(numbers)
numbers = list(range(0, -10, -1))
print(numbers)
numbers = list(range(-9, 1, 1))
print(numbers)
0
If range is called with one argument, it produces an object with values from 0 to that argument.
If it is called with two arguments, it produces values from the first to the second.