Want to award this author with Pubby Cash? You must log in first!

How to Use the Random Method in Python

Categories: Tech | Pubby Cash Received:  0 | Click to Award

The random method in Python is a very useful tool in Python because it has the power to select a random value. In this article, I will try my best to explain how to use the random method. The first thing that you need to know is that you have to import random first to access it. import random I will now provide an example of how to use the random method. Lets say that we want our program to generate a random number from one through five. This is how we can use the random method to do it. import random numbers = [1, 2, 3, 4, 5] print(random.choice(numbers)) This code is saying that numbers is equal to 1, 2, 3, 4, and 5. We are then printing a random number that is in the numbers variable. The output of this program could be 1, 2, 3, 4, or 5. This is because we are choosing a random number. After reading this article, you should have a better understanding about how to use the random method.


Published from: Pennsylvania US
Liked by: H2O, Andy Tang 

Daily Deals


MECHANICSBURG WEATHER