li = [1, 3, 2, "a", 4, "b", 5,"c"]
l1 = li[:3]
print(l1)
l2 = li[3:6]
print(l2)
l3 = li[:7:2]
print(l3)
l4 = li[1:6:2]
print(l4)
l5 = li[-1:]
print(l5)
l6 = li[-3:-8:-2]
print(l6)
3
lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab","adv"]
lis[3][2][1][0] = lis[3][2][1][0].upper()
# lis[3][2][1][0] = "TT"
print(lis)
lis[1] = "100"
lis[3][2][1][1] ="100"
print(lis)
# lis[3][2][1][2] = "101"
lis[3][2][1][2] = lis[3][2][1][2] +"01"
print(lis)
4
li = ["alex", "wusir", "taibai"]
# 利⽤下划线将列表的每⼀个元素拼接成字符串"alex_wusir_taibai
li1 = li[0]+"-"+li[1]+"_"+li[2]
li1.join("_")
print(li1)
#5
#利⽤for循环和range打印出下⾯列表的索引。
li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
i = 0
for i in range(len(li)):
print(li.index(li[i]))
#6
#利⽤for循环和range找出100以内所有的偶数并将这些偶数插⼊到⼀个新列表中。
list1 = []
for i in range(2,101,2):
list1.append(i)
print(list1)
#7
#利⽤for循环和range 找出50以内能被3整除的数,并将这些数插⼊到⼀个新列表中
list2 = []
for i in range(1,50):
if i%3==0:
list2.append(i)
print(list2)
#8
#利⽤for循环和range从100~1,倒序打印。
list4 = []
for i in reversed(range(1,101)):
print(i,end=" ")
list4.append(i)
print(list4)
9
利⽤for循环和range从100~10,倒序将所有的偶数添加到⼀个新列表中,然后对列
表的元素进⾏筛选,将能被4整除的数留下来
list5 = []
for i in reversed(range(10,101)):
if i%2==0:
list5.append(i)
for i in list5:
if i%4==0:
print(i,end=" ")
10
利⽤for循环和range,将1-30的数字⼀次添加到⼀个列表中,并循环这个列表,将
能被3整除的数改成*。
list6 = []
for i in range(1,31):
list6.append(i)
for i in list6:
if i%3 == 0:
list6[i-1] = "*"
print(list6)
11
查找列表li中的元素,移除每个元素的空格,并找出以"A"或者"a"开头,并以"c"结尾
的所有元素,并添加到⼀个新列表中,最后循环打印这个新列表。
li = ["TaiBai ", "alexC", "Abc ", "egon", " riTiAn", "WuSir", " aqc"]
list7 = []
for i in range(len(li)):
li[i]=li[i].strip()
for i in li:
if (i.startswith("a")or i.startswith("A")) and i.endswith("c"):
list7.append(i)
print(list7)
12
str1 = input("input your comment: ").strip()
list8=[]
li = ["苍老师", "东京热", "武藤兰", "波多野结⾐"]
for i in li:
if i in str1:
# i = len(i)*"*"
str1=str1.replace(i,len(i)*"*")
list8.append(str1)
print(list8)
#13
# 有如下列表
li = [1, 3, 4, "alex", [3, 7, 8, "TaiBai"], 5, "RiTiAn"]
# 循环打印列表中的每个元素,遇到列表则再循环打印出它⾥⾯的元素。
for i in li:
if type(i)==list:
for j in i:
print(j)
else:
print(i)