Python中常用模块的使用

1.Keyword

python的keyword是内置模块,它提供了与Python关键字相关的一些功能。可以使用keyword模块来检查标识符是否是Python关键字,获取所有Python关键字的列表以及其他一些与关键字相关的信息。

用法:

import keyword

print(keyword.kwlist)

2.Random

python的random模块是用于生成伪随机数的内置模块,提供了各种生成随机数的函数和方法,以及随机数种子控制。

一些常用的random模块函数用法:

import random
# 随机一个0-1之间的数
print(random.random())

# 随机整数,包含3和9
print(random.randint(3, 9))

# 从列表中选择一个
print(random.choice(["盖浇饭", "黄焖鸡", "热干面"]))
# 列表中选择一个,指定权重,k代表选择的次数,出现几次结果
print(random.choices(["盖浇饭", "黄焖鸡", "热干面"], [0.55, 0.3, 0.15], k=2))
# 列表中选择n个, k代表选择的个数
print(random.sample(["盖浇饭", "黄焖鸡", "热干面"], k=2))

3.Turtle

python的turtle模块是一个绘图库,通常用于学习编程和图形设计。turtle模块模拟了一只海龟,可以通过编程来控制这只海龟在屏幕上绘制图形。

案例:

# 导入模块
import turtle

turtle.left(120)
# 设置画笔宽度
turtle.width(8)
#开始填充
turtle.begin_fill()
# 指定画笔颜色和填充色
turtle.color("green", "yellow")
# 开始画圆
# turtle.circle(100)
# 向前走
turtle.forward(100)
# 乌龟的头向左旋转90度
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)

# 结束填充
turtle.end_fill()
# 抬起画笔
turtle.up()

turtle.forward(200)
turtle.right(120)
# 放下画笔
turtle.down()
turtle.forward(200)
turtle.right(120)
turtle.forward(200)
turtle.right(120)
turtle.forward(200)


# 设置循环
# 程序一直执行不会立即结束,画图界面可以展示
turtle.mainloop()

4.Math

math模块用于提供各种数学运算和函数,执行各种数学操作。

一些使用案例:

# 数学模块math
import math

# 向上取整
print(math.ceil(3.15))
# 向下取整
print((math.floor(3.15)))

# 对数 第二个参数是底数 第一个是参数值
print(math.log(8, 2))

# 次方
print(math.pow(2, -1))

# 开根
print(math.sqrt(64))

# 弧度转角度
print(math.degrees(math.pi))

print(math.sin(math.pi / 4))

print(math.cos(math.pi / 4))

math.tan(math.pi / 2)

5.Datetime

python的datetime模块是一个内置模块,用于处理日期和时间。包括日期和时间的创建、操作、格式化及计算日期间隔等。

一些常见的用法:

# 构造日期时间
datetime1 = datetime(year=2008, month=8, day=18, hour=23, minute=11, second=34)
# 打印日期时间
print(datetime1.year, datetime1.month, datetime1.day, datetime1.hour, datetime1.minute, datetime1.second)
# 格式化
print(datetime1.strftime("%Y-%m-%d %H:%M:%S"))
# 获取现在的时间
datetime2 = datetime.now()
print(datetime2.year, datetime2.month, datetime2.day, datetime2.hour, datetime2.minute, datetime2.second)
print(datetime2.strftime("%Y-%m-%d %H:%M:%S"))
# 时间增量 timedelta
# 获取现在的时间
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
# 设置时间增量
timedelta0 = timedelta(weeks=3, days=3, hours=3, seconds=45)
print(timedelta0.days, timedelta.seconds)
# 设置一个时间增量,加到现在的时间上
future = now + timedelta(weeks=2, days=3, hours=2, seconds=23)
print(future.strftime("%Y-%m-%d %H:%M:%S"))

6.Time

time模块可以进行延时、获取时间戳、时间结构体化。

常用方法:

import time

# time.sleep()  休眠
print("哈哈哈")
time.sleep(2)

print("嘿嘿嘿")
time.sleep(2)

print("嘻嘻嘻")
time.sleep(2)

# time.time() 获取当前时间戳
# 其整数部分是从1970-1-1 0时到现在的秒数
print(time.time())
time.sleep(2)

print(time.time())
time.sleep(2)

# 将时间结构体化
time.localtime()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

7.Calender

calendar模块用于执行与日历相关的各种操作,如创建日历、查看日期、计算星期几等。

常见用法:

import calendar

# 年历
print(calendar.calendar(2024))
# 月历
print(calendar.month(2024, 1))
# 周
print(calendar.weekday(2024, 1, 11))

# 闰年
print(calendar.isleap(2000))

小结

常用的模块可以帮助我们快速找到实现需求编程的方法,提高解决问题的效率,平常要多积累一些。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值