Python中random模块用法实例分析

yipeiwu_com6年前Python基础

本文实例讲述了Python中random模块用法。分享给大家供大家参考。具体如下:

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使用thrift教程的方法示例

python使用thrift教程的方法示例

一、前言:   Thrift 是一种接口描述语言和二进制通信协议。以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本...

python实现根据图标提取分类应用程序实例

本文实例讲述了python实现根据图标提取分类应用程序,分享给大家供大家参考。 具体方法如下: #!/usr/bin/python # -*- coding: utf-8 -*-...

python中比较两个列表的实例方法

cmp() 方法用于比较两个列表的元素。 cmp()方法语法: cmp(list1, list2) 参数: list1 -- 比较的列表。list2 -- 比较的列表。 返回值:...

一条命令解决mac版本python IDLE不能输入中文问题

安装完Python通常自动就有了一个简易的集成环境IDLE,但在mac上,无法在IDLE中使用中文。 通常故障有两种情况: 1.在IDLE中,中文输入法根本无法工作,不会弹出输入框,所有...

python strip()函数 介绍

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除...