Python Number choice() Method


Advertisements

Description

Python number 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.

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

Useful Video Courses


Video

Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar

Video

Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty

Video

Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official

Video

Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions

Video

Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson

Advertisements