How to create a list of ranges with incremental steps?

I know that you can create a list from a range of numbers:

list(range(0,20,1))
output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

but I want to take a step in each iteration:

list(range(0,20,1+incremental value)

pe when incremental = +1

expected output: [0, 1, 3, 6, 10, 15]  

Is this possible in python?

+4
source share
3 answers

It is possible, but not with range:

def range_inc(start, stop, step, inc):
    i = start
    while i < stop:
        yield i
        i += step
        step += inc
+5
source

You can do something like this:

def incremental_range(start, stop, step, inc):
    value = start
    while value < stop:
        yield value
        value += step
        step += inc

list(incremental_range(0, 20, 1, 1))
[0, 1, 3, 6, 10, 15]
+4
source

I have further simplified the code above. Think it will do the trick.

List=list(range(1,20))
a=0
print "0"
for i in List:
    a=a+i
    print a

Specifying a range nthgives you all numbers with a specific pattern.

0
source

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


All Articles