I want to know whether there is an equivalent statement in lists to do the following. In MATLAB I would do the following
fid = fopen(\'inc.txt\',\'w\')
init
In Python, range(start, stop + 1, step)
can be used like Matlab's start:step:stop
command. Unlike Matlab's functionality, however, range
only works when start
, step
, and stop
are all integers. If you want a parallel function that works with floating-point values, try the arange
command from numpy
:
import numpy as np
with open('numbers.txt', 'w') as handle:
for n in np.arange(1, 5, 0.1):
handle.write('{}\n'.format(n))
Keep in mind that, unlike Matlab, range
and np.arange
both expect their arguments in the order start
, stop
, then step
. Also keep in mind that, unlike the Matlab syntax, range
and np.arange
both stop as soon as the current value is greater than or equal to the stop value.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html