Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python Number choice() Function


previous next AddThis Social Bookmark Button


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:

  • seq: This could be a list, tuple, or string..

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 Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names