小白教程
所有教程
关于
Search
172.69.6.175
172.69.6.175
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 func number choice”的源代码
本页内容
上一节:
Ref_random_randint
下一节:
Python3_func_number_shuffle
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:choice(seq)}}[[Category:python random|4]] = Python3 choice() 函数 = [[python3 number|Python3 数字]] == 描述 == '''choice()''' 方法返回一个列表,元组或字符串的随机项。 == 语法 == 以下是 choice() 方法的语法: <sample title="" desc="" lang="python" hererun="1"> import random random.choice( seq ) </sample> '''注意:'''choice()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。 == 参数 == * seq -- 可以是一个列表,元组或字符串。 == 返回值 == 返回随机项。 以下展示了使用 choice() 方法的实例: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 import random print ("从 range(100) 返回一个随机数 : ",random.choice(range(100))) print ("从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : ", random.choice([1, 2, 3, 5, 9])) print ("从字符串中 'xiaoBai' 返回一个随机字符 : ", random.choice('xiaoBai')) </sample> 以上实例运行后输出结果为: <sample title="" desc="" lang="python" hererun="1"> 从 range(100) 返回一个随机数 : 68 从列表中 [1, 2, 3, 5, 9]) 返回一个随机元素 : 2 从字符串中 'xiaoBai' 返回一个随机字符 : u </sample> [[python3 number|Python3 数字]]
返回至“
Python3 func number choice
”。
上一节:
Ref_random_randint
下一节:
Python3_func_number_shuffle