Python 3 - Number choice() Method


Advertisements

Description

The choice() method 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 the 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 the choice() method.

#!/usr/bin/python3
import random

print ("returns a random number from range(100) : ",random.choice(range(100)))
print ("returns random element from list [1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]))
print ("returns random character from string 'Hello World' : ", random.choice('Hello World'))

Output

When we run the above program, it produces a result similar to the following −

returns a random number from range(100) :  19
returns random element from list [1, 2, 3, 5, 9]) :  9
returns random character from string 'Hello World' :  r
python_numbers.htm
Advertisements