Easy Tutorial
❮ Python Func Hex Python Add Number ❯

Python3 choice() Function

Python3 Numbers


Description

The choice() method returns a random item from a list, tuple, or string.


Syntax

Here is the syntax for the choice() method:

import random

random.choice(seq)

Note: choice() cannot be accessed directly; it requires importing the random module and then calling this method via the random static object.


Parameters


Return Value


Example

Below are examples demonstrating the use of the choice() method:

#!/usr/bin/python3
import random

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

The above example will output something similar to:

A random number from range(100): 68
A random element from list [1, 2, 3, 5, 9]: 2
A random character from string 'tutorialpro': u

Python3 Numbers

❮ Python Func Hex Python Add Number ❯