补充
- range (起始位置,终止位置,步长)是一个左闭右开的区间
turtle库的使用
- turtle是python标准库中的GUI界面
- 导入turtle时后跟as的作用相当于取一个别名
例如:import turtle as t - pensize()—设置画笔的大小
- penup()—抬笔
- pendown()—落笔
- color()—设置颜色
- left()、right()—改变画笔方向
- goto()—改变画笔位置
- forward()—设置画笔移动的长度
- done()—gui界面一直在前台显示
import turtle as t
#设置画笔的大小 10px
t.pensize(10)
#绘制logo Neusoft
#水平左移前抬笔
t.penup()
t.goto(-260,0)
#落笔
t.pendown()
#绘制 N
t.color('blue')#设置颜色
t.left(90)#旋转90度
t.forward(80)
t.right(145)
t.fd(100)
t.left(145)
t.fd(80)
#绘制 E
t.penup()
t.goto(-100,80)
t.pd()
t.left(90)
t.fd(60)
t.left(90)
t.fd(80)
t.left(90)
t.fd(60)
t.penup()
t.goto(-160,40)
#落笔
t.pd()
t.fd(60)
#绘制 U
t.penup()
t.goto(0,20)
t.pd()
t.right(90)
t.circle(-30,180)
t.fd(70)
t.penup()
t.goto(0,20)
t.pd()
t.fd(70)
#绘制 S
t.penup()
t.goto(95,60)
t.pd()
t.circle(25,270)
# #r是正数时时,以左手边为圆心画弧,负值是以右手边为圆心画弧
# #angle是负数时候,代表绘制方向
t.circle(-25,270)
#绘制 O
t.penup()
t.goto(200,30)
t.pd()
t.circle(35,360)
#绘制 F
t.penup()
t.goto(300,80)
t.pd()
t.left(90)
t.fd(50)
t.left(90)
t.fd(100)
t.penup()
t.goto(250,40)
t.pd()
t.left(90)
t.fd(50)
#绘制 T
t.penup()
t.goto(340,80)
t.pd()
t.fd(80)
t.penup()
t.goto(380,80)
t.pd()
t.right(90)
t.fd(100)
#让gui界面一直在前台,所以代码应写在此函数之前
t.done()
效果图
效果图1
列表
- 列表与C语言中的数组相似,不过可以储存不同类型的数据
- 优点:灵活 缺点:效率低
- 列表的定义方式为[]
- 列表访问—列表名[索引]
- 添加列表元素—append()
- 修改列表元素—列表名[索引]=需要修改的值
- 删除列表元素— del 列表名[索引]
#定义方式[]
hero_name=['111','222','333','444']
#输出
print(hero_name)
#遍历
for hero in hero_name:
print(hero)
#列表访问
print(hero_name[2])
#添加
hero_name.append('555')
#修改
hero_name[1]=666
#删除
del hero_name[1]
#创建空列表+使用for循环,在循环中添加元素值
sum1=[]
for i in range(1,11):
sum1.append(i)
print(sum1)
字符串
- 定义方式:" " 或' '
- 切片,对序列截取一部分的操作,适用于列表
- 切片的为字符串名[起始位置:终止位置:步长] 是一个左开右闭的区间
- 全切边的时候可忽略初始和终止位置
- strip()—字符串去空格
- len()—查看序列内元素的个数
- replace('要替换的','替换的')—替换
- join(列表名)—列表变成字符串的方法
name='123456789'
#切边
print(name[1:4])
# 加了步长的切片
print(name[0:7:2])
# 全切片
print(name[::2])
#查看序列内元素的个数 len()
print(len(name))
#去空格
name=name.strip()
#替换
price="$678"
price=price.replace('$','')
print(price)
# 列表变成字符串的方法 join
li=['a','b','c','d']
a=''.join(li)
元组
- 元组和列表很像,只不过元组不可以修改
- 关于元组需要注意:元组只有一个元素的时,后面必须加一个逗号
a=('123','456','789',987)
print(a)
print(type(a))
#访问
print(a[1])
#单个元素
b=('list',)
字典
- 定义形式 {}
- 字典是key-value数据结构
- 字典名.keys()—获取字典中所有的键
- 字典名.values()—获取字典中所有的值
- 字典名.items()—获取字典中所有的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)
集合
- 无序,不重复
如:set1 = {'zhangsan', 'lisi', 222}
列表的排序
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":'zhn',"age":30},
{"name":'gsan',"age":28},
{"name":'ngsan',"age":8}
]
print('排序前',stu_info)
def sort_by_age(x):
return x['age']
#根据年龄大小进行正序排序
stu_info.sort(key=sort_by_age)
print('排序后',stu_info)
#练习
name_info_list=[
('张三',4500),
('张二',2500),
('张一',6500),
('张大',3520),
]
def sort_by_money(x):
return x[1]
name_info_list.sort(key=sort_by_money)
print(name_info_list)
函数
- 定义函数
def 函数名(参数):
函数体 - 调用另一个文件的函数
import 文件名 - 调用函数
文件名.函数名()
本地文件读取(i/o操作)
- python中使用open内置函数进行文件读写,不需要关闭
- with as 上下文管理器
f=open(file='./novel/threekingdom.txt',mode='r',encoding='utf-8').read()
print(f)
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
data =f.read()
print(data)
#写入
txt='救救孩子吧'
with open('python.txt','w',encoding='utf-8') as f:
f.write(txt)
text = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>重庆师范欢迎你</h1>
</body>
</html>"""
print(text)
with open('chongqingshifan.html','w', encoding='utf-8') as f:
f.write(text)
中文分词(jieba)
- 通过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)
词云
- 通过pip安装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('./novel/threekingdom.txt','r', encoding='utf-8') as f:
words = f.read()
words_list = jieba.lcut(words)
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')
效果图2
效果图3