Introduction
“The best way to predict the future is to create it.”
This article will discuss a few built-in modules in python, mainly we will focus on the random module in python.
Built-in modules
Modules are files containing a set of functions you want to include in your application just by importing them.
Python offers a wide range of modules that can be used effortlessly by importing them.
Import statement
Whenever we need to use a module, we need to import them. Import statements are basically written in many ways let us see them one by one.
Method 1
The simplest form to import a module is
import module_name
If we write the import in the above method we need to call the functions inside the module with module reference.
For example,
import random
random.randint(1,50)#returns a random number between 1 to 50.
Method 2
import module_name as alternate_name
Here, we are importing a module and naming it with a short name so that we can use it whenever necessary.
For Example,
import random as r
r.randint(1,50)#returns a random number between 1 to 50.
Method 3
from module_name import *
This statement will import all the functions available inside the module.
We no need to write the module name every time for using the functions.
For example,
from random import *
randint(1,50)#returns a random number between 1 to 50.
Method 4
from module_name import function_name
Here, we are importing only the function which we are importing all the functions will not be imported.
For example,
from random import randint
randint(1,50)#returns a random number between 1 to 50.
random()#It throws an error.
Random Module
The random module allows us to generate random numbers or random choices.
Frequently used functions inside the random module are,
Function name | Description | Example |
random() | Returns a random floating number between 0.0 to 1.0 | print(random())#0.6 |
randint(a,b) | Returns a random integer between the range a,b | print(randint(1,50))#25 |
randrange(a,b,c) | Returns a random number between the range a,b and optional step count c. | print(randrange(1,50,2))#29 |
choice(list_name) | Returns a random item in a list. | print(choice([“a”,”b”,”c”])#c |
shuffle(list_name) | Returns the shuffled list | print(shuffle([“a”,”b”,”c”]) |
uniform(a,b) | Returns a random float number inclusive of the range. | print(uniform(1,50))#12.25354 |
Example
from random import *
print(random())
print(randint(1,50))
print(randrange(1,50,2))
x=["a","b","c"]
print(choice(x))
shuffle(x)
print(x)
print(uniform(1,50))
Output
0.9905010751757418
40
17
c
['b', 'c', 'a']
13.82524351700107
Practice Yourself
- Write a program to make the user guess a number between 1-10 and if the guessed number is the same as the random number then the user has won the game. We can give the user 3 chances for guessing the number.
Conclusion
Thus we have understood methods to import the built-in module and have learned in detail regarding the random module in python.
Pingback: Guess the Number Game -
Pingback: Rock Paper Scissor Game in python -