python基础(2)-运算符&while循环

时间:2022-10-17 02:44:53

算术运算符

 a=5;
 b=2;
 print(a+b);#result:7 desc:加
 print(a-b);#result:3 desc:减
 print(a*b);#result:10 desc:乘
 print(b**10);#result:1024 desc:幂
 print(a/b);#result:2.5 desc:除
 print(a//b);#result:2 desc:取整除
 print(a%b);#result:1 desc:取余

 print(not True);#false desc:对应java中 !
 print(True and False);#false desc:对应java中 &&
 print(True or False);#true desc:对应java中 ||

if/else

取3个数中的最大值

 num_1 = int(input());
 num_2 = int(input());
 num_3 = int(input());
 max_num = 0;
 if num_1 > num_2:
     max_num = num_1;
 else:
     max_num = num_2;
 if max_num < num_3:
     max_num = num_3
 print(max_num);    

while循环

猜数字

 true_num = ';
 while True:
     input_num = input("input a num:");
     if input_num == true_num:
         print("猜数字成功!");
         break;

九九乘法表

 i = 1;
 while i <= 9:
     j = 1;
     while j <= i:
         print(str(j) + "*" + str(i) + "="+  str(i*j),end=" ");
         j+=1;
     i += 1;
     print();

打印一个10行的三角形

 height = 10;
 i = 1;
 while i <= height:
     j = 1;
     while j <= i:
         print("*",end="")
         j += 1;
     i += 1;
     print();