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
2009-04-23 02:16:05
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
2009-04-23 02:19:46
+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
2009-04-23 02:20:11
+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
2009-04-23 02:21:27
+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.
If you need the last number of a list, use myList[-1].
Stefan Kendall
2009-04-23 02:25:33