Python Language / Python Modules

It is a file consists of set of functions (code) which you can use it in your application. Create a Module done by saving code in a python file with the file extension .py.

Sample Programs
Program Output

#Save file name with  ourmodule.py
def wishes(name):
print("Welcome to python module concept: " + name)


#Module Creation and usage
import ourmodule
ourmodule. wishes ("Wisdommaterials")

Welcome to python module concept: Wisdommaterials

#Variable usage of a module is ourmodule.py

Fruit = {
  "Name": "Apple",
  "Price": "20",
}

//Importing ourmodule in pmu.py
import ourmodule
v1 = ourmodule.Fruit["Name"]
v2 = ourmodule.Fruit["Price"]
print(v1)
print(v2)

Apple
20


Alias / Naming a Module can be created by using the “as” keyword on module.
Program for Module Creation and usage Output

#Save file name with  ourmodule.py

def wishes(name):
print("Welcome to python module concept: " + name)


#Save file name with  ourothermodule.py

import ourmodule as om
om.wishes ("Wisdommaterials")

Welcome to python module concept: Wisdommaterials


Built-in Modules

Modules Name Example Output
datetime
import datetime
dt = datetime.datetime.now()
print(dt)
print(dt.year)
print(dt.strftime("%A"))
print(x.strftime("%B"))

2020-01-31 12:13:22.214961
2020
Friday
January

Date Objects Creation
import datetime
dt = datetime.datetime(2020, 1, 31)
print(dt)
print(dt.year)
print(dt.strftime("%A"))


2020-01-31 00:00:00
2020
Friday

platform
import platform
pf= platform.system()
print(pf)

Windows


Home     Back