logo

Python Number Choice() Method


Show

Description

The choice() method of the Python Random module is an inbuilt function in the Python programming language that is known to return a random element from a tuple, list, string, or any non-empty sequence. One can make use of the random.choice() function in order to select a random password from the word-list, while selecting a random element from the data available.

Syntax

Here is the syntax for choice() method -

choice( seq )

Note - This function is not accessible directly, so we need to import a random module, and then we need to call this function using the 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.

#!/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 the above program, it produces the following result:

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

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.