一腹缩、定義列表:list1 = ['life','is','short'],
list2 = ['you','need','python']
完成以下幾個(gè)要求:
1)輸出python及其下標(biāo)
list1 = ['life','is','short']
list2 = ['you','need','python']
print(list2.index('python'))
2)在list2后追加 '!' , 在 'short' 后添加 ','
list1 = ['life','is','short']
list2 = ['you','need','python']
n=list1.index('short')
list1.insert(n+1,',')
print(list1)
3)將兩個(gè)字符串合并后屿聋,排序并輸出其長(zhǎng)度
list1 = ['life','is','short']
list2 = ['you','need','python']
list1.extend(list2)
print(len(list1))
print(list1,list1.sort())
4)將 'python' 改為 'python3'
list1 = ['life','is','short']
list2 = ['you','need','python']
list2[list2.index('python')]='python3'
print(list2)
4)移除之前添加的 '!' 和 ','
list1 = ['life','is','short',',']
list2 = ['you','need','python','!']
list1.remove(',')
list2.remove(list2[list2.index('!')])
print(list1,list2)
二、自己定義元組并練習(xí)常用方法(輸出元組長(zhǎng)度藏鹊、指定位置元素等)
tup1 = (1,2,3,4,5,6,7)
tup2 = ('you', 'need', 'python')
print(tup1[0])
print(len(tup2))
三润讥、定義列表:
list1 = ['life','is','short'],
list2 = ['you','need','python']
list3 = [1,2,3,4,5,3,4,2,1,5,7,9]
完成以下操作:
1)構(gòu)造集合 list_set1
list1 = ['life', 'is', 'short']
list2 = ['you', 'need', 'python']
list3 = [1, 2, 3, 4, 5, 3, 4, 2, 1, 5, 7, 9]
list_set1=set(list1+list2+list3)
print(list_set1)
2)將list1和list2合并構(gòu)造集合 list_set2
list1 = ['life', 'is', 'short']
list2 = ['you', 'need', 'python']
list3 = [1, 2, 3, 4, 5, 3, 4, 2, 1, 5, 7, 9]
list_set2=set(list1+list2)
print(list_set2)
3)輸出兩個(gè)集合的長(zhǎng)度
list_set1=set(list1+list2+list3) list_set2=set(list1+list2) print(len(list_set1),len(list_set2))
4)將兩個(gè)集合合并后移除 'python'
list_set1.update(list_set2)
list_set1.remove('python')
print(list_set1)
5)在合并后的新列表中添加 'python3'
list_set1.add('python3')
print(list_set1)