1.floor除法(这里只介绍除法运算,其他暂且不提)
在Python3中,除法运算有两种,一种是真除,一种是Floor除法,这两者是有分别的,分别如代码所示:
2.幂的使用(**)
a = 2**2**32 b = (2 **2)**32
3.分别求一百以内的偶数和奇数,注意代码的区别。
#求100以内的偶数(包含100) num = 100 while num: if num % 2: print() else: print(num) num = num - 1
#求一百以内的偶数(不包含100) num = 100 while num: num = num - 1 if num % 2: print() else: print(num)
#求100以内的奇数 num = 100 while num: num = num - 1 if num % 2: print(num) else: print()
4,求阶乘
i = 1 x = 7 flag = 0 while i < 100: i = i + 1 if (x%2==1)and(x%3==2)and(x%5==4)and(x%6==5): flag = 1 else: x = 7*i if flag ==1: print("阶梯数是:",x) else: print("请扩大范围!")
附小甲鱼源代码:
x = 7 i = 1 flag = 0 while i <= 100: if (x%2 == 1) and (x%3 == 2) and (x%5 == 4) and (x%6==5): flag = 1 else: x = 7 * (i+1) # 根据题意,x一定是7的整数倍,所以每次乘以7 i += 1 if flag == 1: print(‘阶梯数是:‘, x) else: print(‘在程序限定的范围内找不到答案!‘)
时间: 2024-10-10 16:57:38