Python 標(biāo)準(zhǔn)庫中的GUI界面 --》 turtle的簡單使用
- 導(dǎo)入turtle as
# 給turtle 起一個(gè)別名t
import turtle as t
# 設(shè)置畫筆的大小
t.pensize(10)
t.color('blue')
- 繪制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(-130,75)
t.pd()
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(50)
t.lt(180)
t.fd(50)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(50)
# 畫U
t.penup()
t.goto(-110,75)
t.pd()
t.rt(90)
t.fd(55)
t.circle(25,180)
t.fd(55)
# 畫S
t.penup()
t.goto(5,55)
t.pd()
t.circle(22,270)
# r是正數(shù)值時(shí)涕蜂,以左手邊為圓心畫弧务荆,負(fù)數(shù)以右手邊為圓心畫弧
t.circle(-22,270)
# 畫O
t.penup()
t.goto(100,35)
t.pd()
t.circle(38)
# 畫F
t.penup()
t.goto(160,70)
t.pd()
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(40)
t.lt(90)
t.fd(40)
t.lt(180)
t.fd(40)
t.lt(90)
t.fd(40)
# 畫T
t.penup()
t.goto(180,67)
t.pd()
t.lt(90)
t.fd(70)
t.lt(180)
t.fd(40)
t.lt(90)
t.fd(80)
# 讓gui界面一直顯示,所有執(zhí)行代碼要寫在此函數(shù)之前
t.done()
運(yùn)行截圖
Python常用數(shù)據(jù)類型
- 列表:與c語言中的數(shù)組很相似淑翼,可以存儲(chǔ)不同類型的數(shù)據(jù)
hero_name = ['魯班七號(hào)','韓信','李白','百里守約']
# 輸出
print(hero_name)
# 遍歷
for hero in hero_name:
print(hero)
# 添加
hero_name.append('武則天')
# 修改
hero_name[1] = 1000
# 刪除
del hero_name[1]
# 練習(xí):循環(huán)添加1看政,2邦鲫,3碘箍,4匾鸥,......10
n_num = []
for i in range(1,11):
n_num.append(i)
print(n_num)
- 字符串
1.定義形式 ' ' " "
name = 'abcdefg'
2.切片:對(duì)序列截取一部分的操作酬核,適用于列表
name = 'abcdefg'
{起始位置:終止位置:步長} 左閉右開
print(name[1:4])
# a c e g
# 全切片時(shí)可以省略初始和終止位置
print(name[::2])
3.常用方法
(1) 去兩端空格
name = ' abcdefg '
# 查看序列內(nèi)元素的個(gè)數(shù) len()
print(len(name))
name = name.strip()
print(name)
print("去空格之后:",len(name))
(2)替換
price = '$999'
price = price.replace('$','')
print(price)
(3)列表變成字符串方法 join
li = ['a','b','c','d']
li = '_'.join(li)
print(li)
print(type(li))
- 元組 tuple 和列表很像蜜另,但不能修改
a = ('zhangsan','lisi','wangwu',1000)
print(a)
print(type(a))
print(a[1])
# 元組需要注意的是
b = ('lisi',) #是元組
b1 = ('lisi') #不是元組
c = (1000,) #是元組
c1 = (1000) #不是元組
print(type(b)) print(type(c))
- 字典 dict java hashmap
- key-value數(shù)據(jù)結(jié)構(gòu)
- 定義形式 {}
info = {'name':'李四', 'age':34, 'addr':'重慶市渝北區(qū)'}
# 長度
print(len(info))
print(info)
3.方法
# 1.字典的訪問
print(info['name'])
# 2.修改
info['addr'] = '北京市朝陽區(qū)'
print('修改后字典',info)
# 3.增加
info['sex'] = 'female'
print('增加后字典',info)
# 獲取字典中所有的鍵
print(info.keys())
# # 獲取字典中所有的值
print(info.values())
# 獲取字典中所有的key-value
print(info.items())
運(yùn)行截圖
- 列表變?yōu)樽值?/li>
d = [('name', '李四'), ('age', 34), ('addr', '北京市朝陽區(qū)'), ('sex', 'female')]
# 列表變?yōu)樽值?d1 = dict(d)
print(d1)
# 遍歷字典
for k, v in info.items():
print(k, v)
運(yùn)行截圖
- 集合,無序嫡意,不重復(fù)
set1 = {'zhangsan', 'lisi', 222}
print(type(set1))
# 遍歷
for x in set1:
print(x)
運(yùn)行截圖
掌握Python常用數(shù)據(jù)類型和語法
- 列表的排序
li = []
for i in range(10):
li.append(i)
print(li)
from random import shuffle
shuffle(li)
print("隨機(jī)打亂的列表:",li)
li.sort(reverse=True)
print("排序后的列表:",li)
運(yùn)行截圖
str_info = [
{"name":"zhangsan","age":18},
{"name":"lisi","age":32},
{"name":"wangwu","age":2},
{"name":"zhaoliu","age":45},
]
print("排序前:",str_info)
# def 函數(shù)名():
# 函數(shù)體
def sort_by_age(x):
return x['age']
# key= 函數(shù)名 --- 按照什么方式進(jìn)行排序
# 根據(jù)年齡大小進(jìn)行正序排序
str_info.sort(key=sort_by_age, reverse=True)
print("排序后:",str_info)
# 練習(xí) 按照工資大小進(jìn)行排序
name_info_list = [
("張三",5465),
("李四",6545),
("王五",5674),
("趙六",9676),
]
def sort_by_num(x):
return x[1]
name_info_list.sort(key=sort_by_num)
print(name_info_list)
運(yùn)行截圖
- 調(diào)用.py中的方法
- Project1.py
def say_hello(name):
print('hello,{}'.format(name))
say_hello("重慶師范")
- Project2.py
# 導(dǎo)入Project5模塊
import Project5
Project5.say_hello("佩奇")
運(yùn)行截圖
- 本地文件讀取——使用open內(nèi)置函數(shù)進(jìn)行文件讀取
f = open(file='./novel/threekingdom.txt',mode='r',encoding='utf-8')
data = f.read()
f.close()
# data = open(file='./novel/threekingdom.txt',mode='r',encoding='utf-8').read()
print(data)
- with as 上下文管理器
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
data = f.read()
print(data)
- 寫入文本
txt = 'i like Python'
with open('./chongqinshifan.html','w',encoding='utf-8') as f:
f.write(txt)
- 中文分詞 jieba
# 安裝jieba分詞庫
# 指定國內(nèi)鏡像安裝
# 在用戶目錄下新建pip文件夾
# 新建pip.ini 文件
# 添加
# pip install jieba
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
# pip install jieba
添加jieba
# 導(dǎo)入jieba分詞
import jieba
# 三種分詞模式
sea = "我來到北京清華大學(xué)"
# 精確模式 精確分詞
seg_list = jieba.lcut(sea)
print(seg_list)
# 全模式 找出所有可能胡分詞結(jié)果 亢余性大
seg_list1 = jieba.lcut(sea,cut_all=True)
print(seg_list1)
# 搜索引擎模式
seg_list2 = jieba.lcut_for_search(sea)
print(seg_list2)
text = "小明碩士畢業(yè)于中國科學(xué)院計(jì)算所举瑰,后在日本京都大學(xué)深造"
seg_list3 = jieba.lcut(text,cut_all=True)
print(seg_list3)
# 搜索引擎模式 先執(zhí)行精確模式,在對(duì)其中的長詞進(jìn)行處理
seg_list4 = jieba.lcut_for_search(text)
print(seg_list4)
- 三國演義小說分詞
import jieba
# 三國演義小說分詞
# 讀取三國演義小說
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
words = f.read()
print(len(words)) # 字?jǐn)?shù) 55萬
words_list = jieba.lcut(words)
print(len(words_list)) # 分詞后的詞語數(shù) 35萬
print(words_list)
- 詞云的展示
- 安裝
# pip install wordcloud
# 本地安裝Python庫的展示
添加wordcloud
- 導(dǎo)入詞云文件
import jieba
from wordcloud import WordCloud
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')
老人與海詞云
- 三國演義小說詞云繪制
# 三國演義小說分詞
# 讀取三國演義小說
with open('./novel/threekingdom.txt','r',encoding='utf-8') as f:
words = f.read()
print(len(words)) # 字?jǐn)?shù) 55萬
words_list = jieba.lcut(words)
print(len(words_list)) # 分詞后的詞語數(shù) 35萬
print(words_list)
# 將words_list轉(zhuǎn)換成字符串
mask = imageio.imread('./image/china.jpg')
novel_words = " ".join(words_list)
print(novel_words)
# Wordcloud()里面設(shè)置參數(shù)
wac = WordCloud(
font_path = 'msyh.ttc',
background_color = 'white',
width=800,
height=600,
mask=mask
).generate(novel_words)
wac.to_file('三國演義.png')
China
三國演義詞云