Python常用标准库1-Turtle,Random,Time和Datetime

模块名 功能描述
Turtle 海龟画图
Random 随机数
Time 格式化日期和时间模块
Datetime 日期和时间处理函数
Os 使用操作系统功能操控文件  
Sys 提过对Python解释器相关操作
Timeit 性能度量的模块
Zlib 数据打包和压缩
Math 数学模块
Re 正则表达式模块
Urllib.request 处理从urls接收到的数据模块
Unittest 代码测试模块

1.Turtle模块  

  Turtle模块可以控制一个画笔来进行二维空间的画图操作。

  1.1 画布(Canvas)

    用来展开绘图的区域,默认有一个坐标原点来确定x轴和y轴。采用函数来设置大小,和颜色

    1.1.1 采用 turtle.screensize()函数来调整大小和颜色

    

函数原型 turtle.screensize(width,height,bg)  
width为画布的宽,height为画布的高,单位都是像素,bg是画布背景颜色,直接用英语,如"black"

    1.1.2 采用 turtle.setup()函数来设置大小和左上角定点在窗口的坐标位置

turtle.setup(width,height,startx,starty)
startx是横坐标,starty的纵坐标。用来描述左上角顶点在口疮的坐标位置

  1.2 画笔

    1.2.1 画笔状态,Turtle模块绘图使用位置方向描述画笔的状态。

    1.2.2 画笔属性。 画笔的属性包括颜色、宽度和移动速度:

      

turtle.pensize(width)  #宽度越大画笔越大
turtle.pencolor(color)  #设置颜色可以用字符串“block”也可以用RGB格式
turtle.speed(speed)    #设置画笔速度speed,范围是[0,10]整数,数字越大速度越快

    1.2.3 绘图命令:分为三类画笔运动命令,画笔控制命令和全局控制命令

      画笔运动命令:

turtle.penup() 提起画笔,提起后再移动画笔就不会绘制图形了
turtle.pendown() 放下画笔,放下后移动画笔默认为绘制图形
turtle.forward(dis) 向当前画笔方向移动指定长度dis
turtle.backward(dis) 向当前方向的反方向移动dis长度
turtle.right(degree) 顺时针旋转指定角度
turtle.left(degree) 逆时针旋转指定角度
turtle.home() 画笔回到原点,朝向默认位置
turtle.goto(x,y) 画笔移动到绝对位置的(x,y)坐标
setx(x)  将当前x轴移动到指定x轴位置
sety(y)
turtle.circle(r) r为负表示在画笔的右边画圆,r为正表示在画笔的左边画圆

      画笔控制命令

turtle.color(color1,color2)    画笔颜色为color1,填充颜色为color2
turtle.fillcolor(color) 设置填充颜色color
turtle.begin_fill()     开始填充
turtle.end_fill()        结束填充
turtle.hideturtle()    隐藏turtle箭头
turtle.showturtle()    显示turtle箭头

      全局控制命令

turtle.clear()    清空所有内容
turtle.reset()    将状态和位置复位为初始值
turtle.done()    使turtle窗口不会自动消失
turtle.undo()    取消最后一个图形操作
turtle.write(s[,font-"font-name","font_size","font_type"])
在画布上写文本
例子: 
t.write("sm7是傻逼",font=("微软雅黑",14,"normal"))

         下面是一个海龟画图的小demo

import turtle
#进行画布和画笔的设置


#画一个长方形
def rectangle():
    #先画长
    turtle.pendown()#拿着画笔
    turtle.right(0)#画笔指向0读地方
    turtle.forward(100)#向前移动一百
    turtle.right(90)#向下90度
    turtle.forward(50)
    turtle.right(90)
    turtle.forward(100)
    turtle.right
        (90)
    turtle.forward(50)

#开始画图
rectangle()


turtle.done()

2. Random模块

  2.1 功能介绍:random模块用于生成随机数。

  2.2 函数介绍:

    2.2.1 random.random()函数用来生成一个[0,1)之间的随机浮点数

例子:
import random
for i in range(1,6):
    print(random.random())
