Python - Libraries and Builtin Functions

Libraries and builtin functions in Python

random module

Python has many inbuilt packages and modules. One of the most useful modules is random. This module helps in generating random numbers.

The code given below generates a random number between x and y-1 (both inclusive) using the randrange function of the random module.

Try out  the below code and observe the output. 

	import random
	x=10
	y=50
	print(random.randrange(x,y))

Sample output:

33

46

math module

math is another useful module in Python. Once you have imported the math module, you can use some of the below functions:

Function

Explanation

math.ceil(x)

Smallest integer greater than or equal to x

math.floor(x)

Largest integer smaller than or equal to x

math.factorial(x)

Factorial of x

math.fabs(x)

Gives absolute value of x

Try out  the below code and observe the output. 

	import math
	num1=234.01
	num2=6
	num3=-27.01

	print("The smallest integer greater than or equal to num1,",num1,":",math.ceil(num1))
	print("The largest integer smaller than or equal to num1,",num1,":",math.floor(num1))
	print("The factorial of num2,",num2,":", math.factorial(num2))
	print("The absolute value of num3",num3,":",math.fabs(num3))