This is impossible to do with Python's range. But this can be accomplished by creating your own generator function.
def myRange(start,end,step):
i = start
while i < end:
yield i
i += step
yield end
for i in myRange(0,99,20):
print(i)
Output:
0
20
40
60
80
99
Answer from Neil on Stack OverflowThis is impossible to do with Python's range. But this can be accomplished by creating your own generator function.
def myRange(start,end,step):
i = start
while i < end:
yield i
i += step
yield end
for i in myRange(0,99,20):
print(i)
Output:
0
20
40
60
80
99
First of all, it usually does not makes much sense to include the end condition, the idea of a range is to perform hops until the end value is reached.
Nevertheless, you can for instance use itertools.chain for that:
from itertools import chain
for i in chain(range(0,99,20), [99]):
# ...
pass
chain concatenates iterables together. So after the range(..) is exhausted, it will iterate over the next iterable, and so on.
The above approach is not very elegant: it requires some knowledge about how chain works. We can however encapsulate that logic:
def range_with_end(start, stop, step):
return chain(range(start, stop, step), (stop,)) python - loop at exact time intervals - Stack Overflow
python - How do I loop through a list by twos? - Stack Overflow
how would I iterate over a list at set intervals?
time - Python loop to run for certain amount of seconds - Stack Overflow
Videos
Ideally one would use threading to accomplish this. You can do something like
import threading
interval = 15
def myPeriodicFunction():
print "This loops on a timer every %d seconds" % interval
def startTimer():
threading.Timer(interval, startTimer).start()
myPeriodicFunction()
then you can just call
startTimer()
in order to start the looping timer.
Consider tracking the time it takes the code to run (a timer() function), then sleeping for 15 - exec_time seconds after completion.
start = datetime.now()
do_many_important_things()
end = datetime.now()
exec_time = end - start
time.sleep(15-exec_time.total_seconds())
You can use a range with a step size of 2:
Python 2
for i in xrange(0,10,2):
print(i)
Python 3
for i in range(0,10,2):
print(i)
Note: Use xrange in Python 2 instead of range because it is more efficient as it generates an iterable object, and not the whole list.
You can also use this syntax (L[start:stop:step]):
mylist = [1,2,3,4,5,6,7,8,9,10]
for i in mylist[::2]:
print i,
# prints 1 3 5 7 9
for i in mylist[1::2]:
print i,
# prints 2 4 6 8 10
Where the first digit is the starting index (defaults to beginning of list or 0), 2nd is ending slice index (defaults to end of list), and the third digit is the offset or step.
my_list=[('x',9.99),('y',12.55),('p',9.99),('i',55.55)]Lets say I want to add every 2 numerical values together.
Try this:
import time
t_end = time.time() + 60 * 15
while time.time() < t_end:
# do whatever you do
This will run for 15 min x 60 s = 900 seconds.
Function time.time returns the current time in seconds since 1st Jan 1970. The value is in floating point, so you can even use it with sub-second precision. In the beginning the value t_end is calculated to be "now" + 15 minutes. The loop will run until the current time exceeds this preset ending time.
If I understand you, you can do it with a datetime.timedelta -
import datetime
endTime = datetime.datetime.now() + datetime.timedelta(minutes=15)
while True:
if datetime.datetime.now() >= endTime:
break
# Blah
# Blah