day13包和模块总结

模块

import 模块名 - 导入指定模块;导入后可以通过’模块.xx’的方式使用这个模块中所有的全局变量

from 模块名 import 变量1,变量2··· -导入指定变量:导入后可以直接使用指定变量

from 模块名 import * - 导入指定模块中所有的变量;导入后可以直接使用所有变量

import 模块名 as新模块名 - 直接导入指定模块并且对模块进行重命名,重命名后需要通过新模块来代替原模块名使用模块

from 模块名 import变量1 as新变量

导入方式

 ----------导入方式1--------------
 import test
 print(test.a)
 test.func1()
 print(test.b)

# ----------导入方式2--------------
# from test import a, func1
# print(a)
# func1()

# ----------导入方式3--------------
# from test import *
# print(a)
# func1()
# print(b)

# ----------导入方式4--------------
# import test as ts
# print(ts.a)
# ts.func1()
# print(ts.b)

# import time as T
# time = '2000-9-1'
# print(T.time())
# print(time)
导入原理及阻止原理

不管以什么样的方式导入模块,导入模块的时候系统会自动进入模块执行这个模块中所有的代码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I8SpMJZp-1651144735626)(C:\Users\Asus\AppData\Roaming\Typora\typora-user-images\image-20220427192315706.png)]

执行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uRmE2pvF-1651144735635)(C:\Users\Asus\AppData\Roaming\Typora\typora-user-images\image-20220427192522794.png)]

阻止导入

导入模块的时候会默认执行模块中所有的代码,就导致有些完全没必要再导入的时候执行的代码被执行

如果希望导入模块的时候某些代码可以不执行,就需要去阻止代码在被导入的时候执行 - 阻止导入

阻止方法:将不需要执行的代码放到main对应if条件语句里面

在这个if语句里面的代码再被别的模块导入的时候不会被执行

包就是存放在py文件里面的文件夹

怎么使用包中的内容:

方法1; import包名 -直接导入包名,导入后可以通过 (包.xx来使用.py中所有的变量

方法2;import 包名.模块名 - 导入指定包中的指定模块,导入后可以通过‘包.模块名.xx’来使用指定模块中所有的全局变量

方法三;from包名import 模块1 模块2 模块3····导入指定包中的指定模块,导入后通过‘模块名.xx’使用对应的中的内容

方法四;from 包名 import * - 导入包中py文件中所有的全局变量

方法五;from 包名.模块名 import 变量1,变量2··· - 导入指定模块中的指定模块

# --------------导入方式1: 直接导入包--------------
import package1
print(package1.y)
package1.func3()

# --------------导入方式2: 直接导入包中的指定模块--------------
# import package1.test2
# print(package1.test2.x)
# package1.test2.func2()

# 改良:
# import package1.test2 as test2
# print(test2.x)
# test2.func2()

# --------------导入方式3: 直接导入包中的指定模块--------------
# from package1 import test2, test3
# print(test2.x)
# test2.func2()
#
# print(test3.z)
# test3.func4()
数字模块
import math, cmath
# math - 普通数字对应的数学模块
# cmath - 复数对应的数学模块

# 补充复数  - python支持复数
# 格式: a + bj
num1 = 10 + 3j
num2 = 5 - 6j
print(num1 + num2)
print(num1 * num2)


# 1.浮点数转整数
# 1)int(浮点数)  -  直接保留整数部分
print(int(1.23))        # 1
print(int(-1.23))       # -1

# 2)math.ceil(浮点数)  -   向大取整
print(math.ceil(1.98))      # 2
print(math.ceil(1.002))     # 2
print(math.ceil(-1.23))     # -1

# 3)math.floor(浮点数)     -   向小取整
print(math.floor(1.98))     # 1
print(math.floor(1.002))    # 1
print(math.floor(-1.23))    # -2

# 4)round(浮点数)      -   四舍五入
# >0.5 -> 入
# <0.5 -> 舍
# == 0.5 ->可能舍也可能入
print(round(1.68))      # 2
print(round(1.402))     # 1

# 注意: x.5的时候,x如果是奇数就入,x是偶数就舍
print(round(1.5))       # 2
print(round(4.5))       # 4


# 2.求绝对值
# 1)abs(数字)
print(abs(-23))
print(abs(-23.45))

# 2)fabs(数字)
print(math.fabs(-23))       # 23.0
print(math.fabs(-23.45))    # 23.45

随机模块

1.创建随机整数:randint(a,b) -产生[a,b]的随机整数

2.创建随机小数:random() -产生[0,1)的随机小数

print(random.random())
# 产生 0 ~ 100的随机小数
print(random.random() * 100)
print(float(f'{random.random() * 100: .2f}'))
print(int(random.random() * 10000) / 100)
# 3. 在指定的等差数列中随机获取一个数: randrange(N)、randrange(M, N)、randrange(M, N, step)
print(random.randrange(0, 100, 2))
print(random.randrange(10, 61, 10))
# 2)
# choices(序列) - 从指定序列中随机获取一个元素,返回值是列表
print(random.choices([10, 20, 30, 40, 50]))
print(random.choices('abc123'))

# choices(序列, k=次数) - 从指定序列中随机获取指定个数个元素,返回值是列表 (有放回抽样)
print(random.choices([10, 20, 30, 40, 50], k=2))

# choices(序列, weights=权重列表, k=次数)
# weights: [1, 1, 1, 1, 1]    1/5
# weights: [1, 2, 1, 1, 1]    1/6、2/6、1/6、1/6、1/6
list1 = ['谢谢!', '5元红包', '100元红包', 'ipone13', 'macPro最新', '100万现金']
print(random.choices(list1, weights=[50000, 50000, 5, 3, 2, 1]))


# 3)
# random.sample(序列, k=次数)         (无放回抽样)  # 3.9以前的版本
# random.sample(序列, k=次数, counts=权重列表)   # 3.9以后的版本
print(random.choices([10, 20], k=2))
print(random.sample([10, 20], k=2))
print(random.sample(list1, k=1, counts=[50000, 50000, 5, 3, 2, 1]))
time模块

time() - 获取当前时间,返回的是时间戳

时间戳 - 通过保存一个时间到1970年1月1日0时0分0秒(格林威治时间)之间的时间差(单位: 秒)来保存一个时间值

使用时间戳保存时间的好处:节约时间 、方便加密

localtime()       -       # 获取本地当前时间,返回的是结构体时间
# localtime(时间戳)    -    #  将时间戳转换成结构体时间
t2 = localtime()
print(t2)       # tm_wday - 星期(0(周一)~6(周天))

秒)来保存一个时间值

使用时间戳保存时间的好处:节约时间 、方便加密

localtime()       -       # 获取本地当前时间,返回的是结构体时间
# localtime(时间戳)    -    #  将时间戳转换成结构体时间
t2 = localtime()
print(t2)       # tm_wday - 星期(0(周一)~6(周天))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值