www.rtmj.net > python 1乘到100

python 1乘到100

num = 1 for i in range(1,100): num *= i s = str(num).count("0") print s结果:28

##第一种方法 a = 0 for i in range(0,100): a += (i+1); print a##第二种方法 sum(range(1,101))##第三种方法 sum([ x for x in range(0,101)])

一句话搞定,里面用到了2个函数,range是给出1到100,sum是求和.sum(range(1,101))

i = 1sum = 0while i <= 100: sum += sum + i i += i +1 print(sum + ", ", end="") 需注意两点一、输出sum python2用 :print sum Python3用:print(sum) 二、脚本运行环境的不同会有一些差别 例如在Python自带的idle 中运行 和在cmd命令下运行 有细微差别

val = list(range(1,101))print(sum(val))

其实我是看不懂你题目什么意思,而不是抄代码怎么写,1-100所有末尾数乘积不是0吗,然后小于本身,是不是你题目写错了是不是1-100所有数的各个位数的乘积小于本身zhidao的数,如果是按照我理解的代码就这样写num_lis = [ num for num

if后面的c=True缩进不对另外,这里应该写break或c=False,不然循环跳出不了

for i in range (1,101): print i, if i%3==0 and i%5==0: print 'A*B', elif i%3==0: print 'A', elif i%5==0: print 'B', print

result=[] for i in range(2,101): for j in range(2,i): if i%j==0 break else: result.append(i) print(result)

def factorial(n):if n <= 1:return 1else:return n * factorial(n - 1)the_range = range(1, 100)the_list = [factorial(a) for a in the_range]sum = reduce(lambda x,y: x+y, the_list)print sum

网站地图

All rights reserved Powered by www.rtmj.net

copyright ©right 2010-2021。
www.rtmj.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com