【python爬虫】第一周作业(续)_顾静

第四次作业

字典、 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的数据类型
5.打印dict_a所有的value 命名变量vs,打印输出vs及vs的数据类型
6.执行代码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数据更新到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及其数据类型
17.将dict_b转化为json类型 命名为变量 json_c
18.打印json_c及其数据类型 观察16题打印结果和18题结果 将不同之处指明
19.将json_c转换为字典类型 命名为dict_c 打印输出 dict_c及其数据类型

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))

第五次作业

字符串分割、索引和切片练习题

1.定义字符串(例如:str1 = ‘http://www.jianshu.com/u/a987b338c373′),字符串内容为自己的首页连接,输出自己的简书id(u/之后的内容,例中为a987b338c373)

str1='https://www.jianshu.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])

第六次作业

逻辑运算练习题

下列表达式逻辑运算后的结果为?
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"))
    原文作者:gujingnk
    原文地址: https://www.jianshu.com/p/2c7021a0c60d
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