1.Keyword
import keyword
# print(keyword.kwlist)
2.Random
随机
编写应用程序经常使用随机模块
import random
# print(random)
# 随机一个0-1之间的浮点数 [0, 1)
print(random.random())
# 随机一个位于a,b之间的整数 [a, b]
print(random.randint(1, 3))
# 从列表中选择一个
print(random.choice(["哈尔滨", "大连", "长春"]))
# 从列表中选择一个 指定每一个出现的权重 k=2代表选两次
print(random.choices(["哈尔滨", "大连", "长春"], [0.05, 0.45, 0.5], k=2))
# 从列表中选择n个
print(random.sample(["哈尔滨", "大连", "长春"], k=2))
3.Turtle
# 导入模块
import turtle
print(turtle)
turtle.right(45)
# 指定画笔宽度
turtle.width(3)
# 指定画笔颜色
turtle.color("green", "blue")
# 开始填充
turtle.begin_fill()
# 画一个圆形
# turtle.circle(100)
turtle.forward(100)
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
数学
import math
# 无穷大
print(math.inf)
# 3.14
print(math.pi)
# 弧度转角度
print(math.degrees(math.pi))
# 角度转弧度
print(math.radians(180))
# 正弦值
print(math.sin(math.pi / 2))
# 反正弦值
print(math.asin(1))
# 余弦值
print(math.cos(math.pi / 2))
# 反余弦值
print(math.acos(0))
# 正切值
print(math.tan(math.pi / 4))
# 反正切
print(math.atan(1))
# 向上取整
print(math.ceil(3.14))
# 向下取证
print(math.floor(3.14))
# 对数 第二个参数是底数 第一个参数值
print(math.log(1000, 10))
# 次方
print(math.pow(5, 3))
# 开根
print(math.sqrt(64))
print(math.isinf(math.inf))
print(math.isfinite(math.inf))
print(math.isnan(5.5))
5.Datetime
日期时间
from datetime import date, time, datetime, timedelta
# 使用数字构造日期
# # date0 = date(year=2001, month=1, day=10)
# 获取今天日期
# date0 = date.today()
# print(type(date0))
# # 取得日期中每一个部分
# print(date0.year, date0.month, date0.day, date0.weekday())
# %y 两位年 %Y 四位年份 %m 两位月 %d两位日
# print(date0.strftime("%Y-%m-%d"))
# 使用数字构造时间
# time0 = time(hour=17, minute=1, second=30)
# print(type(time0))
# print(time0.hour, time0.minute, time0.second)
# print(time0.strftime("%H:%M:%S"))
# datetime0 = datetime(year=2001, month=5, day=25, hour=5, minute=29, second=11)
# datetime0 = datetime.now()
# print(type(datetime0))
# print(datetime0.year, datetime0.month, datetime0.day, datetime0.hour, datetime0.minute, datetime0.second)
# print(datetime0.strftime("%Y-%m-%d %H:%M:%S"))
# now = datetime.now()
# print(now.strftime("%Y/%m/%d %H:%M:%S"))
#
# timedelta0 = timedelta(weeks=1, days=1, hours=2, seconds=30)
# print(timedelta0.days, timedelta0.seconds)
#
# future = now + timedelta(weeks=2, hours=2)
# print(type(future), future.strftime("%Y/%m/%d %H:%M:%S"))
#
# future = now - timedelta(hours=8)
# print(type(future), future.strftime("%Y/%m/%d %H:%M:%S"))
6.time
时间
使用数字构造时间
# time0 = time(hour=17, minute=1, second=30)
# print(type(time0))
# print(time0.hour, time0.minute, time0.second)
# print(time0.strftime("%H:%M:%S"))
# time.sleep(5)
# print(5)
# time.time整数部分是从1970-1-1 0时到现在的秒数
# print(time.time())
# time.sleep(5)
# print(time.time())
# time.sleep(5)
# print(time.time())
# 将时间结构体元组 格式化
# print(time.strftime("%Y/%m/%d %H:%M:%S", (1999, 9, 9, 9, 9, 9, 0, 0, 0)))
# 获取时间结构体
# print(time.localtime())
# 将时间结构体元组 格式化
# print(time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()))
7.calenderd
日历
import calendar
# print(calendar)
# 年历
# print(calendar.calendar(2024))
# 月历
# print(calendar.month(2024, 1))
# 周几
# print(calendar.weekday(2024, 1, 11))
# 是否为闰年
print(calendar.isleap(2001))