python中的turtle绘图,图形库
1.绘制NEUSOFT
- turtle.penup()抬笔,turtle.pendown()落笔,简写pu,pd
- turtle.forward(distance) //向当前画笔方向移动distance像素长度
- turtle.right(degree) //顺时针移动degree°
- turtle.left(degree) //逆时针移动degree°
- turtle.goto(x,y) //将画笔移动到坐标为x,y的位置
- turtle.circle() //画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆
- turtle.done() //必须是乌龟图形程序中的最后一个语句。
Python标准库中的GUI界面 -- 》 turtle
turtle的简单使用
# 导入turtle as 是给起一个别名
import turtle as t
# 设置画笔的大小 10px
t.setup(800, 500)
t.pensize(10)
t.color('green')
# 绘制 NEUSOFT
# 水平左移
# 抬笔
t.penup()
t.goto(-260, 0)
t.pd()
# 绘制 N
t.left(90)
t.forward(80)
t.right(145)
#简写
t.fd(100)
t.lt(145)
t.fd(80)
#绘制E\
t.penup()
t.goto(-170, 80)
t.pd()
t.goto(-120, 80)
t.goto(-170, 80)
t.left(90)
t.goto(-170, 0)
t.goto(-170, 40)
t.left(90)
t.goto(-120, 40)
t.pu()
t.goto(-170, 0)
t.pd()
t.goto(-120, 0)
#绘制U
t.pu()
t.goto(-90, 80)
t.pd()
t.goto(-90, 30)
t.circle(30, 180)
t.goto(-30, 80)
# 绘制s
t.pu()
t.goto(40, 60)
t.pd()
t.circle(22, 270)
t.circle(-22, 270)
# 绘制 O
t.penup()
t.goto(120, 55)
t.pd()
t.circle(28, 180)
t.goto(65, 20)
t.circle(28, 180)
t.goto(120, 50)
#绘制F
t.pu()
t.goto(150, 80)
t.pd()
t.goto(190, 80)
t.goto(150, 80)
t.goto(150, 0)
t.goto(150, 40)
t.goto(190, 40)
#绘制T
t.pu()
t.goto(215, 80)
t.pd()
t.goto(275, 80)
t.goto(245, 80)
t.goto(245, 0)
t.goto(245, 10)
# 让gui界面一直显示, 所有执行的代码要写在此函数之前
t.done()
执行结果
python中的元组、列表、字典
# hero_name = ["安琪拉", "赵云", "鲁班七号", "韩信"]
# print(hero_name[1])
# hero_name[1] = "赵子龙"
# print(hero_name[1])
# del hero_name[1]
# print(hero_name)
# hero_name.append("礼拜")
# print(hero_name)
# n_number = [1, 2, 3, 4, 5]
# for i in range(6, 15, 1):
# n_number.append(i)
# print(n_number)
# # 练习
# # 创建 [1, 2, 3......10] 这样的一个数字列表
# # 1.创建空列表
# li = []
# # 2.使用for 循环, 在循环中添加元素值
# for i in range(1, 11):
# li.append(i)
# print(li)
# 字符串
# 定义形式 '' ""
# 切片 对序列截取一部分的操作,适用于列表
# name = 'abcdefg'
# # name[1]
# # [起始位置:终止位置:步长] 左闭右开
# print(name[1:4])
# # a c e g
# print(name[0:7:2])
# # 全切片的时候可以省略初始和终止位置
# print(name[::2])
# 常用方法
# 去两端空格
# name = ' abcdefg '
# # 查看序列内元素的个数 len()
# print(len(name))
# name = name.strip()
# print('去空格之后', len(name))
# 替换
# price = '$999'
# price = price.replace('$','')
# print(price)
# # 列表变成字符串的方法 join
# li = ['a', 'b', 'c', 'd']
# a = '_'.join(li)
# print(a)
# print(type(a))
# 数字
# 元组 tuple 元组和列表很像只不过元组不可以修改
# 定义 ()
# a = ('zhangsan', 'lisi', 'wangwu',1000)
# print(a)
# print(type(a))
#
# # 访问
# print(a[1])
# # # 修改
# # a[3] = 'zhaoliu'
#
# # 关于元组需要注意的是 只有一个元素的元组
# b = ('lisi',) #是不是元组
# c = (1000,) #是不是元组
# print(type(b))
# print(type(c))
# 字典 dict java hashmap
# key-value数据结构
# 定义形式 {}
info = {'name':'李四', 'age':34, 'addr':'重庆市渝北区'}
print(len(info))
print(info)
# 1.字典的访问
print(info['name'])
# 2.修改
info['addr'] = '北京市朝阳区'
print('修改后字典',info)
# 3.增加
info['sex'] = 'female'
print('增加后字典',info)
# 获取字典中所有的键
print(info.keys())
# # 获取字典中所有的z值
print(info.values())
# 获取字典中所有的key-value
print(info.items())
d = [('name', '李四'), ('age', 34), ('addr', '北京市朝阳区'), ('sex', 'female')]
d1 = dict(d)
print(d1)
# 遍历字典
for k, v in info.items():
print(k, v)
# 集合
# 无序,不重复
set1 = {'zhangsan', 'lisi', 222}
#
print(type(set1))
# 遍历
for x in set1:
print(x)
1.列表排序
# 1.掌握python常用数据类型和语法
# 列表的排序
# li = []
# for i in range(10):
# li.append(i)
# print(li)
# from random import shuffle
# shuffle(li)
# print('随机打乱的列表', li)
# li.sort(reverse=True)
# print('排序后的列表', li)
stu_info = [
{"name":'zhangsan', "age":18},
{"name":'lisi', "age":30},
{"name":'wangwu', "age":99},
{"name":'tiaqi', "age":3},
]
print('排序前', stu_info)
# def 函数名(参数):
# 函数体
def sort_by_age(x):
return x['age']
# key= 函数名 --- 按照什么进行排序
# 根据年龄大小进行正序排序
stu_info.sort(key = sort_by_age, reverse=True)
print('排序后', stu_info)
# 练习
name_info_list = [
('张三', 4500),
('李四', 9900),
('王五', 2000),
('赵六', 5500),
]
# 根据元组第二个元素进行正序排序
name_info_list.sort(key=lambda x: x[1])
print(name_info_list)
# 根据元组第二个元素进行正序排序
name_info_list = [
('张三', 4500),
('李四', 9900),
('王五', 2000),
('赵六', 5500),
]
def sort_by_grade(i):
return i[1]
name_info_list.sort(key=sort_by_grade)
print(name_info_list)
2.函数定义
- def 函数名(参数列表):
函数体
def say_hello(name):
print('hello, {}'.format(name))
# say_hello('重庆师范')
3.jieba分词库
- 三种分词模式:
1.精确模式 精确分词
seg_list = jieba.lcut(seg)
2.全模式 找出所有可能的分词结果 冗余性大
seg_list1 = jieba.lcut(seg,cut_all=True)
3.搜索引擎模式 先执行精确模式,在对其中的长词进行处理
seg_list4 = jieba.lcut(text,cut_all=True)
seg_list5 = jieba.lcut_for_search(text)
# 3.中文分词 jieba
# 安装jieba分词库
# 指定国内镜像安装
#1.在用户目录下新建pip文件夹
# 2.新建pip.ini文件
# 添加
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
# pip install jieba
# 导入jieba分词
import jieba
# 三种分词模式
seg = "我来到北京清华大学"
# 精确模式 精确分词
seg_list = jieba.lcut(seg)
print(seg_list)
# 全模式 找出所有可能的分词结果 冗余性大
seg_list1 = jieba.lcut(seg,cut_all=True)
print(seg_list1)
# 搜索引擎模式
seg_list2 = jieba.lcut_for_search(seg)
print(seg_list2)
#
text = '小明硕士毕业于中国科学院计算所,后在日本京都大学深造'
seg_list4 = jieba.lcut(text,cut_all=True)
print(seg_list4)
# 搜索引擎模式 先执行精确模式,在对其中的长词进行处理
seg_list5 = jieba.lcut_for_search(text)
print(seg_list5)
# nlp
4.词云(WordCloud)
# 词云展示
# 安装
# pip install wordcloud
# 本地安装python库
# 导入词云 WordCloud类
from wordcloud import WordCloud
import jieba
import imageio
# 绘制词云
# text = 'He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish. In the first forty days a boy had been with him. But after forty days without a fish the boy’s parents had told him that the old man was now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at their orders in another boat which caught three good fish the first week. It made the boy sad to see the old man come in each day with his skiff empty and he always went down to help him carry either the coiled lines or the gaff and harpoon and the sail that was furled around the mast. The sail was patched with flour sacks and, furled, it looked like the flag of permanent defeat.'
# wc = WordCloud().generate(text)
# wc.to_file('老人与海.png')
# 三国演义小说词云绘制
# 三国演义小说分词
# 读取三国演义小说
mask = imageio.imread('china.jpg')
with open('threeking.txt','r', encoding='utf-8') as f:
words = f.read()
# print(len(words)) # 字数 55万
words_list = jieba.lcut(words)
# print(len(words_list)) # 分词后的词语数 35万
print(words_list)
# 将words_list转化成字符串
novel_words = " ".join(words_list)
print(novel_words)
# WordCloud()里面设置参数
wc = WordCloud(
font_path='msyh.ttc',
background_color='white',
width=800,
height=600,
mask=mask
).generate(novel_words)
wc.to_file('三国词云.png')
网友评论