import 与 from import的区别

import 导入的是全部的库。

from  import 导入的是部分库。

下面以math库为例:

1、import math

导入 math 下的所有48个函数

>>>import math

>>>math.ceil(10.2)

11

>>>math.factorial(10)

3628800

2、from math import hypot

只导入 math 下的 hypot(x,y) 函数

>>>hypot(3,4)

5.0

3、from math import factorial as f

导入 math 下的 factorial 函数并用 f 代替

>>>f(10)

3628800

4、from math import *

导入 math 下的所有函数

>>>sin(5)

-0.9589242746631385

5、import math as m

导入 math 下的所有函数并起别名 m

>>>m.sin(5)

-0.9589242746631385

强烈推荐2、3、5;不建议使用4;使用3也可。具体原因自己在使用中就明白了。

本人小白一枚,说错还望指正,谢谢。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值