Python编程练习题
1 求可用被17整除的所有三位数
for num in range(99,1000): if num % 17 == 0: print num
ps:下面的写法和上面的写法性能的差距,上面好吧?
for num in range(99,1000): if num % 17 == 0: print num else: continue
2 连续整数之合
>>> sum = 0 >>> x = int (raw_input ('pls input x: ')) pls input x: 22 >>> for num in range(0,x): ... sum += num ... >>> print sum 231 >>>
累加
x = int(raw_input('please input x : ' ))
layer = 1
while layer <= x:
lsum = 0
count =1
while count <= layer:
lsum += count
count += 1
fstr = ''
for num in range(1 , layer ):
fstr = fstr + str(num) + '+'
fstr += str(layer)
print fstr,'=', lsum
layer += 1
============== please input x : 5
1 = 1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
# 如何控制空格的数量让等号在一起呢
整数和能整除数字个数,奇数
x = int(raw_input('please input x : ' )) for num in range(1 , x ): #print num sum = 0 for y in range(1,num + 1 ): sum += y if sum % num == 0: print num ,'---' ,sum
root@planpls:/var/python/web2py# python seventeen.py
please input x : 23
1 --- 1
3 --- 6
5 --- 15
7 --- 28
9 --- 45
11 --- 66
13 --- 91
15 --- 120
17 --- 153
19 --- 190
21 --- 231
不要浅尝辄止要认真深入