Buckethead
Buckethead

Reputation: 135

Java for loop in Python example

I have in java:

    public static void main(String[] args) {

    int x = 10;

    for(int i = 0; i <= x; i++ ){
        System.out.println("i = " + i + "******");
        for(int j = 0; j <= i; j++){
            System.out.print("j = " + j + " ");
        }

    }

with output:

run:
i = 0******
j = 0 i = 1******
j = 0 j = 1 i = 2******
j = 0 j = 1 j = 2 i = 3******
j = 0 j = 1 j = 2 j = 3 i = 4******
j = 0 j = 1 j = 2 j = 3 j = 4 i = 5******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 i = 6******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 7******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 i = 8******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 i = 9******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 i = 10******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10 

I'd like to achieve exactly effect in python for loop:

x = 10

for i in range(0, x, 1):
    print("i = ", i, "*******")
    for j in range(0, i, 1):
        print("j = ", j, end="")

Output:

i =  0 *******
i =  1 *******
j =  0i =  2 *******
j =  0j =  1i =  3 *******
j =  0j =  1j =  2i =  4 *******
j =  0j =  1j =  2j =  3i =  5 *******
j =  0j =  1j =  2j =  3j =  4i =  6 *******
j =  0j =  1j =  2j =  3j =  4j =  5i =  7 *******
j =  0j =  1j =  2j =  3j =  4j =  5j =  6i =  8 *******
j =  0j =  1j =  2j =  3j =  4j =  5j =  6j =  7i =  9 *******
j =  0j =  1j =  2j =  3j =  4j =  5j =  6j =  7j =  8

I started learning python, I know java at intermediate level and cant start thinking in those pyhon loops. Python 3.6

Upvotes: 0

Views: 859

Answers (3)

Rohit-Pandey
Rohit-Pandey

Reputation: 2159

Simple solution of your problem.

x=10
for i in range(0,x+1):
print "i = "+str(i)+"******"
for j in range(0,i+1):
    print "j = "+str(j)+" ",

output:-

i = 0******
j = 0  i = 1******
j = 0  j = 1  i = 2******
j = 0  j = 1  j = 2  i = 3******
j = 0  j = 1  j = 2  j = 3  i = 4******
j = 0  j = 1  j = 2  j = 3  j = 4  i = 5******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  i = 6******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  j = 6  i = 7******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  j = 6  j = 7  i = 8******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  j = 6  j = 7  j = 8  i = 9******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  j = 6  j = 7  j = 8  j = 9  i = 10******
j = 0  j = 1  j = 2  j = 3  j = 4  j = 5  j = 6  j = 7  j = 8  j = 9  j = 10

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

The upper bound of range(..) is exclusive, so you simply need to add one to the upper bound:

x = 10

for i in range(0, x+1):
    print("i = ",i,"*******",sep='')
    for j in range(0, i+1):
        print("j = ",j, sep='', end=' ')

If the step is 1, you do not have to mention this. By default Python uses 1 as step.

Furthermore by default Python will separate two arguments with a space, you can use the sep parameter to split it with no space (or another character sequence).

This prints:

i = 0*******
j = 0 i = 1*******
j = 0 j = 1 i = 2*******
j = 0 j = 1 j = 2 i = 3*******
j = 0 j = 1 j = 2 j = 3 i = 4*******
j = 0 j = 1 j = 2 j = 3 j = 4 i = 5*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 i = 6*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 i = 7*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 i = 8*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 i = 9*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 i = 10*******
j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 j = 10

Upvotes: 7

Błotosmętek
Błotosmętek

Reputation: 12927

You want to use end=" " rather than end="" to have a space between a value of j and the string j = from next iteration. Besides, the result of range does not include the upper limit, you have to add 1 to compensate for it. And finally, instead of:

print("i = ", i, "*******")

use

print("i = {}*******".format(i))

to exactly reproduce your Java output.

Upvotes: 3

Related Questions