python——模块(time、datetime、random、math)

模块

内置模块
自写模块
  • 为了方便性,我们把写好的代码进行分类,包装在不同的后缀名为.py文件中,使用的时候我们只需要导入相应的文件就可以直接调用,这些文件成为模块。

导入模块

语法:
import <模块名>
  • 导入的时候不用写文件后缀名.py,相当于我们借用了导入模块的内容进行使用。
  • 导入模块以后,我们可以通过使用==模块名.函数名(变量名)==就可以进行调用相关函数(变量)。
  • 导入时,我们还可以import 模块名 as (你自己想的简称)
  • 我们需要同时导入多个模块时可以用逗号隔开import a,b,c
# other.py
slogan = '带你打开编程世界的大门'
print(slogan)

# main.py
import other  # 导入 other 模块
# 输出:带你打开编程世界的大门

*********************分割线************************

# other.py
slogan = '带你打开编程世界的大门'

def repeat_slogan():
  return slogan * 2

# main.py
import other  # 导入 other 模块

print(other.slogan)
# 输出:带你打开编程世界的大门

print(other.repeat_slogan())
# 输出:带你打开编程世界的大门带你打开编程世界的大门
  • 导入模块时也可以只导入某个模块中我们需要的那一部分from 模块 import 变量(函数)
# other.py
slogan = '带你打开编程世界的大门'

def repeat_slogan():
  return slogan * 2

# main.py
from other import slogan  # 从 other 模块导入 slogan

print(slogan)
# 输出:带你打开编程世界的大门

print(other.repeat_slogan())
# 报错:NameError: name 'other' is not defined on line 6
# 使用 from ... import ... 的方式导入就不能再用 other. 的方式访问模块内的变量或函数

程序入口 if __name__ == '__main__'

  • 当在做复杂项目的时候,我们会将代码拆分到各个模块中。这样,我们就需要一个程序入口,通过这个入口文件将整个项目串联运行起来。
  • Python 作为脚本语言本身并没有程序入口的概念,但是我们用if __name__ == '__main__'来作为python模拟的程序入口。
# xxx.py
代码块 A

if __name__ == '__main__':
  代码块 B
# 当直接运行xxx.py文件时,执行代码块 A,并执行代码块 B
#当xxx.py文件被import时,只执行代码块 A,不执行代码块 B

#实例:
# other.py
print('我是 other 模块')

if __name__ == '__main__':
  print('我被直接运行了(other)')

# main.py
import other

print('我是 main 模块')

if __name__ == '__main__':
  print('我被直接运行了(main)')

结果:
我是 other 模块
我是 main 模块
我被直接运行了(main)
  • __name__ 其实是py文件的内置函数,当文件以模块导入时,__name__的值为文件名,但是文件被直接运行时,__name__的值为'__main__'我们可以通过__name__的值来判断文件是否为程序入口文件。

内置模块

time模块

time.sleep():用于暂停程序的运行,参数为秒。
time.time():获取当前时间的Unix 时间戳。
  • Unix时间戳是指格林威治时间 1970 年 01 月 01 日 00 时 00 分 00 秒起至现在的总秒数。Unix 时间戳只是一个单纯不断增长的数字,所以不会存在时区等问题,全球统一,简单便捷。
time.ctime():获取当前时间,可读性比较高。
# 导入time 模块
import time

# 暂停程序的执行,参数为秒
time.sleep(3)  # 暂停 3 秒再执行后续代码

# 获取当前时间的 Unix 时间戳
print(time.time())
# 输出:1558329240.201314

print(time.ctime())
# 输出:Mon May 20 13:14:00 2019

datetime模块

# 导入 datetime 模块中的对象
from datetime import datetime, timedelta

# 获取当前时间
print(datetime.now())	#与ctime()作用差不多,但是这个没有星期
# 输出:2019-05-20 13:14:00.201314

# 将 Unix 时间戳转换成时间
print(datetime.fromtimestamp(1558329240.201314))
# 输出:2019-05-20 13:14:00.201314

#当前时间的五天前的时间
print(datetime.now() - timedelta(days=5))	
# 输出:2019-05-15 13:14:00.201314

# 周(weeks)
print(datetime.now() - timedelta(weeks=2))
# 输出:2019-05-06 13:14:00.201314

# 时(hours)
print(datetime.now() + timedelta(hours=2))
# 输出:2019-05-20 15:14:00.201314

# 分(minutes)
print(datetime.now() + timedelta(minutes=2))
# 输出:2019-05-20 13:16:00.201314

# 秒(seconds)
print(datetime.now() + timedelta(seconds=2))
# 输出:2019-05-20 13:14:02.201314

# 毫秒(milliseconds)
print(datetime.now() + timedelta(milliseconds=2))
# 输出:2019-05-20 13:14:00.203314

# 微秒(microseconds)
print(datetime.now() + timedelta(microseconds=2))
# 输出:2019-05-20 13:14:00.201316

#还可以传入多个参数
#两个半小时
print(datetime.now() - timedelta(hours=2, minutes=30))
# 输出:2019-05-20 10:44:00.201314

random模块

  • random.randint()函数和random.uniform()函数均是左右都可以取到。
# 导入 random 模块
import random

# 随机生成 0-1 之间的浮点数,包括 0,不包括 1
print(random.random())
# 输出:0.878022926133

# 随机生成 0-5 之间的整数,包括 0 和 5
print(random.randint(0, 5))
# 输出:3

# 随机生成 0-5 之间的浮点数,包括 0 和 5
print(random.uniform(0, 5))
# 输出:4.86369987147

# 从非空序列中随机返回一个元素,如果是一个空序列就会报错。。
print(random.choice([1, 2, 3]))
# 输出:2

math模块

# 导入 math 模块
import math

# 向上取整
print(math.ceil(2.3))
# 输出:3
print(math.ceil(2.9))
# 输出:3

# 向下取整
print(math.floor(2.3))
# 输出:2
print(math.floor(2.9))
# 输出:2

# 取平方根
print(math.sqrt(4))
# 输出:2.0

# 数学常数 π
print(math.pi)
# 输出:3.14159265359

# 数学常数 e
print(math.e)
# 输出:2.71828182846
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值