2

Just a quick question regarding converting loop commands in IDL to python.

I have the following loop structure in IDL syntax:

for ... do begin
   for ... do begin
      if ...
         ...
      endif else begin
         ....
      endelse
   endfor
endfor

Now, I would say that is roughly translates to

for ... :
   for ... :
      if ...
         ...
      end if else:
         ....
      endelse
   endfor
endfor

In python.

However, I would say the endelse and endfor commands are redundant? But what should I replace them with?

veda905
  • 782
  • 2
  • 12
  • 32

4 Answers4

2

You are partially right, you just need to abandon the endelse and endfor and replace else if with elif.

for ... :
   for ... :
      if ...:
         ....
      elif:
         ....

From the Python documentation, here is an example for an If statement:

>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print 'Negative changed to zero'
... elif x == 0:
...     print 'Zero'
... elif x == 1:
...     print 'Single'
... else:
...     print 'More'
...
More

For statement

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print w, len(w)
...
cat 3
window 6
defenestrate 12
Forge
  • 6,538
  • 6
  • 44
  • 64
2

Python grouping relies only on indentation, so you need no end for your if and for loops / groups.

This is valid Python code:

for i in range(5):
    print(i)
bastelflp
  • 9,362
  • 7
  • 32
  • 67
2

However, I would say the endelse and endfor commands are redundant? But what should I replace them with?

You don't replace them with anything.

From the Python tutorial:

The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.

To end a block, simply return to the previous level of indentation.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
1

There is no endif or endfor in python. You unindent the line to indicate that and elif is "else if"

for ... :
   for ... :
      if ...:
         ...
      elif...:

I might be misinterpreting the "end if begin" as a new if statement instead of simply an else. In that case, it is

 for ... :
   for ... :
      if ...:
         ...
      else:
         ... 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • So, would it be elif or else. (I've had two answers one else one elif, I'm more inclined to go with your answer as you have higher reputation...) –  Feb 14 '16 at 13:51
  • Rep doesn't mean anything, just I spend too much time answering questions :) elif requires a condition like if this: do action1, else if that: do action2, else: do action 3. If you are doing a simple if else between like even-odd where there are only two conditions, you can simply do if, then else, and skip the elif – OneCricketeer Feb 14 '16 at 13:55