Python中random模块用法实例分析
2015-05-19 00:00
1311 查看
本文实例讲述了Python中random模块用法。分享给大家供大家参考。具体如下:
运行结果如下:
(2, 'cherry')
不管学哪个语言,我总喜欢弄个随机数玩玩。农历十一月初六,Let's Python!!!
运行结果如下:
5050
运行结果如下:
运行结果如下:
希望本文所述对大家的Python程序设计有所帮助。
import random x = random.randint(1,4); y = random.choice(['appale','banana','cherry','durian']); print(x,y);
运行结果如下:
(2, 'cherry')
不管学哪个语言,我总喜欢弄个随机数玩玩。农历十一月初六,Let's Python!!!
l=[ ] while True: name=input("请输入一个名字:") if name!='': l.append(name) else: break print(l);
'''求1到100直接所有整数的和''' x=range(1,101,1) sumi=0 for i in x: sumi+=i print(sumi)
运行结果如下:
5050
u1='''求阶乘的一般方法''' print(u1) def jc(n): s=1 for i in range(1,n+1): s*=i return s n = input("n! usage:<number>:") h = jc(int(n)) print(h)
运行结果如下:
求阶乘的一般方法 n! usage:<number>:5 120
u2='''通过递归实现阶乘''' print(u2) def jch(n): if(n==1): return 1 else: return n*jch(n-1) un=input("input n:") s=jch(int(un)) print("n! is ",s)
运行结果如下:
通过递归实现阶乘 input n:5 ('n! is ', 120)
希望本文所述对大家的Python程序设计有所帮助。
相关文章推荐
- Python中random模块用法实例分析
- Python随机数用法实例详解【基于random模块】
- Python unittest模块用法实例分析
- python的keyword模块用法实例分析
- python开发中module模块用法实例分析
- Python中threading模块join函数用法实例分析
- Python内置模块logging用法实例分析
- python的keyword模块用法实例分析
- Python hashlib模块用法实例分析
- Python pickle模块用法实例分析
- Python pickle模块用法实例分析
- python开发中module模块用法实例分析
- python的keyword模块用法实例分析
- Python动态导入模块的方法实例分析
- python中sys.argv参数用法实例分析
- Python过滤列表用法实例分析
- Python常用模块用法分析
- Python中logging模块的用法实例
- python下MySQLdb用法实例分析
- Python中XlsxWriter模块简介与用法分析