xxxxxxxxxx
#import the choice function from the random module
from random import choice
#the list to select from
my_list = ['Python', 'Java', 'C++', 'Ruby']
#get the random elemet
random_element = choice(my_list)
print(random_element)
xxxxxxxxxx
import random
#1.A single element
random.choice(list)
#2.Multiple elements with replacement
random.choices(list, k = 4)
#3.Multiple elements without replacement
random.sample(list, 4)
xxxxxxxxxx
import random
# with replacement = same item CAN be chosen more than once.
# without replacement = same item CANNOT be chosen more then once.
# Randomly select 2 elements from list without replacement and return a list
random.sample(list_name, 2)
# Randomly select 3 elements from list with replacement and return a list
random.choices(set_name, k=3)
# Returns 1 random element from list
random.choice(list_name)
xxxxxxxxxx
#import the random module
import random
my_list = ['apple', 'banana', 'orange', 'strawberry', 'mango']
#pick a random item from the list
print(random.choice(my_list))
xxxxxxxxxx
import random
# there are 2 ways for this
listofnum = [1, 2, 3, 4, 5]
# 1
print(random.choice(listofnum))
# 2
random.shuffle(listofnum)
print(listofnum)
xxxxxxxxxx
import random
my_list = [1, 2, 3, 4, 5]
random_element = random.choice(my_list)
print(random_element)
xxxxxxxxxx
import random
rand_index = random.randrange(len(list))
random_num = list[rand_index]
random_num = random.choice(list)
xxxxxxxxxx
#import the random module
import random
my_list = ['apple', 'banana', 'orange', 'strawberry', 'mango']
#pick a random item from the list
print(random.choice(my_list))
xxxxxxxxxx
#select multiple random items from a list
import random
#ten students
students = ['Dev', 'Andy', 'Cindy', 'Beth', 'Ester','Rahul', 'Peter', 'Nancy', 'Mark', 'Shiv']
#select 3 random students
lucky_three = random.sample(students, k=3)
print("The three randomly picked students are: ", lucky_three)