Python學習第二天

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)
  • 字符串
 定義形式   ''  ""
  1. 切片 對序列截取一部分的操作娜睛,適用于列表
 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))
  1. 訪問
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())
  1. 獲取字典中所有的key-value
print(info.items())

7.列表轉換成字典

d = [('name', '李四'), ('age', 34), ('addr', '北京市朝陽區(qū)'), ('sex', 'female')]
d1 = dict(d)
print(d1)
  1. 遍歷字典
for k, v in info.items():
    print(k, v)
  • 集合
#  無序,不重復
set1 = {'zhangsan', 'lisi', 222}
print(type(set1))
  1. 遍歷
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')
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市闪盔,隨后出現(xiàn)的幾起案子弯院,更是在濱河造成了極大的恐慌,老刑警劉巖泪掀,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件听绳,死亡現(xiàn)場離奇詭異,居然都是意外死亡异赫,警方通過查閱死者的電腦和手機椅挣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來塔拳,“玉大人鼠证,你說我怎么就攤上這事】恳郑” “怎么了量九?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我荠列,道長类浪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任肌似,我火速辦了婚禮费就,結果婚禮上,老公的妹妹穿的比我還像新娘川队。我一直安慰自己受楼,他們只是感情好,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布呼寸。 她就那樣靜靜地躺著,像睡著了一般猴贰。 火紅的嫁衣襯著肌膚如雪对雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天米绕,我揣著相機與錄音瑟捣,去河邊找鬼。 笑死栅干,一個胖子當著我的面吹牛迈套,可吹牛的內容都是我干的。 我是一名探鬼主播碱鳞,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼桑李,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了窿给?” 一聲冷哼從身側響起贵白,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎崩泡,沒想到半個月后禁荒,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡角撞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年呛伴,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片谒所。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡热康,死狀恐怖,靈堂內的尸體忽然破棺而出劣领,到底是詐尸還是另有隱情褐隆,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布剖踊,位于F島的核電站庶弃,受9級特大地震影響衫贬,放射性物質發(fā)生泄漏。R本人自食惡果不足惜歇攻,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一固惯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缴守,春花似錦葬毫、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至村砂,卻和暖如春烂斋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背础废。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工汛骂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人评腺。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓帘瞭,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蒿讥。 傳聞我的和親對象是個殘疾皇子蝶念,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內容