第四次作業(yè)
字典亲桦、 json類型 練習題
1.導入json模塊
2.定義一個空字典dict_a,空字典dict_b
3.給dict_a 添加3個key a1,a2,a3分別對應的值為b1,b2,b3
4.獲取dict_a所有的key,命名變量ks,打印輸出ks及ks的數(shù)據(jù)類型
5.打印dict_a所有的value 命名變量vs,打印輸出vs及vs的數(shù)據(jù)類型
6.執(zhí)行代碼print(dict_a.items()) 觀察輸出結果
import json
dict_a={}
dict_b={}
dict_a['a1']='b1'
dict_a['a2']='b2'
dict_a['a3']='b3'
ks=dict_a.keys()
print(ks)
print('type:',type(ks))
vs=dict_a.values()
print(vs)
print('type:',type(vs))
print(dict_a.items())
7.將a1和a3對應的值對換
8.打印輸出dict_a
9.刪除字典dict_a中a1對應的值
10.打印輸出dict_a
a1_value=dict_a['a1']
dict_a['a1']=dict_a['a3']
dict_a['a3']=a1_value
print(dict_a)
dict_a.pop('a1')
print(dict_a)
11.將此時的dict_a數(shù)據(jù)更新到dict_b
12.打印dict_b 并觀察a1和a4是否在dict_b中
13.a1如不存在dict_b中,輸入以下代碼 a1=dict_b.get('a1') 并打印變量a1
14.將13題變量a1 添加到dict_b中,key為'a1'
15.a4如不存在dict_b中,將a4對應的值默認為'null',并添加到dict_b中,key為'a4'
dict_b=dict_a
print(dict_b)
if not 'a1' in dict_b:
a1 = dict_b.get('a1')
print(a1)
dict_b['a1']='a1'
if not 'a4' in dict_b:
dict_b.get('a4')
dict_b['a4']='a4
16.打印dict_b及其數(shù)據(jù)類型
17.將dict_b轉(zhuǎn)化為json類型 命名為變量 json_c
18.打印json_c及其數(shù)據(jù)類型 觀察16題打印結果和18題結果 將不同之處指明
19.將json_c轉(zhuǎn)換為字典類型 命名為dict_c 打印輸出 dict_c及其數(shù)據(jù)類型
print(dict_b,type(dict_b))
json_c=json.dumps(dict_b)
print(json_c,type(json_c))
dict_c=json.loads(json_c)
print(dict_c,type(dict_c))
第五次作業(yè)
字符串分割焚辅、索引和切片練習題
1.定義字符串(例如:str1 = 'http://www.reibang.com/u/a987b338c373')淑玫,字符串內(nèi)容為自己的首頁連接,輸出自己的簡書id(u/之后的內(nèi)容霎褐,例中為a987b338c373)
str1='http://www.reibang.com/u/f46d8c326513'
pos=str1.find('/u/')
id=str1[pos+3:]
print(id)
2.設s = "abcdefg"址愿, 則下列值為
? ?s[3] ? ?s[2:4]
? ?s[:5] ? ?s[3:]
? ?s[::-1]??s[::2]
s = "abcdefg"
print(s[3])
print(s[2:4])
print(s[:5])
print(s[3:])
print(s[::-1])
print(s[::2])
3.定義列表: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])
print(list1[2:4])
print(list1[:5])
print(list1[3:])
print(list1[::-1])
print(list1[::2])
4.定義元組: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])
print(touple1[2:4])
print(touple1[:5])
print(touple1[3:])
print(touple1[::-1])
print(touple1[::2])
第六次作業(yè)
邏輯運算練習題
下列表達式邏輯運算后的結果為?
1.True and True
2.False and True
3.1 == 1 and 2 == 1
4."test" == "test"
5.1 == 1 or 2 != 1
6.True and 1 == 1
7.False and 0 != 0
8.True or 1 == 1
9."test" == "testing"
10.1 != 0 and 2 == 1
11."test" != "testing"
12."test" == 1
13.not (True and False)
14.not (1 == 1 and 0 != 1)
15.not (10 == 1 or 1000 == 1000)
16.not (1 != 10 or 3 == 4)
17.not ("testing" == "testing" and "Zed" == "Cool Guy")
18.1 == 1 and not ("testing" == 1 or 1 == 0)
19.3 == 3 and not ("testing" == "testing" or "Python" == "Fun")
print(True and True)
print(False and True)
print(1 == 1 and 2 == 1)
print("test" == "test")
print(1 == 1 or 2 != 1)
print(True and 1 == 1)
print(False and 0 != 0)
print(True or 1 == 1)
print("test" == "testing")
print(1 != 0 and 2 == 1)
print("test" != "testing")
print("test" == 1)
print(not (True and False))
print(not (1 == 1 and 0 != 1))
print(not (10 == 1 or 1000 == 1000))
print(not (1 != 10 or 3 == 4))
print(not ("testing" == "testing" and "Zed" == "Cool Guy"))
print(1 == 1 and not ("testing" == 1 or 1 == 0))
print(3 == 3 and not ("testing" == "testing" or "Python" == "Fun"))