Python第二天

turtle库学习(海龟图形)

导入turtle库
as是给起turtle库起一个别名
import turtle as t

1. 方法介绍

  • forward() | fd():向前移动指定的距离。
    t.forward(20)
  • backward() | bk() | back():向后移动指定的距离。
    t.backward(20)
  • right() | rt() , left() | lt() :以角度单位转动。
  • goto() | steps() | setposition():移动到绝对位置,如果笔落下,画线,不改变方向。
  • circle():绘制一个给定半径的圆。
    t.circle(radius,extent,steps)
    radius:半径,如果值为正则逆时针,负数为顺时针。
    extent:程度; 限度; 大小; 面积; 范围,一个数字。
    steps:执行的步数。
  • t.done():让GUI界面一直显示,所有代码都要放在它之前实行。
  • t.pensize() , t.color("blue"):设置画笔的粗细程度以及画笔的颜色。

2. 绘制实例

import turtle as t
t.pensize(10)
t.color('blue')
# 绘制N
t.penup()
t.goto(-300, 0)
t.pd()
t.left(90)
t.forward(80)
t.right(145)
t.fd(100)
t.lt(145)
t.fd(80)
# 绘制E
t.penup()
t.goto(-170, 0)
t.pd()
t.left(90)
t.fd(50)
t.right(90)
t.fd(80)
t.right(90)
t.fd(50)
t.penup()
t.goto(-220, 40)
t.pd()
t.fd(50)
# 绘制U
t.penup()
t.goto(-145, 20)
t.pd()
t.left(90)
t.fd(60)
t.penup()
t.goto(-145, 20)
t.pd()
t.circle(-22, -180)
t.penup()
t.goto(-99, 20)
t.pd()
t.right(90)
t.right(90)
t.fd(60)
# 绘制S
t.penup()
t.goto(-35, 60)
t.pd()
t.circle(21, 270)
# r为正数,以左手边为圆心,为负数,以右手边为圆心
t.circle(-21, 270)
# 绘制O
t.penup()
t.goto(65, 40)
t.pd()
t.circle(40)
# 绘制F
t.penup()
t.goto(90, 0)
t.pd()
t.right(90)
t.left(90)
t.fd(80)
t.right(90)
t.fd(50)
t.penup()
t.goto(90, 40)
t.pd()
t.fd(50)
# 绘制T
t.penup()
t.goto(160, 80)
t.pd()
t.fd(60)
t.penup()
t.goto(190, 80)
t.pd()
t.right(90)
t.fd(80)
# 让gui界面一直显示,所有执行的代码要写在此函数之前
t.done()
Python第二天_第1张图片
image.png

python中常用的数据类型

列表: 与c语言中的数组很相似, 只不过可以存储不同类型的数据

1. 优缺点

优点:灵活 ,缺点: 效率低

2. 定义方式

列表格式[] 例如:student = ['xmx','wj','yys','chy','ty']
遍历
for hero in student:
print(hero)
xmx wj yys chy ty

常见操作

  • 列表访问:列表名[索引] print(student[2])
  • 添加 append() student('wz')
  • 修改 student[1] = xxx
  • 删除 del del student[1]

本地文件读写

1. 文件读取

  • 使用open内置函数进行读取
    with open("xmx.txt","r",encoding="utf-8") as f:
    data = f.read()
    print(data)
  • 使用open内置函数进行写
    txt = 'xmx666'
    with open('python.txt','w', encoding='utf-8') as f:
    f.write(txt)

结巴分词

  • 默认模式,试图将句子最精确地切开,适合文本分析
  • 全模式,把句子中所有的可以成词的词语都扫描出来,适合搜索引擎


    Python第二天_第2张图片
    image.png

python词云

  • pip install wordcloud
    本地安装python库
    导入词云 WordCloud类
from wordcloud import WordCloud
import jieba
import imageio

三国演义词云

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)
    novel_words = " ".join(words_list)
    print(novel_words)
    wc = WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        mask=mask
    ).generate(novel_words)
    wc.to_file('三国词云.png')
Python第二天_第3张图片
image.png

你可能感兴趣的:(Python第二天)