使用random模块:
rand = ['0', '1', '2', '3', '4'] print(choice(rand))
其中还有几个方法:
random.randint(a, b)
这是返回从整数a到b之间的任意一个整数。
random.uniform(a, b)
这是返回浮点数a和b之间的任意浮点数。
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] random.sample(list, 5) print (slice)这是以list形式返回list中的任意五个无序的元素