输出5个0-1的浮点数

    2.2.2  ramdom.uniform()函数

        用于生成一个指定范围内的随机浮点数

例子:
    import random
    print(random.uniform(3,6))
    print(random.uniform(1,3))
    print(random.uniform(-1,1))

    2.2.3 random.randint()函数 用于生成一个指定范围内的整数

  

ramdom.randint(a,b)
生成一个a到b内的一个随机整数

    2.2.4  random.randrange()函数用于生成指定范围,步长的随机函数

函数格式为:
    random.randrange([start],stop[,step])
例子:生成10个1-100范围的奇数
import random
list = []
for i in range(10):
  list1.append(random.randrange(1,100,2))
print(list)

    2.2.5  random.choice函数

random.choice()
函数的功能是从序列对象中获取一个随机元素。
格式 random.choice(sequence)
例子:
list = [1,2,3,4,5,6]
x = random.choice(list)
print(x)

    2.2.6  random.shuffle()函数

功能描述:把一个序列中的元素打乱
一般格式为:random.shuffle(sequence[,random])
例子:
import random
list = [1,2,3,4,5,6,7,8,9,10]
list_bak = random.shuffle(list)
print(list_bak)

    2.2.7  random.sample()函数

功能描述:从指定序列中随机获取指定长度的片段
一般格式:random.sample(sequence,k)
例子:
import random
list = [1,2,3,4,5,6,7,8,9]
list_ = random.sample(list,2)

  

3 Time模块和Datetime模块

  3.1 Time模块

    Time模块用于时间访问和转换,提供了与时间相关的函数。

方法/属性 描述

time.time()

返回一个从1970-1-1 0:00:00到现在经历的浮点秒数
time.astime([t]) 将一个tuple或struct_time形式的时间转换为一个表示当时本地时间的字符串
time.ctime([secs])   将一个秒数时间戳转为本地时间的字符串
time.localtime([sec])  返回以指定时间戳对应本地时间struct_time对象
time.strftime(s,t) 将struct_time对象转换为字符串
time.altzone() 返回与utc时间的时间差,以秒为单位

  3.2  Datetime模块

      Datetime模块为日期和时间处理提供了处理的类和方法

    3.2.1  date类

      date类为日期类

      创建需要三个必须参数

      例子: d = datetime.date(year,month,day)

    

from datetime import date
d = date.today()
print("日期: %d 年,%d 月,%d 日."%(d.year,d.month,d.day))

    3.2.2  time类

      time类为时间类,创建一个time类对象的一般格式为:

      t = time(hour,[minute[,second,[microsendcon[,tzinfo]]]])

      t = time(时,分,秒,微妙)

      其中hour是必须指定的参数,其它的可选参数

from datetime import time
print("时间最大为多少:",time.max)
print("时间最小值:",time.min)
t = time(20,30,50,8888)
print("时间:%d时%d分%d秒%d微秒."%(t.hour,t.minute,t.second,t.microsecond)

    3.2.3  datetime类:

      datetime类是一个日期时间类。

      创建对象一般格式:dt = datetime(year,month,day,hour,minute,second,microsecond,tzinfo)

      其中year,month和day是必须指定的参数

#datetime类实例:
from datetime import datetime
dt = datetime.now()
print("当前日期:",dt.date())
print("当前时间:",dt.time())
dt2 = datetime(2020,7,21,0,0,0)
print("时间:",dt2)

    3.2.4  timedelta类

      timedelta对象表示两个不同时间的差值,差值单位任意。

      创建对象格式: td = datetime.timedelta(days,seconds,microseconds,milliseconds,hours,weeks)

      td = datetime.timedelta(日,秒,微秒,毫秒,小时,周)

      都是可以选参数,直接使用赋值来处理

from datetime import timedelta,datetime
print("一周的所有秒数:",timedelta(days=7).total_seconds())
d = datetime.now()
print("当前系统时间:",d)
print("昨天时间:",d+timedelta(day=-1))
print("明天时间:",d+timedelta(day=1))

      

原文地址:https://www.cnblogs.com/beautiful7/p/13341009.html