#import the random module import random #The sequence to sample my_seq = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #get a sample of 4 elements print(random.sample(my_seq, 4))# Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3)) random.sample(population, k, *, counts=None) Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.# Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3)) #random.sample takes a list and randomly selects k items from it new_list = random.sample(list, k) #for example: nums = [1, 2, 3, 4, 5] sample_nums = random.sample(nums, 3) print(sample_nums) # 2, 5, 1# Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3)) # Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3))# Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3)) # Python3 program to demonstrate # the use of sample() function . # import random import random # Prints list of random items of # length 3 from the given list. list1 = [1, 2, 3, 4, 5, 6] print("With list:", random.sample(list1, 3)) # Prints list of random items of # length 4 from the given string. string = "GeeksforGeeks" print("With string:", random.sample(string, 4)) # Prints list of random items of # length 4 from the given tuple. tuple1 = ("ankit", "geeks", "computer", "science", "portal", "scientist", "btech") print("With tuple:", random.sample(tuple1, 4)) # Prints list of random items of # length 3 from the given set. set1 = {"a", "b", "c", "d", "e"} print("With set:", random.sample(set1, 3))