练习15
tu1 = (1,)
tu2 = ('alex')
tu3 = ([1,2,3],)
print(tu1,type(tu1))
print(tu2,type(tu2))
print(tu3,type(tu3))
l1 = [1,2,3]
l2 = ['alex','wusir']
l3 = l1 + l2
print(l3)
l1 = [11, 22, 33, 44, 55, 66, 77, 88]
del l1[1::2]
print(l1)
l1 = [11, 22, 33, 44, 55, 66, 77, 88]
for i in range(len(l1)):
print(l1)
print(i)
if i % 2 == 1:
l1.pop(i)
print(l1)
print(i)
print(l1)
l2 = []
for i in range(len(l1)):
if i % 2 == 0:
l2.append(l1[i])
print(l2)
l1 = l2
print(l1)
l1 = [11, 22, 33, 44, 55, 66, 77, 88]
for index in range(len(l1)-1, -1, -1):
if index % 2 == 1:
l1.pop(index)
print(l1)
fromkeys()
dic1 = dict.fromkeys([1,2,3],'alex')
print(dic1)
dic = {'name':'alex'}
dic = dict({'name':'alex'})
print(dic)
dic1 = dict.fromkeys([1,2,3],[])
print(dic1)
dic1[1].append('alex')
print(dic1)
print(id(dic1[1]))
print(id(dic1[2]))
print(id(dic1[3]))
dic = {'key1': 'value1','key2': 'value2', 'k3':'v3', 'name': 'alex'}
for key in dic:
if 'k' in key:
dic.pop(key)
l1 = []
for key in dic:
if 'k' in key:
l1.append(key)
print(l1)
for key in l1:
dic.pop(key)
print(dic)
数据类型的转换。
'''
int str bool 三者转换
str <---> bytes
str <---> list
dict.keys() dict.values() dict.items() list()
tuple <---> list
dict ---> list
'''
s1 = 'alex wusir taibai'
l1 = s1.split()
print(l1)
l1 = ['alex', 'wusir', 'taibai', 100]
s2 = ' '.join(l1)
print(s2)
tuple <---> list
l1 = [1,2,3]
tu1 = tuple(l1)
print(tu1)
tu2 = (0,2,3)
l1 = list(tu2)
print(l1)
dic1 = {'name': 'alex', 'age': 1000}
l1 = list(dic1)
print(l1)
l2 = ['name', 'age']
dic2 = dict(l2)
print(dic2)
0 " [] () {} set() ---> bool False