str1 = 'aa'
str2 = 'bb'
list1 = [1, 2]
list2 = [10, 20]
t1 = (1, 2)
t2 = (10, 20)
dict1 = {'name': 'python'}
dict2 = {'age': 20}
# 1.len()
print(len(dict1))
# 2.del /del()
del list1[0]
print(list1)
del dict1['name']
print(dict1)
# 3.max()/min()
str1 = 'abcdefg'
print(min(str1))
# 4. range(start, end, step), 不包括end
for num in range(10):
print(num)
print(range(10)) # 返回的为range可以迭代的对象,range(0, 10)
# 5. enumerate(可遍历对象, start=0), 返回结果为元组
list1 = ['a', 'b', 'c', 'd', 'e']
for i in enumerate(list1, start=1):
print(i)
# type transfer
list1 = [1, 2, 3, 4, 5]
s1 = {10, 20, 30, 50}
t1 = ('a', 'b', 'c', 'd', 'e', 'a')
# tuple():转换成元组
print(type(tuple(list1)))
# list() : 转换成列表
print(type(list(s1)))
# set(): 转换成集合
print(type(set(t1)))
print(set(t1))
# 列表推导式
list1 = []
i = 0
while i < 10: # while 实现
list1.append(i)
i = i + 1
print(list1)
list1 = []
for i in range(0, 10, 2): # for实现
list1.append(i)
print(list1)
list1 = [i for i in range(15)] # for列表推导式实现,一行代码完成
print(list1)
list2 = []
list2 = [i for i in range(10) if i % 2 == 0] # 改写成带If的列表推导式
print(list2)
list1 = []
list1 = [(i, j) for i in range(1, 3) for j in range(3)] # 多个for实现列表推导式
print(list1)
# 字典推导式
dict1 = {}
dict1 = {i: i**2 for i in range(5)}
print(dict1)
list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'man']
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)
counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'LENOVO': 199, 'ACER': 99}
count1 = {key: value for key, value in counts.items() if value >= 200}
print(count1)
# 集合推导式
list1 = [1, 1, 2]
set1 = {i**2 for i in list1}
print(set1) # note : 集合有去重功能