for 循环 s1 = “asdfghjkl” for i in s1 print(i) start,end,步长 for i in range(4, 20, 1): print(i) 整型 int 1、可变or不可变 2、有序or无序 3、存一个or存多个 a = 1 b = ‘1’ print(type(b)) b = int(b) print(type(b)) a = 1 print(id(a)) a = 2 print(id(a)) a = 1.2 print(a) print(type(a)) a = int(a) print(a) print(type(a)) # 浮点型 float x = 1.1 y = 100.0 print(type(x)) print(type(y)) 字符串 1、可变 or 不可变 2、有序 or 无序 3、存一个 or 存多个 不可变类型 有序 存一个值 s1 = “sadsad” print(s1) print(id(s1)) s1 += “qwe” print(s1) print(id(s1)) s1 = “abcdef” print(s1[3]) print(s1[0:3]) print(s1[1:4]) print(s1[4:]) print(s1[-2:]) print(len(s1)) 清除字符串空格 .strip name = input(“请输入用户名:”).strip() print(f"你的名字:{name},嘿嘿!") in、not in a = ‘云姐’ print(‘云’ in a) print(‘云’ not in a) 切分 s1 = “sean,18,male” l1 = s1.split(",") print(l1) 循环 ss = “asdfghj” for i in ss: print(i) 转换大小写 a = ‘asda’ b = ‘ASDA’ print(a.upper()) print(b.lower()) yunjie = 18 # name = input(“请输入你的名字:”).strip() # 判断当前字符串是否是一个数字 age = input(“年龄:”).strip() if age.isdigit(): age = int(age) print(age) if age == yunjie: print(“云姐”) else: print(“输入有误!”) else: print(“输入不符合规范!”) replace a = “aa” b = a.replace(“aa”,“bb”) print(b) 列表 list 可变类型 id不变 值变id不变 有序 存多个值 l1 = [1, 2, 3, ‘a’, ‘b’] print(l1) print(id(l1)) print(type(l1)) l1[0] = 110 print(l1) print(id(l1)) print(type(l1)) 索引取值 l1 = [1, 2, 3, ‘a’, ‘b’] print(l1[1]) 切片 l1 = [1, 2, 3, ‘a’, ‘b’] print(l1[:3]) len l1 = [1, 2, 3, ‘a’, ‘b’] print(len(l1)) 增删改查 增 append extend l1 = [1, 2, 3, ‘a’, ‘b’] l1.append(“112”) print(l1) l2 = [7, 8, 9] l1.extend(l2) print(l1) l1.insert(3,4) print(l1) 删 remove pop(删除不彻底,有返回值) l1 = [1, 2, 3, ‘a’, ‘b’] l1.remove(“b”) print(l1) l1.pop() print(l1) 改 l1 = [1, 2, 3, ‘a’, ‘b’] l1[0] = 110 print(l1) 查 l1 = [1, 2, 3, ‘a’, ‘b’] print(l1[0]) 查看索引位置 l1 = [1, 2, 3, ‘a’, ‘b’] print(l1.index(‘a’)) 拷贝 l1 = [1, 2, 3, ‘a’, ‘b’] l3 = l1.copy() print(l3) print(l1) 翻转 l1 = [1, 2, 3, ‘a’, ‘b’] l1.reverse() print(l1) 排序 l1 = [4, 8, 3, 12, 5, 1, 10] print(l1) print(id(l1)) # 列表内置的方法 l1.sort() print(l1) print(id(l1)) # Python内置的方法 ll1 = sorted(l1) print(ll1) print(id(ll1)) 元组 不可变类型 有序 存多个值 t1 = (1, 2, 3, 4, ‘a’, ‘b’) print(t1) print(id(t1)) print(type(t1)) # 索引取值 t1 = (1, 1, 2, 3, 4, ‘a’, ‘b’) print(t1[0]) # 切片(顾头不顾尾) print(t1[:4]) # 长度 len print(len(t1)) # 成员运算 in 和not in print(1 not in t1) # 循环 for i in t1: print(i) # count print(t1.count(2)) print(t1.count(1)) # index l1 = [1, 2] t1.index() 字典 dict dict = {key: value} d1 = {‘a’: 1, ‘b’: 2} print(d1) print(id(d1)) print(type(d1)) 可变类型 无序 存多个值 # 按key存取值: 可存可取 d1 = {‘a’: 1, ‘b’: 2} print(d1) print(d1[‘a’]) d1[‘c’] = 3 print(d1) # 长度len print(len(d1)) # 删除del del d1[‘a’] print(d1) # 键keys()、值value()、键值对items() # 迭代所有value for i in d1.values(): print(i) # 迭代所有key for i in d1.keys(): print(i) # 迭代所有键值对 for key, value in d1.items(): print(key, value) 集合 set s1 = {1, 2, 3, ‘a’, ‘b’, ‘c’} print(s1) print(id(s1)) print(type(s1)) 不可变类型 无序 存多个值 python = {‘发哥’, ‘强哥’, ‘冰哥’} java = {‘紫薇’, ‘考拉’, ‘阿娇’, ‘强哥’} # 长度len print(len(python)) print(len(java)) # 成员运算 in 和not in # 略 # 并集 union print(python | java) print(python.union(java)) # 交集 intersection print(python & java) print(python.intersection(java)) # 差集 difference print(python-java) print(python.difference(java)) 浅拷贝or深拷贝 l1 = [1, 2, 3, 4, ‘a’, ‘b’, [7, 8, 9]] print(l1) print(id(l1)) l2 = l1.copy() print(l2) print(id(l2)) l2[0] = 110 l2[6] [0] = 7777 print(l1) print(l2) import copy l1 = [1, 2, 3, 4, ‘a’, ‘b’, [7, 8, 9]] l2 = l1.copy() l3 = copy.deepcopy(l1) print(l3) print(id(l3)) l3[0] = 100 l3[6] [0] = 9999 print(l1) print(l2) print(l3) # 文件操作 # 读 写 #path = ‘E:\Python学习\Python 老男孩\text.txt’ f = open(‘text.txt’) data = f.read() print(data) f.close() f= open(“001.txt”, mode=‘w’, encoding=‘utf8’) f.write(“考拉|71|female”) f.close() # 回收操作系统的资源 文件打开的三种格式 path = ‘text.txt’ r:只读 f = open(path, mode=‘r’, encoding=‘utf-8’) data = f.read() print(data) f.close() w:只写 f = open(path, mode=‘w’, encoding=‘utf-8’) f.write(“shuishui|84|female”) f.close() a:追加写 f = open(path, mode=‘a’, encoding=‘utf-8’) f.write(“bingge|11|female”) f.close() b模式 (bytes) f = open(‘tp.jpg’, mode=‘rb’) data = f.read() print(data) with管理上下文 with open(“text.txt”)as r_f,\ open(“001.txt”, mode=‘w’)as w_f: data = r_f.read() w_f.write(“ajiao|77|female”) print(data) 修改文件 import os with open(“001.txt”, mode=‘r’, encoding=‘utf-8’)as rf, \ open(“001_wrap.txt”, mode=‘w’, encoding=‘utf-8’)as wf: data = rf.read() print(data) data = data.replace(“ajiao”, “阿娇”) print(data) wf.write(data) os.remove(‘001.txt’) os.rename(‘001_wrap.txt’, ‘001.txt’)