Copyright © tutorialspoint.com

Python Number choice() Function

previous next


Description:

This function returns a random item from a list, tuple, or string.

Syntax:

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:

Here is the detail of parameters:

Return Value:

A random item

Example:

#!/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')

This will produce following result:

choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n

previous next

Copyright © tutorialspoint.com