# -*- coding: utf-8 -*-
"""
第四次 字典 json類型 練習(xí)題
第五次 字符串分割钙蒙、索引和切片練習(xí)題
第六次 邏輯運(yùn)算練習(xí)題
"""
'''
第四次 字典 json類型 練習(xí)題
'''
# 導(dǎo)入Json
import json
# 定義一個(gè)空字典dict_a,空字典dict_b
dict_a = {}
dict_b = {}
# 給dict_a 添加3個(gè)key a1,a2,a3分別對(duì)應(yīng)的值為b1,b2,b3
dict_a["a1"] = "b1"
dict_a["a2"] = "b2"
dict_a["a3"] = "b3"
# 獲取dict_a所有的key,命名變量ks,打印輸出ks及ks的數(shù)據(jù)類型
ks = dict_a.keys()
print("ks ", ks)
print("type(ks) ", type(ks))
# 打印dict_a所有的value 命名變量vs,打印輸出vs及vs的數(shù)據(jù)類型
vs = dict_a.values()
print("vs ", vs)
print("type(vs) ", type(vs))
# 執(zhí)行代碼print(dict_a.items()) 觀察輸出結(jié)果
print(dict_a.items())
# 將a1和a3對(duì)應(yīng)的值對(duì)換
temp = dict_a["a1"]
dict_a["a1"] = dict_a["a3"]
dict_a["a3"] = temp
# 打印輸出dict_a
print(dict_a)
# 刪除字典dict_a中a1對(duì)應(yīng)的值
dict_a.pop("a1")
# 打印輸出dict_a
print(dict_a)
# 將此時(shí)的dict_a數(shù)據(jù)更新到dict_b
dict_b.update(dict_a)
# 打印dict_b 并觀察a1和a4是否在dict_b中
print(dict_b)
# a1如不存在dict_b中,輸入以下代碼 a1=dict_b.get('a1') 并打印變量a1
a1 = dict_b.get('a1')
print(a1)
# 將13題變量a1 添加到dict_b中,key為'a1'
dict_b["a1"] = a1
# a4如不存在dict_b中,將a4對(duì)應(yīng)的值默認(rèn)為'null',并添加到dict_b中,key為'a4'
a4 = 'null'
dict_b["a4"] = a4
# 打印dict_b及其數(shù)據(jù)類型
print(dict_b, type(dict_b))
# 將dict_b轉(zhuǎn)化為json類型 命名為變量 json_c
json_c = json.dumps(dict_b)
# 打印json_c及其數(shù)據(jù)類型 觀察16題打印結(jié)果和18題結(jié)果 將不同之處指明
print(json_c, type(json_c))
# 將json_c轉(zhuǎn)換為字典類型 命名為dict_c 打印輸出 dict_c及其數(shù)據(jù)類型
dict_c = json.loads(json_c)
print(dict_c, type(dict_c))
'''
第五次 字符串分割祠乃、索引和切片練習(xí)題
'''
# 理解索引這個(gè)會(huì)在之后經(jīng)常用到
# 定義字符串妄呕、例如:str1 = 'http://www.reibang.com/u/a987b338c373'字符串內(nèi)容為自己的首頁(yè)連接
str_1 = "http://www.reibang.com/u/85c62640043d"
# 輸出自己的簡(jiǎn)書(shū)id(u/之后的內(nèi)容--a987b338c373)
print(str_1.split("u/")[1])
# 設(shè)s = "abcdefg"恃鞋, 則下列值為
# s[3] s[2:4]
# s[:5] s[3:]
# s[::-1] s[::2]
s = "abcdefg"
print(s[3], "d")
print(s[2:4], "cd")
print(s[:5], "abcde")
print(s[3:], "defg")
print(s[::-1], "gfedcba")
print(s[::2], "aceg")
# 定義列表:list1 = [1,2,3,4,5,6,7],則下列值為
# list1[3] list1[2:4]
# list1[:5] list1[3:]
# list1[::-1] list1[::2]
list1 = [1, 2, 3, 4, 5, 6, 7]
print(list1[3], 4)
print(list1[2:4], [3, 4])
print(list1[:5], [1, 2, 3, 4, 5])
print(list1[3:], [4, 5, 6, 7])
print(list1[::-1], [7, 6, 5, 4, 3, 2, 1])
print(list1[::2], [1, 3, 5, 7])
# 定義元組:touple1 = (1,2,3,4,5,6,7),則下列值為
# touple1[3] touple1[2:4]
# touple1[:5] touple1[3:]
# touple1[::-1] touple1[::2]
touple1 = (1, 2, 3, 4, 5, 6, 7)
print(touple1[3], 4)
print(touple1[2:4], (3, 4))
print(touple1[:5], (1, 2, 3, 4, 5))
print(touple1[3:], (4, 5, 6, 7))
print(touple1[::-1], (7, 6, 5, 4, 3, 2, 1))
print(touple1[::2], (1, 3, 5, 7))
# 對(duì)之前學(xué)習(xí)過(guò)得集中基本類型及其方法進(jìn)行復(fù)習(xí)卓缰,重中之重理解索引和切片
'''
第六次 邏輯運(yùn)算練習(xí)題
'''
# 下列表達(dá)式邏輯運(yùn)算后的結(jié)果為?(盡量直接思考解答,可以用代碼測(cè)試結(jié)果)
print("True and True", True and True, True)
print("False and True", False and True, False)
print("1 == 1 and 2 == 1", 1 == 1 and 2 == 1, False)
print("\"test\" == \"test\"", "test" == "test", True)
print("1 == 1 or 2 != 1", 1 == 1 or 2 != 1, True)
print("True and 1 == 1", True and 1 == 1, True)
print("False and 0 != 0", False and 0 != 0, False)
print("True or 1 == 1", True or 1 == 1, True)
print("\"test\" == \"testing\"", "test" == "testing", False)
print("1 != 0 and 2 == 1", 1 != 0 and 2 == 1, False)
print("\"test\" != \"testing\"", "test" != "testing", True)
print("\"test\" == 1", "test" != "testing", True)
print("not (True and False)", not (True and False), True)
print("not (1 == 1 and 0 != 1)", not (1 == 1 and 0 != 1), False)
print("not (10 == 1 or 1000 == 1000)", not (10 == 1 or 1000 == 1000), False)
print("not (1 != 10 or 3 == 4)", not (1 != 10 or 3 == 4), True)
print("not (\"testing\" == \"testing\" and \"Zed\" == \"Cool Guy\")",
not ("testing" == "testing" and "Zed" == "Cool Guy"), True)
print("1 == 1 and not (\"testing\" == 1 or 1 == 0)", 1 == 1 and not ("testing" == 1 or 1 == 0), True)
print("3 == 3 and not (\"testing\" == \"testing\" or \"Python\" == \"Fun\")",
3 == 3 and not ("testing" == "testing" or "Python" == "Fun"), False)
【Python爬蟲(chóng)】第一周練習(xí)(二)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門卑吭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人马绝,你說(shuō)我怎么就攤上這事豆赏。” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵掷邦,是天一觀的道長(zhǎng)白胀。 經(jīng)常有香客問(wèn)我,道長(zhǎng)耙饰,這世上最難降的妖魔是什么纹笼? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮苟跪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蔓涧。我一直安慰自己件已,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布元暴。 她就那樣靜靜地躺著篷扩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪茉盏。 梳的紋絲不亂的頭發(fā)上鉴未,一...
- 那天,我揣著相機(jī)與錄音鸠姨,去河邊找鬼铜秆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛讶迁,可吹牛的內(nèi)容都是我干的连茧。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼巍糯,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼啸驯!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起祟峦,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤罚斗,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后宅楞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體针姿,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年咱筛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了搓幌。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布撤蟆,位于F島的核電站,受9級(jí)特大地震影響堂污,放射性物質(zhì)發(fā)生泄漏家肯。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一盟猖、第九天 我趴在偏房一處隱蔽的房頂上張望讨衣。 院中可真熱鬧,春花似錦式镐、人聲如沸反镇。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)歹茶。三九已至,卻和暖如春你弦,著一層夾襖步出監(jiān)牢的瞬間惊豺,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓领迈,卻偏偏與公主長(zhǎng)得像彻磁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子狸捅,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 報(bào)了網(wǎng)易云課堂的麻瓜編程的《四周學(xué)會(huì)用Python做爬蟲(chóng)網(wǎng)站》尘喝,看中的是這門課的實(shí)戰(zhàn)性強(qiáng)磁浇,可以自己做點(diǎn)東西出來(lái)。第...
- 笨方法學(xué)python電子書(shū)里下面習(xí)題27-34 習(xí)題27:記住邏輯關(guān)系朽褪; --coding:utf-8-- 練習(xí)2...
- 進(jìn)入麻瓜編程用python學(xué)爬蟲(chóng)第一周第二課的練習(xí)題了缔赠,自己鼓搗了半天衍锚,無(wú)解,最后可恥的看了答案:恍然大悟后發(fā)現(xiàn)自...