Python choice() Function
Description
choice()The method returns a random item from a list, tuple, or string.
Syntax
The syntax of the choice() method is as follows:
import random random.choice( seq )
Note:choice() is not directly accessible. You need to import the random module, and then call the method via the random static object.
Parameters
- seq -- can be a list, tuple, or string.
Return Value
Returns a random item.Example
The following example demonstrates the use of the choice() method:
#!/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')
The output of the above example after running is:
choice([1, 2, 3, 5, 9]) : 2
choice('A String') : n
Other Extensions
Python Numbers