格式化
# 格式化 a=123 b=‘ww‘ print("%d,%s,%%"%(a,b)) # %d,%s,%f,%c,%f
while-else:没有while就走else
优先级:()>not>and>or。谁先在,先算谁!!!
列表增删查改
# append 追加,给列表的最后面追加一个元素 l = [1, 2, ‘a‘] l.append(666) print(l) # [1, 2, ‘a‘, 666] # insert 插入在列表的任意位置插入元素 l = [1, 2, ‘a‘] l.insert(1,‘太白‘) print(l) # [1, ‘太白‘, 2, ‘a‘] # extend 迭代着追加,在列表的最后面迭代着追加一组数据 l = [1, 2, ‘a‘] l.extend(‘太白a‘) print(l)
# pop 通过索引删除列表中对应的元素,该方法有返回值,返回值为删除的元素 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] ret = l.pop(1) print(ret,l) # alex [‘太白‘, ‘WuSir‘, ‘女神‘] # remove 通过元素删除列表中该元素 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] l.remove(‘alex‘) print(l) # [‘太白‘, ‘WuSir‘, ‘女神‘] # clear 清空列表 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] l.clear() print(l) # [] # del #按照索引删除该元素 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] del l[2] print(l) # [‘太白‘, ‘alex‘, ‘女神‘] # 切片删除该元素 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] del l[1:] print(l) # [‘太白‘] # 切片(步长)删除该元素 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] del l[::2] print(l) # [‘alex‘, ‘女神‘]
# 按照索引改值 l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] l[0] = ‘男神‘ print(l) # [‘男神‘, ‘alex‘, ‘WuSir‘, ‘女神‘] # 按照切片改值(迭代着增加) l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] l[1:3] = ‘abcdefg‘ print(l) # [‘太白‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘女神‘] # 按照切片(步长)改值(必须一一对应) l = [‘太白‘, ‘alex‘, ‘WuSir‘, ‘女神‘] l[::2] = ‘对应‘ print(l) # [‘对‘, ‘alex‘, ‘应‘, ‘女神‘]
原文地址:https://www.cnblogs.com/shuoran/p/11631648.html
时间: 2024-10-09 03:05:55