# a = ('哈哈', 'xixi', 'hehe') # print(a[0]) # print(a[0:2]) # # # 列表 # a = ['哈哈', 'xixi', 'hehe', 1, True, (1, 2, 3)] # print(a[0]) # print(a[0:2]) # a[1] = '嘻嘻' # 修改,列表:可变数据类型 # a[-1] = '呵呵' # print(a) # a.append('旺旺') # 追加元素 # print(a) # b = [1, 2, 3] # a.append(b) # print(a) # a.extend(b) # print(a) # a += b # print(a) # a[1:1] = b # print(a) # a[1:] = [1, 2, 3] # 分片赋值 # print(a) # a[:] = [1, 2, 3] # print(a) # a[0:1] = [] # 删除 # print(a) # del (a[0:1]) # 删除 # print(a) # # str1 = 'hello huice' # a = list(str1) # print(a) # print(a.count('e')) # print(a.index('l')) # print(a.append('mm')) # 打印函数,但a.append()没有返回值 # print(a) # 打印操作的对象有返回值,说明a.append()做的是原地操作,改变对象本身 # # a.pop() # 弹出最后一个元素 # a.pop(1) # print(a.pop(1)) # 既有返回值,又修改了对象 # print(a) # a.remove('h') # 没有返回值,pop可以不传参数,传的是索引,remove传的是元素 # print(a) # # a.reverse() # 没有返回值,原地反向 # print(a) a = ['Java', 'python', 'C', 'Ruby', 'PHP', 'JS'] # a.sort() # print(a) # a.sort(reverse=True) # print(a) # a.sort(reverse=True, key=len) # print(a) # def my_sort(a): # #return len(a) # return a[-1] # # a.sort(key=my_sort) # print(a) # 匿名函数 # a.sort(key=lambda a: a[-1]) # print(a) # # table = [(1, 'zhangsan', 3000), (2, 'lisi', 2500), (3, 'tiantian', 20000)] # # table.sort(reverse=True, key=lambda a: a[-1]) # print(table) # # # sort原地排序,sorted有返回值,原来列表不变 # table = [(1, 'zhangsan', 3000), (2, 'lisi', 2500), (3, 'tiantian', 20000)] # print(sorted(table, key=lambda x: x[-1], reverse=True)[0][1]) # print(table) # 遍历列表 # a = ['Java', 'python', 'C', 'Ruby', 'PHP', 'JS'] # for ele in a: # print('*') # # for i in range(len(a)): # '0,'省略 # print(a[i]) # # for i, j in enumerate(a): # print(i, j) # # a = [1, 2, 3, 4, 5] # print([ele ** 2 for ele in a]) # a = [1, 2, 3] # b = [2, 3, 4] # print(a + b) # print(a * 3) # a = [1, 2, 3] # b = ['zhangsan', 'lisi', 'tiantian'] # c = ['2000', '3000', '20000'] # print(tuple(zip(a, b, c))) # for i in zip(a, b, c): # print(i) # str0 = 'xiaoming&xiaohua' # print(str0.split('i')) # print(''.join(str0)) # eval使用的前提是,这个字符串长的像列表或元祖 # str = '[1,"a",23]' # print(type(eval(str))) # # str = '(1,"a",3)' # print(type(eval(str))) # Set集合 无序不重复元素集 不支持索引 只能用for in 循环,不能用 for in range # s1 = {1, 5, 2, 3, 5, 2} # 集合 # d1 = {'name': 'zhangsan', 'age': '18'} # 字典 key value # print(type(s1)) # print(type(d1)) # print(s1) # # for i in s1: # print(i) # # s1 = [8, 1, 5, 2, 3, 5, 2] # print(list(set(s1))) # 用来给列表去重 # lst0 = list(set(s1)) # lst0.sort(key=lambda x: s1.index(x)) # print(lst0) # # new = [] # for ele in s1: # if ele not in new: # new.append(ele) # print(new) # 字典 {k1:v2,k2:v2} a = {'zhangsan': {'语文': '90', '数学': '100'}, 'lisi': {'语文': '90', '数学': '100'}} dic = {'name': 'zhangsan', 'age': '17'} print(dic['age']) dic['age'] = 18 # 修改 print(dic) print(dic.get('name')) print(dic['name']) # print(dic.get('six')) #当取不到时会报错返回None # print(dic['six']) #当取不到时会报错 dic['six'] = '女' # 赋值 print(dic) # dic{} # dic[key]=value # dic = {} # str = 'k1:1|k2:2|k3:3' # str = str.split('|') # for i in str: # i = i.split(':') # n = i[0] # m = i[1] # dic[n] = int(m) # print(dic) # dic = {} # str = 'k1:1|k2:2|k3:3' # str = str.split('|') # for i in str: # key = i.split(':')[0] # value = i.split(':')[-1] # dic[key] = int(value) # print(dic) # # print('k1' in dic) # print(dic.keys()) # print(dic.values()) # print(dic.items()) # # dic.pop('k1') # print(dic) # # # dic = {} # # a = ['name', 'age', 'six'] # # print(dic.fromkeys(a)) # # a = {'k2': 10, 'k4': 5} # dic.update(a) # print(dic) # 字典的遍历 dic = {'k1': 1, 'k2': 2} for key in dic.keys(): print(key) for value in dic.values(): print(value) for item in dic.items(): print(item) for k, v in dic.items(): print(k, v) # k1:1|k2:2|k3:3 # # dic ={'k3': 3, 'k2': 2, 'k1': 1} print(sorted(dic.keys(), reverse=True)) print(sorted(dic.values(), reverse=True)) print(sorted(dic.items(), reverse=True)) s = {'zhagnsan': 98, 'lily': 99} print(sorted(s.items(), key=lambda x: x[-1], reverse=True)) # 函数 def add_two_number(x, y): # x、y形式参数 形参 '求加法的函数' return x + y a = 3 b = 4 # add_two_number(a, b) # a、b 实际参数 实参 # add_two_number(3, 4) # 3、4 实际参数 实参 print(add_two_number.__doc__) print(add_two_number(y=4, x=3))