1 判断一个数字是否是质数.
for x in range(100): for y in range(2, x): if x%y == 0: break else: print(x)
2 写代码实现 1-3+5-7+9-11+.....-99=?
i = 1 s = 0 while i<=100: if i%4 == 1: s = s + i else: s = s - i i = i + 2 print(s)
3 计算∏, 计算公司为∏/4 = 1-1/3+1/5-1/7+1/9+........., 直到最后一项的绝对值小于10的负六次方为止.
n = 0 p = 1 while p<1000000: if p%4 == 1: n = n + 1/p else: n = n -1/p p = p + 2 print(n*4)
4 制作简易版的计算器
a = input('请输入第一个数字:') a = int(a) b = input('请输入运算符:') c = input('请输入第二个数字:') c = int(c) if b == '+': print(a,b,c,'=',a+c) elif b == '-': print(a,b,c,'=',a-c) elif b == '*': print(a,b,c,'=',a*c) else: print(a,b,c,'=',a/c)
5 判断一个数字是奇数还是偶数
a = int(input('请输入一个数字:')) if a%2 == 0: print(a, '是偶数') else: print(a, '是奇数')
6 一位四位数乘以9等于它的逆序, 即abcd*9 = dcba
abcd = 1000 while abcd<10000: a = abcd//1000 bcd = abcd%1000 b = bcd//100 cd = bcd%100 c = cd//10 d = cd%10 if d*1000+c*100+b*10+a == abcd: print(abcd) abcd = abcd+1
7 用while循环计算100项的平方数列和, 即1+4+9+....+100000
a = 1 s = 0 while a<=100: s = s+a*a a = a + 1 print(s)