choice( seq )
註:此函式無法直接存取,所以我們需要匯入 random 模組,然後用數學靜態物件呼叫這個函式。
seq -- 這可以是一個列表,元組或字串...
#!/usr/bin/python3 import random print ("returns a random number from range(100) : ",random.choice(range(100))) print ("returns random element from list [1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])) print ("returns random character from string 'Hello World' : ", random.choice('Hello World'))
returns a random number from range(100) : 19 returns random element from list [1, 2, 3, 5, 9]) : 9 returns random character from string 'Hello World' : r