Python choice () function
description
choice () method returns a random list of items, tuple or string.
grammar
The following is the syntax choice () method:
import random random.choice( seq )
Note: choice () is not directly accessible, you need to import the random module, and then call the method by random static objects.
parameter
- seq - may be a list, tuple or string.
return value
Returns a random item.Examples
The following shows an example of the use of 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')
After running the above example output is:
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n