赞
踩
引用库用import,当库名比较长且多次引用时有import<库名>as<库别名>
画笔控制函数(操作一直有效,一般成对出现) | |||
turtle.penup()/pu() | 拾起画笔,海龟在飞行 | ||
Turtle.pendown()/pd() | 落下画笔,海龟在爬行 | ||
Turtle.pensize(width)别名turtle.width(width) | 画笔宽度,海归的腰围 | ||
Turtle.pencolor(color) color为颜色字符串或RGB值 | 画笔颜色,海龟在涂装 |
运动控制函数 | |
Turtle.forward(d)别名turtle.fd(d) d为行进距离,可以为负 | 前行行进,海龟走直线 |
Turtle.circle(r,extent=None) 根据半径r绘制extent角度的弧度 R:默认圆心在海龟左侧r距离的位置 Extent:绘制角度,默认是360度整圆 |
方向控制函数 | ||
Turtle.setheading(angle)别名turtle.seth(angle) | 改变行进方向,海龟走角度 | |
Turtle.left(angle) Angle:在海龟当前行进方向上旋转的角度 | 海龟向左转 | |
Turtle.right(angle) | 海龟向右转 |
turtle库基本介绍 | turtle的绘图窗口一般表示为: turtle.setup(width,height,startx,starty) setup()设置窗体大小及位置;4个参数中后两个可选 |
| ||
turtle绘图窗口布局 | | |||
turtle空间坐标体系 |
| |||
turtle角度坐标体系 |
| | ||
RGB色彩体系 | RGB色彩模式由三种颜色构成的万物色(红蓝绿)。每色取值范围0-255整数或0-1小数 | | ||
|
例代码:
#PythonDraw.py
import turtle
turtle.setup(650, 350, 200, 200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4): //range() 随机函数,搭配使用按一定次数循环指向一组语句,for <变量> in range(<参数>)<被循环指向的语句>
turtle.circle(40, 80) //range()函数:产生循环技术序列
turtle.circle(-40, 80) //range(N)产生0到N-1的整数序列,共N个;range(M,N)产生M到N-1的整数序列,共N-M个
turtle.circle(40, 80/2)
turtle.fd(40)
turtle.circle(16, 180)
turtle.fd(40 * 2/3)
turtle.don
![]() | |
![]() |
Time库包括三类函数
time():获取当前时间戳,及计算机内部时间值,浮点数
ctime():获取当前时间并以易读方式表示,返回字符串
gmtime():获取当前时间,表示为计算机可处理的时间格式
strftime(tpl,ts):tpl是格式化模板字符串,用来定义输出效果;ts是计算机内部时间变量
strptime(str,tpl):str是字符串形式的时间值,tpl是格式化模板字符串,用来定义输出效果
sleep(s)(产生实践):s拟定休眠的时间,单位是秒,可以是浮点数
per_counter()(测量时间):返回一个CPU级别的精确时间计数值,单位为秒由于这个计
数值起点不确定,连续调用差值才有意义
例子:
文本进度条
#TextProBarV3.py
import time
scale = 50 print("执行开始".center(scale//2, "-"))
start = time.perf_counter()
for i in range(scale+1):
a = '*' * i
b = '.' * (scale - i)
c = (i/scale)*100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
time.sleep(0.1)
print("\n"+"执行结束".center(scale//2,'-'))
Random库包括两类函数,常用共8个
基本随机数函数:
seed(a=None):初始化给定的随机数种子,默认为当前系统时间
Random():生成一个[0.0,1.0]之间的随机小数
扩展随机数函数:
Randint(a,b):生成一个[a,b]之间的整数
Randrange()(m,n[,k]):生成一个[m,nn)之间以k为步长的随机整数
Uniform(a,b):生成一个[a,b]之间的随机小数
Getrandbits(k):生成一个k比特长的随机整数
Choice(sep):从序列sep中随机选择一个元素
Shuffle(sep):将序列sep中元素随机排列,返回打乱后的序列
例子:
蒙特卡罗方法
#CalPiV2.py
from random import random
from time import
perf_counter
DARTS = 1000*1000
hits = 0.0
start = perf_counter()
for i in range(1, DARTS+1):
x, y = random(), random()
dist = pow(x ** 2 + y ** 2, 0.5)
if dist <= 1.0:
hits = hits + 1 pi = 4 * (hits/DARTS)
print("圆周率值是: {}".format(pi))
print("运行时间是: {:.5f}s".format(perf_counter() - start))
将.py源代码转换成无需源代码的可执行文件(非常神奇)
第三方库需要额外安装:
官方网站:http://www.pyinstaller.org
但是最好是用cmd命令行 pip install pyinstaller
使用:
(cmd命令行) pyinstaller-F<文件名.py>
Pyinstaller库常用参数 | |
-h | 查看帮助 |
--clean | 清理打包过程中的临时文件 |
-D,--onedir | 默认值,生成dist文件夹 |
-F,--onefile | 在dist文件夹中只生成独立的打包文件 |
-i<图标文件名.ico> | 指定打包过程使用的图标(icon)文件 |
例子:
Pyinstaller -i curve.ico -F SevenDigtsDrawV2.py
Jieba是优秀的中文分词第三方库,安装有(cmd命令行)pip install jieba
jieba库常用函数 | |
Jieba.lcut(s) | 精准模式,返回一个列表类型的分析结果 |
Jieba.lcut(s,cut_all=ture) | 全模式,返回一个列表类型的分词结果,存在冗余 |
Jieba.lcut_for_search(s) | 搜索引擎模式,返回一个列表类型的分词结果,存在冗余 |
Jieba.add_world(w) | 向分词词典增加新词w |
例子:
Hamlet词频统计(含Hamlet原文文本)
#CalHamletV1.py
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1 items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))《三国演义》人物出场统计(上)(含《三国演义》原文文本)
安装:(cmd命令行) pip install worldcloud
wordcloud库常规方法w=wordcloud.WordCloud() | |
W.generate(txt) | 向wordcloud对象w中加载文本txt |
W.to_file(filename) | 将词云输出为图像文件,.png或.jpg格式 |
配置对象参数 | |
Width | 指定词云对象生成图片的宽度,默认400像素 |
Height | 指定词云对象生成图片的高度,默认200像素 |
Min_font_size | 指定词云中字体的最小字号,默认4号 |
Max_font_size | 指定词云中字体的最大字号,根据高度自动调节 |
Font_step | 指定词云中字体字号的步进间隔,默认为1 |
Font_path | 指定字体文件的路径,默认None |
Max_words | 指定词云显示的最大单词数量,默认200 |
Stop_words | 指定词云的排除词列表,及不显示的单词列表 |
Mask | 指定词云形状,默认为长方形,需要引用imread()函数 |
Background_color | 指定词云图片的背景颜色,默认为黑色 |
例子:
常规矩形词云
#GovRptWordCloudv1.py
import jieba
import wordcloud
f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8") t = f.read()
f.close() ls = jieba.lcut(t) txt = " ".join(ls)
w = wordcloud.WordCloud( \ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc" )
w.generate(txt)
w.to_file("grwordcloud.png")
不规则图形词云
#GovRptWordCloudv2.py
import jieba
import wordcloud from scipy.misc
import imread
mask = imread("chinamap.jpg")
excludes = { } f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
t = f.read() f.close()
ls = jieba.lcut(t)
txt = " ".join(ls) w = wordcloud.WordCloud(\ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", mask = mask )
w.generate(txt)
w.to_file("grwordcloudm.png")
os库提供通用的、基本的操作系统交互功能;是Python标准库,包含几百个函数
常规路径操作:os.path子库以path为入口,用于操作和处理文件路径
进程管理:os.system(command)
环境参数:
环境参数 | |
Os.chdir(path) | 修改当前程序操作的路径 |
Os.getcwd() | 返回程序的当前路径 |
Os.getlogin() | 获得当前系统登录用户名名称 |
Os.cpu_count() | 获得当前系统的CPU数量 |
Os.urandom(n) | 获得n个字节长度的随机字符串,通常用于加密运算 |
例子(可以安装很多的库哦,非常有用哦)
#BatchInstall.py
import os libs = {"numpy","matplotlib","pillow","sklearn","requests",\ "jieba","beautifulsoup4","wheel","networkx","sympy",\ "pyinstaller","django","flask","werobot","pyqt5",\ "pandas","pyopengl","pypdf2","docopt","pygame"}
try:
for lib in libs:
os.system("pip3 install "+lib)
print("Successful")
except:
print("Failed Somehow")
学习之路漫长啊
-数据分析
-数据可视化
-文本处理
-机器学习
-网络爬虫
-web信息提取
-web网站开发
-网络应用开发
-图形用户界面
-游戏开发
-虚拟现实
-图形艺术
学习之路:Python基础语法(函数式编程)->Python进阶语法(面向对象编程)->Python高级语法(pythonic编程)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。