-7

I know the similar question has been asked before and I looked through a lot of previous questions but couldn't find the answer hence I am posting it.

I have a basic question about for loop. I have been using VBA for a long time and recently have to learn Python for work. I will need to use FOR LOOP in the middle of the code and looks like there in no NEXT following the FOR in python. For example, this is what the code looks like in VBA:

sub test()
dim i as integer
dim j as integer

for I = 1 to 10
     j = j+i
Next
.

Print(j)
.
.
'rest of the code

end

Can someone please tell me how to define the FOR LOOP in the middle of the code? Thanks

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Ahsan Khan
  • 57
  • 1
  • 2
  • 10
  • 2
    `for i in range(1,10):` I guess given 10 is exclusive. Otherwise it is `for i in range(1,11):` – Willem Van Onsem Jan 31 '17 at 17:13
  • Hey @WillemVanOnsem it was just an example. I am trying to ask where should I put the "NEXT" and continue the remaining code outside FOR LOOP? – Ahsan Khan Jan 31 '17 at 17:19
  • 3
    You really should read a tutorial which will cover such basics. If you know VBA, Python is pretty easy. If you are an experienced programmer, just 2 or 3 hours will be enough to get a basic working knowledge (though obviously far more time to develop any expertise). Python variables are a lot like VBA variants, and for loops are like VBA `for-each` loops used to iterate over collections more than counter-based `for-next` loops. – John Coleman Jan 31 '17 at 17:21
  • @AhsanKhan: there is no `Next`. Python forces indentation: as long as there is indentation, you are still in the body of the loop. – Willem Van Onsem Jan 31 '17 at 17:21
  • 1
    [Getting started with Python](http://stackoverflow.com/documentation/python/193/getting-started-with-python-language#t=20170131172159655508) and [Loops](http://stackoverflow.com/documentation/python/237/loops#t=201701311722575628371) in Python, on Documentation.SO. – Mathieu Guindon Jan 31 '17 at 17:22
  • Possible duplicate of [for loop in Python](http://stackoverflow.com/questions/4170656/for-loop-in-python) – Chris_Rands Jan 31 '17 at 17:22
  • Thank you all for the help. – Ahsan Khan Jan 31 '17 at 17:30

3 Answers3

0

In python for loop are used over iterable object or generator. List:

num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in num:
    print x

Generator:

for x in range(10):
    print x

For more example, see https://wiki.python.org/moin/ForLoop

Your question not very clear, so if this does not answer your question do not hesitate to edit your message.

lelabo_m
  • 509
  • 8
  • 21
  • This is generator in python 3.* for python 2.7 it creates array ( xrange - generator implementation ) – iklinac Jan 31 '17 at 17:27
0

Python alternative for your function

def test():
    for i in range(1, 10):
        print(i)

Note out that python has no variable declaration as it is not strongly-typed language. Also instead of next it has indentation as depth of execution.

iklinac
  • 14,944
  • 4
  • 28
  • 30
-1

This will also do your job.

for i in range(10): # from 0 to 9.
  j = j + i
Community
  • 1
  • 1
Roshan Nalawade
  • 172
  • 1
  • 16