Java for loop in Python example -


i have in java:

    public static void main(string[] args) {      int x = 10;      for(int = 0; <= x; i++ ){         system.out.println("i = " + + "******");         for(int j = 0; j <= i; j++){             system.out.print("j = " + j + " ");         }      } 

with output:

run: = 0****** j = 0 = 1****** j = 0 j = 1 = 2****** j = 0 j = 1 j = 2 = 3****** j = 0 j = 1 j = 2 j = 3 = 4****** j = 0 j = 1 j = 2 j = 3 j = 4 = 5****** j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 = 6****** j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 = 7****** j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 = 8****** j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 = 9****** j = 0 j = 1 j = 2 j = 3 j = 4 j = 5 j = 6 j = 7 j = 8 j = 9 = 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 achieve effect in python loop:

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

output:

i =  0 ******* =  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, know java @ intermediate level , cant start thinking in pyhon loops. python 3.6

the upper bound of range(..) exclusive, need add 1 upper bound:

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

if step 1, not have mention this. default python uses 1 step.

furthermore default python separate 2 arguments space, can use sep parameter split no space (or character sequence).

this prints:

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

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -