0

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

1
  • sep="",end=" " should work in your case. Default separation between arguments sep is set to " ". Python range doesnt include end value, so loops would have to go to x+1 and i+1 Commented Jul 28, 2017 at 12:47

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i got it now.
3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.