How to get the last number from range () function?

Is there a way to get the last number from a function range()? Do I need to get the last number in the Fibonacci sequence for the first 20 terms or use a list instead range()?

+3
source share
4 answers

Not quite sure that you are here, but here goes:

rangeList = range(0,21)
lastNumber = rangeList[len(rangeList)-1:][0]

or

lastNumber = rangeList[-1]
+7
source

in the range, do you mean the last value provided by the generator? If so, you can do something like this:

def fibonacci(iterations):
    # generate your fibonacci numbers here...


[x for x in fibonacci(20)][-1]

This will give you the last generated value.

+2
source

, - , . , , , n- .

Binet

, myList [-1].

+1
source

This is what you need?

somerange = range(0,20)
print len(somerange) # if you want 20
print len(somerange)-1 # if you want 19

now if you want a number or element to be listed ...

x = [1,2,3,4]
print x[len(x)-1]
# OR
print x[-1] # go back 1 element from current index 0, takes you to list end
0
source

Source: https://habr.com/ru/post/1706934/


All Articles