Python实训---第二天

Python实训---第二天

turtle简易GUI绘图

import  turtle as t
t.pensize(10)
#设置画笔颜色为蓝色
t.color('pink')
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(-130, 0)
t.pd()

t.left(90)
t.forward(40)
t.right(90)
t.forward(80)

t.penup()
t.goto(-130,40)
t.pd()
t.left(90)
t.forward(40)

t.penup()
t.goto(-130,80)
t.pd()
t.forward(40)
t.penup()
t.goto(-100, 80)
t.pd()
t.left(90)
t.forward(60)

t.penup()
t.goto(-50, 80)
t.pd()
t.forward(60)

t.penup()
t.goto(-100, 20)
t.pd()

t.circle(25,180)
t.penup()
t.goto(20, 60)
t.pd()
t.circle(22,270)
t.circle(-22,270)
t.penup()
t.goto(100, 60)
t.pd()

t.circle(22, 180)
t.fd(40)
t.penup()
t.goto(100, 20)
t.pd()
t.circle(-22, 180)

t.penup()
t.goto(100, 20)
t.pd()
t.fd(40)
t.penup()
t.goto(140, 0)
t.pd()

t.forward(80)

t.penup()
t.goto(180,40)
t.pd()
t.left(90)
t.forward(40)

t.penup()
t.goto(180,80)
t.pd()
t.forward(40)
t.penup()
t.goto(230, 0)
t.pd()

t.right(90)
t.forward(80)
t.penup()
t.goto(255,80)
t.pd()
t.lt(90)
t.forward(50)
t.done()

列表

①列表名

print(hero_name[2])

②添加append

hero_name.append('后羿')
print('添加后的列表',hero_name)

③修改

hero_name[1] = 1000
print('修改后的列表',hero_name)

④删除

del hero_name[1]
print('删除后的列表',hero_name)

#定义方式[]
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)

字典

# 字典 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)
# 4.获取字典中所有的键
print(info.keys())
# 5.获取字典中所有的值
print(info.values())
# 6.获取字典中所有的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 = {'zhangshan','lili',222}
print(type(set1))
print(set1)

# 列表的排序
#shuffle() 方法将序列的所有元素随机排序。
li = []
for i in range(10):
     li.append(i)
print(li)
from random import shuffle
shuffle(li)
print('随机打乱的列表',li)
li.sort(reverse=True)  #排序,正序
stu_info = [
   {"name":'zhangsan',"age":18},
   {"name":'lisi',"age":23},
   {"name":'wangwu',"age":15},
  {"name":'zhaoliu',"age":11},
]
print('排序前',stu_info)

# def 函数名(参数)
#        函数体
def sort_by_age(x):
     return x['age']
#  根据年龄大小排序
stu_info.sort(key=sort_by_age)
print('排序后的列表',stu_info)

# 练习
name_info_list = [
     ('张三',4500),
     ('李四',9900),
     ('王五',2000),
     ('赵六',5500)
 ]

# 根据元组第二个元素进行正序排序
def sort_by_money(x):
     return x[1]
name_info_list.sort(key=sort_by_money)
print('排序后的列表',name_info_list)

中文分词

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)

词云

# 词云展示
#安装
# 在Terminal中输入 pip install 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()
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')

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