Java | Python 流程控制对比
生活随笔
收集整理的這篇文章主要介紹了
Java | Python 流程控制对比
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
| ? | Java | Python | ? |
| 數(shù)據(jù)類型 | byte、short、int、long、float、double、char、boolean 數(shù)組、類、接口 | Number(數(shù)字):?int、float、bool、complex(復(fù)數(shù)) String(字符串) List(列表) Tuple(元組) Set(集合) Dictionary(字典): 相當(dāng)于map? | ? |
| if-else | 1 int num = 3;
2 if (num > 0 && num < 4) {
3 System.out.println("4舍");
4 } else if (num > 5 && num < 10) {
5 System.out.println("5入");
6 } else {
7 System.out.println("不舍不入");
8 } ? | 1 num = 3
2 if num > 0 and num < 4:
3 print("4舍")
4 elif num > 5 and num < 10:
5 print("5入")
6 else:
7 print("不舍不入") ? | Python條件后面用冒號(hào) ,縮進(jìn)劃分語句塊 Python不支持&& Python用?elif?代替?else if |
| 三元運(yùn)算 | 1 int a = 3, b; 2 b = (a > 1) ? 200 : 400; | ?1 a = 3 2 b = 200 if a > 1 else 400 3 print(b)? | Python通過if-else實(shí)現(xiàn)三元運(yùn)算 |
| while | 1 int y = 1; 2 while (y < 10) { 3 System.out.println("y=" + y); 4 y++; 5 } | 1 y = 1 2 while y < 10: 3 print('y =', y) 4 y += 1 | Python不支持 i++ Python +和 Java并不一樣 Python不支持 do-while |
| for | 1 for (int x = 0; x < 5; x++) { 2 for (int y = x + 1; y < 5; y++) { 3 System.out.print(" "); 4 } 5 6 for (int y = 0; y <= x; y++) { 7 System.out.print("* "); 8 } 9 System.out.println(); 10 } | 1 x, y, z = 0, 0, 4 2 for x in range(5): 3 for y in range(z): 4 print(' ', end = '') 5 z -= 1 6 for y in range(x + 1): 7 print('* ', end = '') 8 print('\n', end = '') | Python中end = '' 不換行 |
| switch | 1 char ch = '1'; 2 switch (ch) { 3 default: 4 System.out.println("error"); 5 break; 6 case '1': 7 System.out.println("兩"); 8 break; 9 case '2': 10 System.out.println("顆"); 11 break; 12 case '3': 13 System.out.println("糖"); 14 break; 15 } | 1 def switch(var): 2 return { 3 '1': '兩', 4 '2': '顆', 5 '3': '糖' 6 }.get(var,'error') 7 print(switch('2')) | ?Python通過字典實(shí)現(xiàn)switch |
| ? | ? | ? | ? |
| ? | ? | ? | ? |
轉(zhuǎn)載于:https://www.cnblogs.com/sunjunxi/p/8494145.html
總結(jié)
以上是生活随笔為你收集整理的Java | Python 流程控制对比的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目拆分子工程(简单版)
- 下一篇: python模块中的__all__属性