Comparison of for loops in Java and Python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.