Arb
Arb

Reputation: 91

Two nested for with several statements under each in python

I am new in python. I want to write two "for" where each of the has many statements under it. I cannot find how I should differentiate these statement from each other? I need a code like this in python. I appreciate if any one can help.

 for(i=1,i<10,i++){
      statement 1;
      statement 2;
      for(j=1,j<5,j++){
           statement 1;
           statement 1;
      }
      statement 3;
      statement 4;
    }

Upvotes: 0

Views: 36

Answers (2)

Mohammed
Mohammed

Reputation: 426

This is how you do it in python:

for i in range(1, 11):
    print("statement 1 example")
    print("statement 2 example")
    for j in range(1, 6):
        print("statement 1 example")
        print("statement 2 example")
    print("statement 3 example")
    print("statement 4 example")

There are no curly braces, semi-colons
Line indentation is what determines if it's nested or not. Also you need a colon after your for loop. This will help Python For Loop Wiki

Upvotes: 1

Saad Saadi
Saad Saadi

Reputation: 1061

One of the most distinctive features of Python is its use of indentation to mark blocks of code. For example:

if pwd == 'apple':
    print('Logging on ...')
else:
    print('Incorrect password.')

print('All done!')

So your pseudocode will be written like this in python.

for i in range(1, 11):
    statement 1
    statement 2
    for j in range(1, 6):
        statement 1
        statement 2
    statement 3
    statement 4

Upvotes: 0

Related Questions