python 標準庫中的GUI界面-->turtle
- 導入turtle as 別名
import turtle as t
繪制 NEUSOFT
# 設置畫筆大小10px
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)
# fd為forward簡寫
t.lt(145)
t.fd(80)
# 繪制E
t.pu()
t.goto(-180,0)
t.pd()
t.fd(80)
t.right(90)
t.fd(60)
t.left(180)
t.fd(60)
t.left(90)
t.fd(40)
t.left(90)
t.fd(60)
t.left(180)
t.fd(60)
t.left(90)
t.fd(40)
t.left(90)
t.fd(60)
# 繪制U
t.pu()
t.goto(-95, 80)
t.pd()
t.rt(90)
t.fd(50)
t.circle(30, 180)
t.fd(50)
# 繪制S
t.penup()
# 水平左移
t.goto(34, 60)
# 落筆
t.pd()
t.circle(22, 270)
# r是正數時晃痴,以左手邊為圓心畫弧删铃,r是負數時以左手邊為圓心畫弧
# angle是負數時歼捏,代表繪制方向
t.circle(-23, 270)
# 繪制O
t.penup()
# 水平左移
t.goto(140, 40)
# 落筆
t.pd()
t.circle(40)
# 繪制F
t.pu()
t.goto(160, 0)
t.pd()
t.fd(40)
t.rt(90)
t.fd(60)
t.lt(180)
t.fd(60)
t.rt(90)
t.fd(40)
t.rt(90)
t.fd(60)
# 繪制T
t.pu()
t.goto(280,0)
t.pd()
t.lt(90)
t.fd(80)
t.lt(90)
t.fd(30)
t.lt(180)
t.fd(60)
# 讓gui界面一直顯示, 所有執(zhí)行的代碼要寫在此函數之前
t.done()
python常用數據類型
- 列表: 與c語言中的數組很相似莉恼, 只不過可以存儲不同類型的數據
優(yōu)點:靈活 ,缺點: 效率低
定義方式 []
hero_name = ['魯班七號', '安琪拉', '李白', '劉備']
# 輸出
print(hero_name)
# 遍歷
for hero in hero_name:
print(hero)
常見操作
1.列表的訪問
# 列表名[索引]
print(hero_name[2])
2.添加 append
hero_name.append('后羿')
print('添加后的列表', hero_name)
3.修改
hero_name[1] = 1000
print('修改后的列表',hero_name)
4.刪除
del hero_name[1]
print('刪除后的列表',hero_name)
練習
# 創(chuàng)建 [1, 2, 3......10] 這樣的一個數字列表
# 1.創(chuàng)建空列表
li = []
# 2.使用for 循環(huán)泳秀, 在循環(huán)中添加元素值
for i in range(1, 11):
li.append(i)
print(li)
- 字符串
定義形式 '' ""
- 切片 對序列截取一部分的操作娜睛,適用于列表
name = 'abcdefg'
name[1]
# [起始位置:終止位置:步長] 左閉右開
print(name[1:4])
# a c e g
print(name[0:7:2])
# 全切片的時候可以省略初始和終止位置
print(name[::2])
2.常用方法:
(1). 去兩端空格
name = ' abcdefg '
# 查看序列內元素的個數 len()
print(len(name))
name = name.strip()
print('去空格之后', len(name))
(2).替換
price = '$999'
price = price.replace('$','')
print(price)
(3). 列表變成字符串的方法 join
li = ['a', 'b', 'c', 'd']
a = '_'.join(li)
print(a)
print(type(a))
- 元組 tuple 元組和列表很像只不過元組不可以修改
定義 ()
a = ('zhangsan', 'lisi', 'wangwu',1000)
print(a)
print(type(a))
- 訪問
print(a[1])
2.關于元組需要注意的是 只有一個元素的元組
b = ('lisi',)
c = (1000,)
print(type(b))
print(type(c))
- 字典 dict java hashmap
key-value數據結構
# 定義形式 {}
info = {'name':'李四', 'age':34, 'addr':'重慶市渝北區(qū)'}
print(len(info))
print(info)
1.字典的訪問
print(info['name'])
2.修改
info['addr'] = '北京市朝陽區(qū)'
print('修改后字典',info)
3.增加
info['sex'] = 'female'
print('增加后字典',info)
4.獲取字典中所有的鍵
print(info.keys())
5.獲取字典中所有的值
print(info.values())
- 獲取字典中所有的key-value
print(info.items())
7.列表轉換成字典
d = [('name', '李四'), ('age', 34), ('addr', '北京市朝陽區(qū)'), ('sex', 'female')]
d1 = dict(d)
print(d1)
- 遍歷字典
for k, v in info.items():
print(k, v)
- 集合
# 無序,不重復
set1 = {'zhangsan', 'lisi', 222}
print(type(set1))
- 遍歷
for x in set1:
print(x)
1.掌控python的常用數據類型和語法
- 列表的序列
#列表的序列
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":'lisi',"age":30},
{"name":'wangwu',"age":99},
{"name":'tiaqi',"age":8},
]
print('排序前:',stu_info)
# def 函數名():
# 函數體
def sort_by_age(x):
return x['age']
# key=函數名 ---》按照什么進行排序
# 根據年齡大小進行排序
stu_info.sort(key=sort_by_age)
print('排序后:',stu_info)
- 練習
name_info_list=[
('張三',4500),
('李四',9900),
('王五',2000),
('趙六',5500),
]
#根據元組第二個元素進行排序
def sort_by_grade(x):
return x[1]
name_info_list.sort(key=sort_by_grade)
print('排序后:',name_info_list)
- 方法
def say_hello(name):
print('hello, {}'.format(name))
say_hello('重慶師范')
本地文件讀取
- python中使用open內置函數進行文件讀取
f=open(file='./novel/threekingdom.txt',mode='r',encoding='utf-8')
data=f.read()
f.close()
print(data)
- with as 上下文管理器 不用close關閉流
with open(file='./novel/threekingdom.txt',mode='r',encoding='utf-8') as f:
data=f.read()
print(data)
- 文件寫入
txt='i like python'
with open('python.txt',mode='w',encoding='utf-8') as f:
f.write(txt)
- 寫html文件
text="""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>擦大師傅</title>
</head>
<body>
擦大師傅
</body>
</html>"""
print(text)
with open('chongqingshifan.html',mode='w',encoding='utf-8') as f:
f.write(text)
3.中文分詞 jieba
- 安裝jieba分詞庫
# 指定國內鏡像安裝
#1.在用戶目錄下新建pip文件夾
# 2.新建pip.ini文件
# 添加
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
import jieba
- 三種分詞模式
# 精確模式
seg="我來到北京清華大學"
seg_list=jieba.lcut(seg)
print(seg_list)
# 全模式 找出所有分詞結果 冗余性大
seg_list1=jieba.lcut(seg,cut_all=True)
print(seg_list1)
# 搜索引擎模式 先執(zhí)行精確模式,再對其中的長詞進行處理
seg_list2=jieba.lcut_for_search(seg)
print(seg_list2)
#eg:
text = '小明碩士畢業(yè)于中國科學院計算所掘托,后在日本京都大學深造'
seg_list4 = jieba.lcut(text,cut_all=True)
print(seg_list4)
# 搜索引擎模式 先執(zhí)行精確模式瘦锹,在對其中的長詞進行處理
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))
words_list=jieba.lcut(words)
print(len(words_list)) #分詞后的詞語書
print(words_list)
詞云展示
- 本地安裝python庫
# 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()
words_list=jieba.lcut(words)
print(len(words_list)) #分詞后的詞語書
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')