Python同Java及C++的不同之处

1. C++、Java对变量的定义很严格 比如 int a = 0  python则直接定义 a=0

2. C++、Java代码结束时需要用 ;隔开 比如 int a =0; ,而python则不用   a=0 直接换行即可

3. C++、Java中的循环或者判断需要用{}括起来 for (i=0;i<5;i++){ },python使用: for i in(1,5):

4. C++、Java代码中没有严格的代码对齐要求,但是python中有严格的对其关系

5. 输入输出语句不同,

              C++:

              string a;

              cin >> a;

              cout << a;

              Java:

              import java.util.Scanner;

              Scanner sc = new Scanner(System.in);

              String a=sc.nextLine(); System.out.println(a);

              python:

              a=input()

              print(a)

6.计算精度不同:计算1/1000时,python和c++的结果都是0.001,java得到的结果是0

              Python:print(1/(1000))

              Java:System.out.println(1/1000);

              C++:cout << 1/pow(10,3);

7.判断结构不同

              Python

              a="true"

              b="true"

              if a==b:

              print("两个值相等")

              Java

              String a="true";

              String b="true";

              if(a.equals(b))

              {

              System.out.println("两值相等");

              }

              C++

              string a = "true";

              string b = "true";

              if (a.compare(b)==0)

              {

              cout << "两个值相等";

              }

8.异常捕获处理结构:java和c++使用的是try catch结构,python使用的是try except结构

              Python

              try:

              print(1/0)

              except Exception as e:

              print("除数不能为0")

              Java

              try {

              System.out.println(1/0);

              } catch (Exception e) {

              // TODO: handle exception

              System.out.println("除数不能为0");

              }

9.i++ :C++、Java中使用i++进行i+1操作,在python中不存在i++

10.python 中没有 &&  ,!, ||(但存在&(与)、|(或)、!=(不等于)) 这3个运算符,在逻辑表达式中写成这3个会报逻辑错误的。要实现同样的功能,要写成 and,not,or

猜你喜欢

转载自www.cnblogs.com/lixv2018/p/12716667.html