Python学习的第二天

python标准库中的GUI界面--->>>turtle

绘制NEUSOFT:
①导入turtle as t ,其中t是给起个别名
②设置画笔的大小:t.pensize(10)
③设置画笔颜色:t.color('blue')
④抬笔:t.penup()
⑤落笔:t.pendown(),可以简写为t.pd()
⑥旋转:t.left(90),t.right(145)
⑦移动画线:t.forward(80)
⑧移动位置:t.goto(-360,0)
⑨让GUI界面一直显示:t.done()

import turtle as t
#设置画笔的大小  10px
t.pensize(10)
t.color('blue')
#绘制NEUSOFT
#抬笔
t.penup()
#水平左移
t.goto(-360,0)
#落笔pendown()
t.pd()
#绘制 N
#旋转90度
t.left(90)
t.forward(80)
t.right(145)
#简写
t.fd(100)
t.lt(145)
t.fd(80)

#绘制E
t.penup()
t.goto(-200,0)
t.pd()
t.left(90)
t.fd(60)
t.right(90)
t.fd(80)
t.right(90)
t.fd(60)
t.penup()
t.goto(-260,40)
t.pd()
t.fd(60)

#绘制U
t.penup()
t.goto(-160,80)
t.pd()
t.right(90)
t.fd(65)
t.circle(26,180)
t.fd(65)

#绘制S
t.penup()
t.goto(-20,60)
t.pd()
# t.left(90)
t.circle(24,270)
#r是正数时,以左手为圆心画弧,负值是以右手为圆心画弧
#angle 是负数时代表绘制方向
t.circle(-24,270)

#绘制O
t.penup()
t.goto(100,40)
t.pd()
t.circle(45)

#绘制F
t.penup()
t.goto(150,80)
t.pd()
t.right(90)
t.fd(60)
t.penup()
t.goto(150,80)
t.pd()
t.right(90)
t.fd(90)
t.penup()
t.goto(150,40)
t.pd()
t.left(90)
t.fd(60)

#绘制T
t.penup()
t.goto(250,80)
t.pd()
t.fd(100)
t.penup()
t.goto(300,80)
t.pd()
t.right(90)
t.fd(80)
#让GUI界面一直显示,所有执行的代码要写在此函数之前
t.done()

运行结果:

Python学习的第二天_第1张图片
QQ图片20190729145937.png

python常用数据类型和语法

1、列表

与C语言中的数组很相似,只不过可以存储不同类型的数据
优点:灵活 缺点:效率低
①定义方式[]

hreo_name = ['鲁班七号','安其拉','李白','刘备']

②输出

print(hreo_name)

③遍历

for hreo in hreo_name:
    print(hreo)

④列表的访问:列表名[索引]

print(hreo_name[2])

⑤列表的添加

hreo_name.append('后裔')

⑥列表的修改

hreo_name[1] = 1000

⑦列表的删除

del hreo_name[1]

⑧列表的排序

li = []
for i in range(10):
    li.append(i)
print(li)
from random import shuffle
shuffle(li)
print('随机打乱的列表',li)
#reverse=True  降序
li.sort(reverse=True)
print('排序后的列表',li)

练习:按年龄进行排序

stu_info = [
    {'name':'zhangsan','age':18},
    {'name':'lisi','age':30},
    {'name':'wangwu','age':99},
    {'name':'tianqi','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)
QQ图片20190729191310.png

练习:创建[1,2,3.......10]这样的一个数字列表

#1、创建空列表
li = []
#2、使用for循环,在循环中添加元素值
for i in range(1,11):
    li.append(i)
    i += 1
print(li)
QQ图片20190729151046.png
2、字符串

①定义形式 ' ' ," "

name = 'abcdefg'

②切片:对序列截取一部分的操作,适用于列表
[起始位置:终止位置:步长] 左闭右开
注:全切片的时候可以省略初始和终止位置

print(name[1:4])#b c d
print(name[0:7:2])#a c e g
print(name[::2])#a c e g

③去两端空格
查看序列内元素的个数 len()

name = 'abcdefg     '
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))#str
3、元组 tuple

元组和列表很像,只不过元组不可以修改
①定义 ()

a = ('zhangsan','lisi','wangwu',1000)

②访问

print(a[1])

注:元组不支持修改
只有一个元素的元组:d = ('list',)

d = ('list',)#tuple
b = ('list')#str
c = (1000)#int
4、字典 dict

①数据结构
key-value
②定义形式{}

info = {'name':'李四','age':34,'addr':'重庆市渝北区'}

③字典的访问

print(info['name'])

④修改

info['addr'] = '北京市朝阳区'

⑤增加

info['sex'] = 'female'

⑥获取字典中所有的键

print(info.keys())

⑦获取字典中所有的值

print(info.values())

⑧获取字典中所有的key-value

print(info.items())

⑨list转换成字典

d = [('name', '李四'), ('age', 34), ('addr', '北京市朝阳区'), ('sex', 'female')]
d1 = dict(d)
print(d1)

⑩遍历字典

for k,v in info.items():
    print(k,v)
6、集合 set

无序、不重复
①定义

set1 = {'zhangsan','lisi',222}

②遍历

for x in set1:
    print(x)
7、函数

def 函数名(参数):
函数体

def say_hello(name):
    print('hello,{}'.format(name))
say_hello('重庆师范')

本地文件读取

1、python中使用open内置函数进行文件读取

f = open(file = './novel/threekingdom.txt',mode='r',encoding='utf-8')
data = f.read()
f.close()
print(data)

2、with as 上下文管理器 不用手动关闭

with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
    data = f.read()
    print(data)

写入文件

1、写成txt文件

txt = 'i like python!'
with open('python.txt','w',encoding='utf-8') as f:
    f.write(txt)

2、写成html文件

text = """
 
 
    
    Title
 
 

重庆师范欢迎你

""" print(text) with open('chongqing.html','w',encoding='utf-8') as f: f.write(text)

中文分词 jieba

安装jieba分词库
指定国内镜像安装:
①在用户目录下新建pip文件夹
②新建pip.ini文件
③添加
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
④安装:pip install jieba
导入jieba分词:import jieba

三种分词模式

seg = "我来到北京清华大学"

1、精确模式 :精确分词

seg_list = jieba.lcut(seg)
print(seg_list)

2、全模式 :找出所有可能的分词结果 ,冗余性大

seg_list1 = jieba.lcut(seg,cut_all=True)
print(seg_list1)

3、搜索引擎模式

seg_list2 = jieba.lcut_for_search(seg)
print(seg_list2)

4、搜索引擎模式,先执行精确模式,在对其中的长词进行处理

text = '小明硕士毕业于中国科学院计算所,后在日本京都大学深造'
seg_list4 = jieba.lcut(text,cut_all=True)
print(seg_list4)
seg_list5 = jieba.lcut_for_search(text)
print(seg_list5)

三国演义小说分词

import jieba
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)

词云展示

①安装:pip install wordcloud
②本地安装Python库
③导入词云:from wordcloud import WordCloud
1、老人与海词云绘制

from wordcloud import WordCloud
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')
运行结果:
Python学习的第二天_第2张图片
image.png

2、三国演义小说词云绘制

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)
    #将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学习的第二天_第3张图片
image.png

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