Python模块

Python模块是包含Python定义和语句的文件。模块可以定义函数、类和变量。模块还可以包含可运行代码。将相关代码分组到一个模块中可以使代码更容易理解和使用。它还使代码具有逻辑组织性。

null

示例:创建一个简单的模块

Python3

# A simple module, calc.py
def add(x, y):
return (x + y)
def subtract(x, y):
return (x - y)


Python中的导入模块–导入语句

我们可以使用 进口声明 在其他一些Python源文件中。

语法:

import module

当解释器遇到import语句时,如果模块出现在搜索路径中,解释器将导入该模块。搜索路径是解释器为导入模块而搜索的目录列表。例如,要导入模块calc.py,我们需要在脚本顶部放置以下命令。

注: 这不会直接导入函数或类,而是只导入模块。要访问模块内部的功能,请点(.)使用运算符。

示例:在Python中导入模块

Python3

# importing  module calc.py
import calc
print (calc.add( 10 , 2 ))


输出:

12

这是进口货 陈述

Python的 从…起 语句允许您从模块导入特定属性,而无需将模块作为一个整体导入。

示例:从模块导入特定属性

Python3

# importing sqrt() and factorial from the
# module math
from math import sqrt, factorial
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print (sqrt( 16 ))
print (factorial( 6 ))


输出:

4.0720

导入所有名称–来自导入*语句

from import语句使用的*符号用于将模块中的所有名称导入到当前名称空间。

语法:

from module_name import *

使用*有其优点和缺点。如果您确切地知道您将需要从模块中获得什么,则不建议使用*,否则请使用*。

示例:导入所有名称

Python3

# importing sqrt() and factorial from the
# module math
from math import *
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print (sqrt( 16 ))
print (factorial( 6 ))


输出

4.0720

定位模块

无论何时在Python中导入模块,解释器都会查找多个位置。首先,它将检查内置模块,如果找不到,则会查找在 系统。路径 .Python解释器以以下方式搜索模块——

  • 首先,它在当前目录中搜索模块。
  • 如果在当前目录中找不到该模块,Python就会搜索shell变量中的每个目录 Python .PYTHONPATH是一个环境变量,由目录列表组成。
  • 如果同样失败,python会检查安装python时配置的目录的安装依赖列表。

示例:模块的目录列表

Python3

# importing sys module
import sys
# importing sys.path
print (sys.path)


输出:

[‘/home/nikhil/Desktop/gfg’,’/usr/lib/python38.zip’,’/usr/lib/python3.8’,’/usr/lib/python3.8/lib dynload’,“,’/home/nikhil/.local/lib/python3.8/site packages’,’/usr/local/lib/python3.8/dist packages’,’/usr/lib/python3/dist/dist packages’,’/usr/local/local/lib/python3/python3/dist/IPython/.8/IPython’]

导入和重命名模块

我们可以在导入模块时使用as关键字重命名模块。

示例:重命名模块

Python3

# importing sqrt() and factorial from the
# module math
import math as gfg
# if we simply do "import math", then
# math.sqrt(16) and math.factorial()
# are required.
print (gfg.sqrt( 16 ))
print (gfg.factorial( 6 ))


输出

4.0720

dir()函数

内置函数dir()返回包含模块定义的名称的字符串排序列表。该列表包含模块中定义的所有模块、变量和函数的名称。

Python3

#  Import built-in module  random
import random
print ( dir (random))


输出:

“BPF”、“LOG4”、“LOG4”、“LOG4”、“NV(U)和(U)和(U)和(U)和(BPF”、“随机”、“SG(U)和“系统随机”、“系统随机”、“两个PI”、“两个PI”、“两个PI”、“建交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交交对分,”cos”,”e”,”exp”,”inst”,”itertools”,”log”,”pi”,”随机”,”sha512″sin””,”sqrt””测试””生成器””””,”uradom””,“paretovariate”、“randint”、“random”、“randrange”、“sample”、“seed”、“setstate”、“shuffle”、“Triangal”、“uniform”、“vonmisesvariate”、“weibullvariate”]

演示python内置模块的代码片段:

Python3

# importing built-in module math
import math
# using square root(sqrt) function contained
# in math module
print (math.sqrt( 25 ))
# using pi function contained in math module
print (math.pi)
# 2 radians = 114.59 degrees
print (math.degrees( 2 ))
# 60 degrees = 1.04 radians
print (math.radians( 60 ))
# Sine of 2 radians
print (math.sin( 2 ))
# Cosine of 0.5 radians
print (math.cos( 0.5 ))
# Tangent of 0.23 radians
print (math.tan( 0.23 ))
# 1 * 2 * 3 * 4 = 24
print (math.factorial( 4 ))
# importing built in module random
import random
# printing random integer between 0 and 5
print (random.randint( 0 , 5 ))
# print random floating point number between 0 and 1
print (random.random())
# random number between 0 and 100
print (random.random() * 100 )
List = [ 1 , 4 , True , 800 , "python" , 27 , "hello" ]
# using choice function in random module for choosing
# a random element from a set such as a list
print (random.choice( List ))
# importing built in module datetime
import datetime
from datetime import date
import time
# Returns the number of seconds since the
# Unix Epoch, January 1st 1970
print (time.time())
# Converts a number of seconds to a date object
print (date.fromtimestamp( 454554 ))


输出:

5.03.14159265359114.5915590261.04719755120.9092974268260.877582561890.2341433623512430.40153317295188.4917616788True1461425771.871970-01-06

本文由 高拉夫·什里斯塔 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论。如果你喜欢GeekSforgeks,并且想贡献自己的力量,你也可以写一篇文章,然后把你的文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享