Skip to content
Advertisement

Printing between two number ranges with a given step value

I am new to loops, and I am trying to iterate over all items in a list, and I need to generate the values between 0 and 2 with a given step value. I have tried to use the “range” function, but cannot get it to work.

The end result should look something like this (doesn’t have to be in a pandas dataframe, just for illustrative purposes):

JavaScript

Here is what I have tried:

JavaScript

The following error is thrown:

JavaScript

How can I properly create my loop?

Advertisement

Answer

np.arange()

You can use numpy.arange() which supports floats as step values.

JavaScript

Expected output:

JavaScript

To include 2 just add the step value once again:

JavaScript

Expected output:

JavaScript

np.linspace()

Alternatively you can use np.linspace(): This has the ability to include the endpoint using endpoint=True;

JavaScript

Expected output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement