内容简介:choice() 方法返回一个列表,元组或字符串的随机项。
描述
choice() 方法返回一个列表,元组或字符串的随机项。
语法
以下是 choice() 方法的语法:
import random random.choice( seq )
注意:choice()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。
参数
- seq -- 可以是一个列表,元组或字符串。
返回值
返回随机项。
实例
以下展示了使用 choice() 方法的实例:
#!/usr/bin/python import random print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) print "choice('A String') : ", random.choice('A String')
以上实例运行后输出结果为:
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n
以上所述就是小编给大家介绍的《Python choice() 函数》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Python 拓展之特殊函数(lambda 函数,map 函数,filter 函数,reduce 函数)
- Python 函数调用&定义函数&函数参数
- python基础教程:函数,函数,函数,重要的事说三遍
- C++函数中那些不可以被声明为虚函数的函数
- 017.Python函数匿名函数
- 纯函数:函数式编程入门
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Development Recipes
Brian P. Hogan、Chris Warren、Mike Weber、Chris Johnson、Aaron Godin / Pragmatic Bookshelf / 2012-1-22 / USD 35.00
You'll see a full spectrum of cutting-edge web development techniques, from UI and eye candy recipes to solutions for data analysis, testing, and web hosting. Make buttons and content stand out with s......一起来看看 《Web Development Recipes》 这本书的介绍吧!