views:

406

answers:

4

there is a way to get the last number in range i need to get the last number in fibonacci sequence for first 20 terms or can to do with a list instead range?

A: 

Is this what you're after?

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

now if you want the number or item contained in a list...

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
John T
thanks for answer but withfor i in range(0,21):print all numbers 0,1,2...and i need only the last 20 for the example
franklinrony
+2  A: 

Not quite sure what you are after here but here goes:

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

or:

lastNumber = rangeList[-1]
Cannonade
thanks te code is fine for me and work :)
franklinrony
glad to hear it :)
Cannonade
+2  A: 

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

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


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

That would get you the last generated value.

vezult
+1  A: 

I don't think anyone considered that you need fibonacci numbers. No, you'll have to store each number to build the fibonacci sequence recursively, but there is a formula to get the nth term of the fibonacci sequence.

Binet's Formula

If you need the last number of a list, use myList[-1].

Stefan Kendall
interesante también, muchas gracias
franklinrony
interesting too, many thanks
franklinrony