Important Notice:

Course Content
The Calendar Module
Library Function/Predefined Functions
0/4
Sys Module
Library Function/Predefined Functions
0/2
OS Module
Library Function/Predefined Functions
0/2
Functions in Python (Chapter-10) M3-R5
About Lesson

choice() Method

               The choice() method returns a randomly selected element from the specified sequence.

             The sequence can be a string, a range, a list, a tuple or any other kind of sequence.

Syntax

random.choice(sequence)

Parameter Values

Parameter

Description

sequence

Required. A sequence like a list, a tuple, a range of numbers etc.

Example

Return a random element from a list:

import random
myl = [“apple”, “banana”, “cherry”]
print(random.choice(myl))

Output:

Apple

Example

Return a random character from a string:

import random
x = “WELCOME”
print(random.choice(x))

Output:

C

error: Content is protected !!