Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python choice() function

Python choice() function

Python Numbers Python Numbers


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() cannot be accessed directly; you need to import the random module and call the method through the static random object.


Parameters


Return Value

Returns a random item.


Example

The following shows examples of using 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')

Running the example above produces:



choice([1, 2, 3, 5, 9]) :  2

choice('A String') :  n

Python Numbers Python Numbers