少说话多写代码之Python学习021——导入模块

栏目: Python · 发布时间: 7年前

内容简介:从其他模块导入函数时,通常我们使用import 模块,或者

从其他模块导入函数时,通常我们使用

import 模块,

或者

from 模块 import 函数

如果导入指定的几个函数,可以这样

from 模块 import 函数1,函数2,函数3

或者

from 模块 import *

最后一种表示从某模块中导入所有函数。

对于导入的模块和函数,我们可以给这个模块和函数起别名。例如,下面导入了math模块,

import math as foobar
v1=foobar.sqrt(2)
v2=foobar.sqrt(81)
print(v1)
print(v2)

输出

1.4142135623730951
9.0

math模块别名为foobar,使用foobar.sqrt调用了math中开平方的方法。

再比如,对函数命别名,

from math import  pow as hoobar
v3=hoobar(2,3)
print(v3)

输出

8.0

这样,将math中pow 函数命名为hoobar,用hoobar完成幂运算。

工程文件下载: https://download.csdn.net/download/yysyangyangyangshan/10712521


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 3

The Art of Computer Programming, Volume 3

Donald E. Knuth / Addison-Wesley Professional / 1998-05-04 / USD 74.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 3》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码