看代码写结果
1.看代码写结果
'''
v1 = [1,2,3,4,5]
v2 = [v1,v1,v1]
v1.append(6)
print(v1)
print(v2)
'''
'''
结果
[1, 2, 3, 4, 5, 6]
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]
'''
2.看代码写结果
v1 = [1,2,3,4,5]
v2 = [v1,v1,v1]
v2[1][0] = 111
v2[2][0] = 222
print(v1)
print(v2)
'''
结果
[222, 2, 3, 4, 5]
[[222, 2, 3, 4, 5], [222, 2, 3, 4, 5], [222, 2, 3, 4, 5]]
'''
3.看代码写结果,并解释每一步的流程。
v1 = [1,2,3,4,5,6,7,8,9]
v2 = {}
for item in v1:
if item < 6:
continue
if 'k1' in v2:
v2['k1'].append(item)
else:
v2['k1'] = [item ]
print(v2)
'''
v2 = {'k1':[6,7,8,9]}
1-5执行第一个if 后边碰到continue后边的不执行
当item=6时执行第二个if的else 此时v2 = {'k1':[6]}以后每一步都执行第二个if的append语句最终得到v2 = {'k1':[6,7,8,9]}
'''
4.简述赋值和深浅拷贝?
'''
(1) 赋值
将多个变量名指向同一个内存地址
(2) 浅拷贝
只拷贝第一层,修改拷贝后数据的第一层时不影响源数据
(可变数据类型)添加第一层数据时源数据受影响
(3) 深拷贝
不可变数据类型内存地址共用,可变数据类型重新开辟一个新的空间
'''
5.看代码写结果
import copy
v1 = "alex"
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1 is v2)
print(v1 is v3)
'''
True
True
'''
6.看代码写结果
import copy
v1 = [1,2,3,4,5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1 is v2)
print(v1 is v3)
'''
False
False
'''
7.看代码写结果
import copy
v1 = [1,2,3,4,5]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1[0] is v2[0])
print(v1[0] is v3[0])
print(v2[0] is v3[0])'''
True
True
True
'''
8.看代码写结果
import copy
v1 = [1,2,3,4,[11,22]]
v2 = copy.copy(v1)
v3 = copy.deepcopy(v1)
print(v1[-1] is v2[-1])
print(v1[-1] is v3[-1])
print(v2[-1] is v3[-1])
'''
True
False
False
'''
9.看代码写结果
import copy
v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5]
v2 = copy.copy(v1)
print(v1 is v2)
print(v1[0] is v2[0])
print(v1[3] is v2[3])
print(v1[3]['name'] is v2[3]['name'])
print(v1[3]['numbers'] is v2[3]['numbers'])
print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
'''
False
True
True
True
True
True
'''
10.看代码写结果 import copy
v1 = [1,2,3,{"name":'太白',"numbers":[7,77,88]},4,5]
v2 = copy.deepcopy(v1)
print(v1 is v2)
print(v1[0] is v2[0])
print(v1[3] is v2[3])
print(v1[3]['name'] is v2[3]['name'])
print(v1[3]['numbers'] is v2[3]['numbers'])
print(v1[3]['numbers'][1] is v2[3]['numbers'][1])
'''
False
True
True
True
True
True
False
True
False
True
False
True
'''
11.请说出下面a,b,c三个变量的数据类型。
a = ('太白金星')
b = (1,)
c = ({'name': 'barry'})
'''
str
tuple
dict
'''
12.按照需求为列表排序:
l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
从大到小排序
从小到大排序
反转l1列表
'''
l1 = [1, 3, 6, 7, 9, 8, 5, 4, 2]
l1.sort(reverse=True)
print(l1)
l1.sort()
print(l1)l1.reverse()
print(l1)
'''
13.利用python代码构建一个这样的列表(升级题):
[['','',''],['','',''],['','','_']]
'''
ls = "_"
for i in range(2):
ls = [ls,ls,ls]
print(ls)
'''
14.看代码写结果:
l1 = [1,2,]
l1 += [3,4]
print(l1)
'''
[1, 2, 3, 4]
'''
15.看代码写结果:
dic = dict.fromkeys('abc',[])
dic['a'].append(666)
dic['b'].append(111)
print(dic)
'''
{'a': [666, 111], 'b': [666, 111], 'c': [666, 111]}
'''
16.l1 = [11, 22, 33, 44, 55],请把索引为奇数对应的元素删除'''
l1 = [11, 22, 33, 44, 55]
l1 = l1[::2]
print(l1)
for i in range(1, len(l1), 2):
l1.pop(i)
print(l1)
'''
17.dic = {'k1':'太白','k2':'barry','k3': '白白', 'age': 18} 请将字典中所有键带k元素的键值对删除.
'''
dic = {'k1':'太白','k2':'barry','k3': '白白', 'age': 18}
dic1 = dic.copy()
for i in dic1:
if "k" in i:
dic.pop(i)
print(dic)
'''
18.完成下列需求:
s1 = '宝元'
将s1转换成utf-8的bytes类型。
将s1转化成gbk的bytes类型。
b = b'\xe5\xae\x9d\xe5\x85\x83\xe6\x9c\x80\xe5\xb8\x85'
b为utf-8的bytes类型,请转换成gbk的bytes类型。
'''
s1 = '宝元'
s1 = s1.encode("utf-8")
print(s1)
s1 = s1.decode("utf-8")
print(s1)
s1 = s1.encode("gbk")
print(s1)
b = b'\xe5\xae\x9d\xe5\x85\x83\xe6\x9c\x80\xe5\xb8\x85'
b = b.decode("utf-8")
print(b)
b = b.encode("gbk")
print(b)
'''19.用户输入一个数字,判断一个数是否是水仙花数。
水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数,
例如: 153 = 1** 3 + 5 ** 3 + 3 ** 3
'''
sx_num = input("请输入一个数字:")
if sx_num.isdecimal():
sum = 0
for i in sx_num:
sum += int(i)**3
if len(sx_num) == 3 and int(sx_num) == sum:
print("这是一个水仙花数!")
else:
print("这不是一个水仙花数!")
else:
print("请输入纯数字!")
'''
20.把列表中所有姓周的⼈的信息删掉(此题有坑, 请慎重):
lst = ['周⽼⼆', '周星星', '麻花藤', '周扒⽪']
结果: lst = ['麻花藤']
lst = ['周⽼⼆', '周星星', '麻花藤', '周扒⽪']
for i in range(len(lst)-1,-1,-1):
if "周" in lst[i]:
lst.pop(i)
print(lst)
21.车牌区域划分, 现给出以下车牌. 根据车牌的信息, 分析出各省的车牌持有量. (选做题)
cars = ['鲁A32444','鲁B12333','京B8989M','⿊C49678','⿊C46555','沪 B25041']
locals = {'沪':'上海', '⿊':'⿊⻰江', '鲁':'⼭东', '鄂':'湖北', '湘':'湖南'}
结果: {'⿊⻰江':2, '⼭东': 2, '上海': 1}
'''
cars = ['鲁A32444','鲁B12333','京B8989M','⿊C49678','⿊C46555','沪 B25041']
locals = {'沪':'上海', '⿊':'⿊⻰江', '鲁':'⼭东', '鄂':'湖北', '湘':'湖南'}
dic = {}
for i in cars:
if i[0] in locals.keys() and locals[i[0]] not in dic.keys(): dic[locals[i[0]]] = 1
elif i[0] in locals.keys()and locals[i[0]] in dic.keys():
dic[locals[i[0]]] += 1
print(dic)
python