Featured
- Get link
- X
- Other Apps
Python modules
Before seeing some example about file Operators lets see what module is.....
Simply module is a file consist of python code. A module may have function, classes and variable. A module can also include runable code. Fuctions are group of code and modules are group of function.
There are two types of modules:
1) user defined
2) built-in
To create a module
1) create a file with function and variable then save it as .py extension
2) run the program and check it as no error
3) now create a new python file and import the previous file by using
Import filename
4) We can also access specific function from a module which can reduce the load speed. Use
From filename import function
result=a+b
print ("additional of two number is",result)
def sub(a,b):
result=a+b
print("subtraction of two number is",result)
def mul(a,b):
result=a*b
print("product of two number is", result)
Save the above program as module.py
Now create a new file as new.py and type
module.add(5,7)
module.sub(6,3)
module.mul(4,5)
Using from:(here you can only use add function)
module.add(7,3)
Built-in :
Also like function there are many module in python which are used for specific purpose. Some main modules are
1) random
2) math
3) date and calendar
Random:
This module generates the random number and variable from list or sequence. Use randint() to print random integer number number. randint() consist of two parameters starting and ending value. We can also generate floating point numbers by using random() function. To generate elements in list we use choice() function. Lets see some example
print(random.randint(0,5))
print(random.random())
list=["hello","hi","welcome","thankyou"]
print(random.choice(list))
Output:
Math:
Using this module we can access for mathematical values like pi, factorials etc
print(math.pi)
print(math.degrees(2))
print(math.radians(60))
Print(math.sin(3))
print(math.cos(2))
print(math.tan(0))
print(math.factorial(5))
print(math.sqrt(45))
Output:
114.59155902616465
1.0471975511965976
0.1411200080598672
-0.4161468365471424
0.0
120
6.708203932499369
Calendar and date-time:
By using python calendar and date-time module we can track date from a year and time including current time
cal = calendar.month(2021,5)
val = calendar.month(2021,6)
print(cal)
print(val)
Output:
local = time.localtime(time.time())
print(local)
Output:
Try the code with different variable and values to make it more clear and understandable . feel free to comment!!!!!
Book for python: https://amzn.to/3pCgIqy
ebook: https://amzn.to/3pHioiO
- Get link
- X
- Other Apps





Comments
Post a Comment