ExampleEdit & Run

Use the random.choice() function

#import the random module
import random

#The list to choose from
L = ['Python', 'C++', 'Swift', 'Ruby', 'PHP', 'C#']

#Choose a random value from the list
print(random.choice(L))
copy
Output:
PHP [Finished in 0.012604324147105217s]

The choice() function in the random  module is used to randomly select an item from a sequence such as a list, tuple, string, range e.t.c.

Syntax:
choice(seq)
copy
seq Required. The sequence containing the elements to choose from. 
Parameters:

The function randomly picks an element from the sequence and returns it. Please note that sets and dictionaries are not sequences since the items are unordered, they, therefore, cannot be used as arguments to the choice() function, doing so will raise a TypeError.

ExampleEdit & Run

Use the choice function to get a random integer in a range

#import the random module
import random

#Select an inteher from a range
print(random.choice(range(20)))
copy
Output:
14 [Finished in 0.01258646510541439s]
ExampleEdit & Run

Get a random element from a list

#import the random module
import random

#The list to choose from
L = ['Python', 'C++', 'Swift', 'Ruby', 'PHP', 'C#']

#Choose a random value from the list
print(random.choice(L))
copy
Output:
C++ [Finished in 0.012044440023601055s]
ExampleEdit & Run

Get a random item from a tuple

#import the random module
import random

#The list to choose from
my_tuple = ((1, 2, 3), (4, 5, 6), (7, 8, 9))

#Choose a random value from the list
print(random.choice(my_tuple))
copy
Output:
(4, 5, 6) [Finished in 0.012178225442767143s]