0

I want to run the for loop from 0 to 64 with step size of 0.05. If I use the range function it gives a Typeerror. My code

for i in range(0,64,2):
   P=0+i
   Q=2+i
   for s in range(P,Q,0.05):
       X=s

I am actually converting a FORTRAN code into python and in that code, 0.05 was usesd as the step size. The code:

DO 20 I =0,64,2
    P=0+i
    Q=2+i
DO 10 s=P,Q,0.05
   X=s
   IF((X.GE.P).AND.(X.LT.(P+Q/2))) THEN
         Y = -1
   ELSEIF (X.GE.(P+Q/2).AND.(X.LT.Q))
        Y=1
   ENDIF
        WRITE(*,*)y

Please help me how can I convert this code properly into python. Edit: Please check the rest of the code and I dont understand what this ENDIF statement does.

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
Ahmad
  • 11
  • 5
  • Check `numpy.arange()`. It works just as `range()` with decimals. – Mathieu Jul 13 '18 at 11:57
  • But I don't want to initialize an array, I want to run the loop just as in MATLAB or others – Ahmad Jul 13 '18 at 13:19
  • `numpy.arange()` does not return an array. It is a generator, just as `range()`. Read the doc. – Mathieu Jul 13 '18 at 13:22
  • @Mathieu Thanks, please can you also tell me that what that ENDIF statement does in fortran and how to write that in python? – Ahmad Jul 13 '18 at 13:24
  • No clue, I never used Fortran. – Mathieu Jul 13 '18 at 13:26
  • `endif` does nothing - it isn't an executable statement, it is just syntax to close the whole `if` construct. The `write` statement following it has nothing to do with it; the indentation is misleading. – Rodrigo Rodrigues Jul 13 '18 at 14:44
  • @RodrigoRodrigues thanks, I got this code from a 1995 Thesis. I have run the code and it doesn't seem to do the stuff it says it would – Ahmad Jul 13 '18 at 14:48
  • You may want to open a different question for other parts, also note that if your Fortran code does use arrays elsewhere, Fortran arrays are 1-indexed instead of 0-indexed as Python/C/Java/most other languages I use are – Foon Jul 13 '18 at 14:50
  • @Foon got it. I might open a new question – Ahmad Jul 13 '18 at 14:53

2 Answers2

0

You can use this;

for s in [x * 0.1 for x in range(P, Q)]:
    X=s
Agile_Eagle
  • 1,710
  • 3
  • 13
  • 32
0

I think you can do something like this:

for i in range(0,64,2):
   P=0+i
   Q=2+i
   for s in range(P,Q*20):
       X=s/20
Cyphall
  • 348
  • 1
  • 10
  • `numpy.arange()` is simpler. – Mathieu Jul 13 '18 at 11:56
  • He may not want to use numpy just for one line. – Cyphall Jul 13 '18 at 11:58
  • Correct, I am not trying to initialize an array, actually the code is different and has an equation for which the condition is applied to check for each small iteration that it satisfies and prints the value. – Ahmad Jul 13 '18 at 13:17