How to randomly select an item from a tuple in Python?

PythonServer Side ProgrammingProgramming

The choice() function is useful for this purpose. It returns on item randomly selected from any sequence object (tuple in this case)

>>> import random
>>> t1=(11,'aa',12.50,77,'xyz')
>>> item=random.choice(t1)
>>> item
11
>>> item=random.choice(t1)
>>> item
'xyz'
raja
Published on 05-Mar-2018 07:05:04
Advertisements