This crops up every now and then for me: I have some C# code badly wanting the range()
function available in Python.
I am aware of using
for (int i = 0; i < 12; i++) { // add code here }
But this brakes down in functional usages, as when I want to do a Linq Sum()
instead of writing the above loop.
Is there any builtin? I guess I could always just roll my own with a yield
or such, but this would be so handy to just have.
range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0] . Therefore the last integer generated by range() is up to, but not including, stop . For example range(0, 5) generates integers from 0 up to, but not including, 5.
Definition and Usage. The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
The range() function has two sets of parameters: range(stop) range(start, stop, step)
You're looking for the Enumerable.Range
method:
var mySequence = Enumerable.Range(0, 12);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With