Java is a strongly typed language, and Python is a weakly typed language.
First look at the for loop used in Java, such as:
package test06;/* * for 循环的条件 * for (循环初始表达式;循环条件表达式;循环后的表达式) */public class Fortest { public static void main(String[] args){ /*打印九九乘法表*/ for(int x=1;x<10;x+=1){ for (int y=1;y<=x;y+=1){ if(y<x){ System.out.print(y+"*"+x+"="+x*y+","); } else { System.out.println(y+"*"+x+"="+x*y+"\n"); } } } }}
Results such as:
And look at the use of the For loop in Python:
for x in range(1,10): for y in range(1,x+1): if y<x: print(str(y)+"*"+str(x)+"="+str(x*y)+",",end="") else: print(str(y) + "*" + str(x) + "=" + str(x * y)+"\n")
Results such as:
Comparison:
A 1.Java variable must specify a type before it is used, and the variable is assigned only to the specified type, otherwise an error is given, and the Python variable uses the assignment to confirm the type itself;
The 2.Java variable in for is only used within a for loop, that is, its scope is confined to the For loop body (we can define the initial variable before the loop body, so it can still be used after the loop body), and Python is different. It can still be used after the for loop body;
Comparison of for loops in Java and Python