Functions defined in modules using Python libraries:
- A Module is a file containing Python functions, variables, classes, and statements (.py file called modules).
- The package is nothing but, it is a collection of modules stored in one folder called a package or library.
Importing Python modules:
To import entire module:
Syntax:
import <module name>
Example:
import math
To import specific function from module:
Syntax:
from <module_name> import <function_name>
Example:
from math import sqrt
Another way of importing entire module:
Syntax:
from <module_name> import *
Example:
from math import *
Accessing function:
Syntax:
<module_name>.<function_name>
Example:
print(math.sqrt(5))
Example program using import:
import math
print("Squre root of 4 value:",math.sqrt(4))
print("100 log10 value:",math.log10(100))
print("sin 90 value:",math.sin(90))
print("8 power of 3 value:",math.pow(8,3))
Output:
Example program uisng from module import*:
from random import*
print("Range from 1 to 6 of random value:",randrange(1,6))
print ("List 1, 7, 3, 10, 4 of random value:",choice([1, 7, 3, 10, 4]))
Output:
No comments:
Post a Comment