Python Number choice() Method
Advertisements
Description
The method choice() returns a random item from a list, tuple, or string.
Syntax
Following is the syntax for choice() method −
choice( seq )
Note − This function is not accessible directly, so we need to import random module and then we need to call this function using random static object.
Parameters
seq − This could be a list, tuple, or string.
Return Value
This method returns a random item.
Example
The following example shows the usage of choice() method.
Live Demo#!/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')
When we run above program, it produces following result −
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n
python_numbers.htm
Advertisements