自学python 4.

1.li = ["alex","tom","mike","god","merffy"]
(1)a = len(li)
print(a)
(2)li.append("seven")
print(li)
(3)li.insert(0,"tony")
print(li)
(4)li[1] = li[1].replace("tom","kelly")
print(li)
(5)l2 = [1,"a",3,4,"heart"]
li.extend(l2)
print(li)
(6)s = "qwert"
li.extend(s)
print(li)
(7)li.remove("tom")
print(li)
(8)a = li.pop(1)
print(a, li)
(9)li.pop(1) and li.pop(1) and li.pop(1)
print( li)
(10)li.reverse()
print(li)
(11)a = li.count("alex")
print(a) 2.li = [1,3,2,"a",4,"b",5,"c"]
(1)l1 = li[:3]
print(l1)
(2)l2 = li[3:6]
print(l2)
(3)l3 = li[::2]
print(l3)
(4)l4 = li[1:6:2]
print(l4)
(5)l4 = li[7:6:-1]
print(l4)
(6)l4 = li[5:0:-2]
print(l4) 3.lis = [2,3,"k",["qwe",20,["k1",["tt",3,"1"]],89],"ab","adv"]
(1)lis[3][2][1][0] = lis[3][2][1][0].upper()
print(lis) lis[3][2][1][0] = lis[3][2][1][0].replace("t","T")
print(lis) lis[3][2][1][0] = "TT"
print(lis) (2)lis[3][2][1][1] = "100"
lis[1] = "100"
print(lis) lis[3][2][1][1] = str(lis[3][2][1][1]+97)
lis[1] = str(lis[1]+97)
print(lis) (3)lis[3][2][1][2] = int(lis[3][2][1][2])+100
print(lis) lis[3][2][1][2] = int(lis[3][2][1][2].replace("1","101"))
print(lis) lis[3][2][1][2] = int(lis[3][2][1][2] + "01")
print(lis) 4.li = [ "alex","eric","rain"] #输出alex_eric_rain
s = ""
for c in li:
s = s + c + "_"
print(s[:-1]) 5.列出索引:
li = [ "alex","eric","rain","mike","merffy"]
for c in range(len(li)):
print(c) 6.100以内偶数列表:
lst = []
for c in range(1,100):
if c % 2 == 0:
lst.append(c)
print(lst) 7.50以内被3整除:
lst = []
for c in range(1,50):
if c % 3 == 0:
lst.append(c)
print(lst) 8.100到1倒序:
for c in range(100,0,-1):
print(c) count = 100
while count > 0:
print(count)
count -= 1 9.100~10偶数倒序列表,能被4整除的留下:
lst = []
for c in range(100,9,-2):
lst.append(c)
for el in lst:
if el % 4 !=0:
lst.remove(el)
print(lst) lst = []
for c in range(100,9,-2):
lst.append(c)
lst2 = []
for el in lst:
if el % 4 ==0:
lst2.append(el)
print(lst2) 10.1-30列表,能被3整除的改为*
lst = []
for c in range(1,31):
lst.append(c)
lst2 = []
for el in lst:
if el%3 == 0:
el = "*"
lst2.append(el)
print(lst2) 11.移除空格,找出a/A开头c结尾的元素:
li = ["TaiBai","alexC","AbC","egon","ri TiAn","WuSir","aqc"]
lst = []
for el in li:
el = el.replace(" ","")
if el.startswith("a" or "A") and el.endswith("c"):
lst.append(el)
print(lst) 12.敏感词:
li = ["苍老师","武藤兰","东京热","波多野结衣"]
l2 = []
a = input("请输入内容:")
for x in li:
if x in a:
a = a.replace(x,"*" * len(x))
print(a) 13.遍历,小写,二维列表:
li = [1,2,3,"A",[4,5,"B"],6]
for c in li:
if type(c) == list:
for f in c:
if type(f) == str:
print(f.lower())
else:
print(f)
else:
if type(c) == str:
print(c.lower())
else:
print(c) 14.成绩录入列表并求平均值:
li = []
while 1:
a = input("请输入姓名和成绩(姓名_成绩),q退出:")
if a.upper() == "Q":
break
li.append(a)
sum = 0
for x in li:
b = x.split("_")
sum += int(b[1])
print(sum / len(li)) 15.敲7:
lis = []
n = int(input("请输入最后的数字:"))
for c in range(1,n):
if c % 7 == 0:
c = "敲"
if '7' in str(c):
c = "敲"
lis.append(c)
print(lis) 16.心动女生:
lst = []
for count in range(10,0,-1):
a = input("请输入您心仪的女生(还可以输入%d个):" % count)
lst.append(a)
print("您当前心仪的女生有:")
a = 1
for el in lst:
print("编号:%s,名字:%s" % (a,el))
a += 1
print("请输入你更喜欢的3个女生的编号:")
l2 = []
for f in range(3):
c = int(input("请输入第%s个编号:" % (f+1)))
l2.append(lst[c-1])
print("更喜欢的新鲜出炉:")
b = 1
for el in l2:
print("编号:%s,名字:%s" % (b,el))
b += 1
x = int(input("最后3选1,输入你最喜欢的女生编号:"))
print("你最喜欢的是:" + l2[x-1])
上一篇:hihoCoder 网络流四·最小路径覆盖


下一篇:mysql-python socket连接错误