Create a list named nums and initialize it with at least 3 numbers [1,2,3]. Create a list named
hobbies, initialize it with at least 3 of your hobbies, for example ['Playing Cricket', 'Reading
Books', 'Cooking']. Iterate over the list nums and print hobbies for each list element, so that
the output looks like this:
import random
# Method 1: List Comprehension Random Float List [0, 1]
randlist1 = [random.random() for _ in range(10)]
# Method 2: List Comprehension Random Int List [0, 999]
randlist2 = [random.randint(0, 999) for _ in range(10)]
# Method 3: For Loop Random Int List [0, 99]
randlist3 = []
for _ in range(10):
randlist3.append(random.randint(0, 99))
# Method 4: random.sample()
# Generate 10 random numbers between 0 and 20 (included)
randlist4 = random.sample(range(20), 10)
print(randlist1)
print(randlist2)
print(randlist3)
print(randlist4)
Comments
Leave a comment