Python练习题目2

时间:2021-07-17 11:47:31

1、代码

li = ["alex","eric","rain"]
line = "_".join(li)
print(line)

2、代码

#!/usr/bin/python
# -*- coding:utf-8 -*-

li = ["alec","aric","Alex","Tony","rain"]
tu = ("alec","aric","Alex","Tony","rain")
dic = {"k1":"alex","k2":"aric","k3":"Alex","k4":"Tony"}

for item in li:
item = item.strip()
if (item.startswith("a") or item.startswith("A") ) and item.endswith("c"):
print(item)

for item in tu:
item = item.strip()
if (item.startswith("a") or item.startswith("A") ) and item.endswith("c"):
print(item)

for value in dic.values():
value = value.strip()
if (value.startswith("a") or value.startswith("A") ) and value.endswith("c"):
print(value)

3、代码

li = ["alex","eric","rain"]
print(len(li))
li.append("seven")
li.insert(0,"Tony")
li[1] = "Ke"
li.remove("eric")
del li[1]
del li[2]
del li[1:3]
li.reverse()
for i in range(0,len(li)-1):
print(i,li[i])
for key,value in enumerate(li,100):
print(key,value)
for item in li:
print(item)

4、代码

li = ["hello","seven",["mon",["h","kelly"],"all"],123,446]
print(li[2][1][1])
li[2][2] = "All"
print(li)

5、代码

tu = ("alex","eric","rain")
print(len(tu))
print(tu[1])
print(tu[0:2])
for item in tu:
print(item)
for i in range(0,len(tu)-1):
print(i)
for key,item in enumerate(tu,10):
print(key,item)

6、代码

tu = ("alex",[11,22,{"k1":"v1","k2":["age","name"],"k3":(11,22,33)},44])
print("元组是一个特殊的列表,不支持增删改操作,仅支持查看操作")
print("不可以被更改")
tu[1][2]["k2"].append("Seven")
print(tu)
print("不可以")

7、

#!/usr/bin/python
# -*- coding:utf-8 -*-

dic = {"k1": "v1", "k2": "v2", "k3": [11,22,33]}
for key in dic:
print(key)
for value in dic:
print(value)
for key,value in dic.items():
print(key,value)
dic.setdefault("k4","v4")
print(dic)
dic["k1"] = "alex"
print(dic)
dic["k3"].append(44)
print(dic)
dic["k3"].insert(0,18)
print(dic)

8、

s = "alex"
user_list1 = []
for i in s:
user_list1.append(i)
print(user_list1)

9、

    #!/usr/bin/python
# -*- coding:utf-8 -*-

v1 = [11,22,33,44,55,66,77,88,99,90]
k1_list = []
k2_list = []

for i in v1:
if i < 66:
k1_list.append(i)
else:
k2_list.append(i)
v2 = {}
v2["k1"] = k2_list
v2["k2"] = k1_list

for item in v2.items():
print item

10、

li = ["手机","电脑","鼠标垫","游艇"]
good_label = input("请输入需要添加的商品:")
li.append(good_label)
for key,item in enumerate(li):
print(key+1,item)
num = int(input("请输入相应的序号:"))
print(li[num-1])

11、
12、

print("一般空的内容都是False")
line = ""
if line:
print("111111")
else:
print("22222")

user_info = []
if user_info:
print("11111")
else:
print("2222")

print("真用1表示,假用0表示")
value = 0
if value:
print("11111")
else:
print("2222")

13、

intersection\difference\difference\symmetric_difference

16、

f1 = open("db","w")
for i in range(1,101):
temp = "alex-%s"%(i)+"\t"+"alex%s@live.com"%(i)+"\t"+"pwd%s\n"%i
f1.write(temp.expandtabs(10))
f1.close()

f2 = open("db","r")
data = f2.read()
f2.close()


user_list = data.split("\n")
user_info_list = []
for item in user_list:
user_info_list.append(item)

page_num = int(input("您好,请输入查看的商品列表页码:"))
if page_num <= len(user_info_list):
start = (page_num - 1) * 10
end = page_num * 10
v1 = user_info_list[start:end]
for item in v1:
print(item)

18、

for i in range(1,10):
for j in range(1,10):
if j<=i:
sum = i * j
print("%d*%d=%d\t"%(i,j,sum),end="")
else:
print("\n")
break