How the import random library work . Python programming

 Today we will learn about the 3 useful features of the random library 

First we need to import random 

Now let's look at the first features 

Choosing a random integer

Example :-


import random
number = random.randint(0,100)
print(number)
We could improve it a little bit 

Example :-


import random
max_number = int(input("choose a # between 0 and..."))
number= random.randint(0,max_number)
print(number)
Moving on , let's look at randomly choosing o. Item 

Example :-


import random
basket = ("apple","banana","mango","orange","grapes")
chosen_fruit = random.choice(basket)
print(chosen_fruit)
lastly, let's look at the shuffle option . We will keep the basket 

Example :- 


import random
basket = ["apple","mango","banana","grapes"]
print(basket)
print("Now shuffling the order of the basket...")
random.shuffle(basket)
print(basket)

Post a Comment

0 Comments