例11:打印出所有的”水仙花数”,所谓”水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个”水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
#!/usr/bin/python3 # -*- coding: UTF-8 -*- for n in range(100,1000): i = int(n / 100) j = int(n / 10) % 10 k = n % 10 if n == i ** 3 + j ** 3 + k ** 3: print(n,end=" ")输出结果:
153 370 371 407例12:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
#!/usr/bin/python3 # -*- coding: UTF-8 -*- def reduceNum(n): print ("{0} = ".format(n),end='') if not isinstance(n, int) or n <= 0 : print ('请输入一个正确的数字 !') exit(0) elif n in [1] : print ('{}'.format(n)) while n not in [1] : # 循环保证递归 for index in range(2, n + 1) : if n % index == 0: n = int(n/index) # n 等于 n/index if n == 1: print(index) else : # index 一定是素数 print ('{} *'.format(index),end=" ") break reduceNum(1100)输出结果:
1100 = 2 * 2 * 5 * 5 * 11例13:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
#!/usr/bin/python3 # -*- coding: UTF-8 -*- score = int(input('请输入分数')) if score >= 90: grade = 'A' elif score >= 60: grade = 'B' else : grade = 'C' print('%d属于%s'%(score,grade))输出结果:
请输入分数99 99属于A例14:输出指定格式的日期。
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import datetime # 输出今日日期 print(datetime.date.today().strftime('%Y-%m-%d')) #创建日期对象 mkdateobj = datetime.date(2018,7,7) print(mkdateobj.strftime('%m/%d %Y')) # 日期算术运算 miyazakiBirthNextDay = mkdateobj + datetime.timedelta(days=1) print(miyazakiBirthNextDay.strftime('%d/%m/%Y'))输出结果:
请输入分数99 99属于A例15:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import string s = input('请输入一个字符串:') i = 0 letter = 0 space = 0 digit = 0 other = 0 while i < len(s): c = s[i] i+=1 if c.isalpha(): letter+=1 elif c.isspace(): space+=1 elif c.isdigit(): digit+=1 else: other+=1 print("有%d个字母,有%d个空格,有%d个数字,有%d个其他字符"%(letter,space,digit,other))输出结果:
请输入一个字符串:我在杭州|iam in hangzhou 3 years ! 有22个字母,有5个空格,有1个数字,有2个其他字符例16:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
reduce和lambda使用说明
#!/usr/bin/python3 # -*- coding: UTF-8 -*- from functools import reduce s = 0 arr = [] n = int(input('n:')) a = int(input('a:')) for i in range(n): s = s + a a = a*10 arr.append(s) print(s) arr = reduce(lambda x,y:x+y,arr) print('和是:%d'%arr)输出结果:
n:6 a:3 3 33 333 3333 33333 333333 和是:370368例17:一个数如果恰好等于它的因子之和,这个数就称为”完数”。例如6=1+2+3.编程找出1000以内的所有完数。
#!/usr/bin/python3 # -*- coding: UTF-8 -*- from sys import stdout for j in range(2,1001): k = [] n = -1 s = j for i in range(1,j): if j % i == 0: n += 1 s -= i k.append(i) if s == 0: print(j) for i in range(n): stdout.write(str(k[i])) stdout.write(' ') print(k[n])输出结果:
6 1 2 3 28 1 2 4 7 14 496 1 2 4 8 16 31 62 124 248例18:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
#!/usr/bin/python3 # -*- coding: UTF-8 -*- total = [] height = [] hei = 100.0 tim = 10 for i in range(1,tim+1): if i == 1: total.append(i) else: total.append(2*hei) hei = hei / 2 height.append(hei) print('总高度:total={0}'.format(sum(total))) print('第10次反弹高度: height={0}'.format(height[-1]))输出结果:
总高度:total=200.609375 第10次反弹高度: height=0.09765625例19:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少
#!/usr/bin/python3 # -*- coding: UTF-8 -*- t = 1 for i in range(9,0,-1): x = (t+1)*2 t =x print(t)输出结果:
1534例20: 把字符串”我在$$杭州工作%%,现在@没事学&*Python!” 中的特殊符号替换成空格,替换后的字符串为:”我在 杭州工作 ,现在 没事学 Python “
#!/usr/bin/python3 # -*- coding: UTF-8 -*- import re str = "我在$$杭州工作%%,现在@没事学&*Python!" str1 = re.sub('[$%@&*!]+',' ',str) print(str1)输出结果:
我在 杭州工作 ,现在 没事学 Python