python基礎(2)-運算符&while循環

算術運算符 

 1 a=5;
 2 b=2;
 3 print(a+b);#result:7 desc:加
 4 print(a-b);#result:3 desc:減
 5 print(a*b);#result:10 desc:乘
 6 print(b**10);#result:1024 desc:冪
 7 print(a/b);#result:2.5 desc:除
 8 print(a//b);#result:2 desc:取整除
 9 print(a%b);#result:1 desc:取餘
10 
11 print(not True);#false desc:對應java中 !
12 print(True and False);#false desc:對應java中 &&
13 print(True or False);#true desc:對應java中 ||

if/else

取3個數中的最大值

 1 num_1 = int(input());
 2 num_2 = int(input());
 3 num_3 = int(input());
 4 max_num = 0;
 5 if num_1 > num_2:
 6     max_num = num_1;
 7 else:
 8     max_num = num_2;
 9 if max_num < num_3:
10     max_num = num_3
11 print(max_num);    

while循環 

猜數字

1 true_num = '45';
2 while True:
3     input_num = input("input a num:");
4     if input_num == true_num:
5         print("猜數字成功!");
6         break;

 九九乘法表

1 i = 1;
2 while i <= 9:
3     j = 1;
4     while j <= i:
5         print(str(j) + "*" + str(i) + "="+  str(i*j),end=" ");
6         j+=1;
7     i += 1;
8     print();

打印一個10行的三角形

1 height = 10;
2 i = 1;
3 while i <= height:
4     j = 1;
5     while j <= i:
6         print("*",end="")
7         j += 1;
8     i += 1;
9     print();
相關文章
相關標籤/搜索