1.for
# for可使用迭代器进行元素的访问
for item in 'HelloWord':
print(item, end=' ')
2.while
Sum = 0
while Sum <= 50: # 该循环公执行51次
Sum += 1
print(Sum) # 51
3.else的特殊用法
'''在for和while后面加else 当for和while正常退出时,else执行
如果遇到break没有执行完,则会跳过else里的内容
'''
for item in range(10):
if item == 5:
break
else:
print("for执行完成")
'''while同理'''
4.list
1.创建列表
# 列表类似于数组,其中内容可以混用(内容为多种类型),list里可以添加内容为list的元素
# 创建方法
lst1 = ['abc', 123, 123.456, False, "字符串"]
print(lst1)
# 也包含三种属性
print('id:', id(lst1), end='\t')
print("type:", type(lst1), end='\t')
print("value:", lst1)
# 正索引从0开始, 负索引从-1到-n
print(lst1[0], lst1[-5])
2.添加元素
append() 函数
lst1 = ['abc', 123, 123.456, False, "字符串"]
lst1.append(123.456) # 在list后追加内容 即可变长
print(lst1) # ['abc', 123, 123.456, False, '字符串', 123.456]
insert函数
lst1 = ['abc', 123, 123.456, False, "字符串"]
lst1.insert(0, '789') # 在指定位置插入元素
print(lst1) # ['789', 'abc', 123, 123.456, False, '字符串']
extend() 函数
lst1 = [1, 2, 3, 4]
lst1.extend([10, 9, 8]) # 在末尾添加多个元素 参数类型:列表
print(lst1) # [1, 2, 3, 4, 10, 9, 8]
pop() 函数
lst1 = [0, 1, 2, 3, 4, 5, 6, 7]
lst1.pop(0) # 根据索引移除元素
print(lst1) # [1, 2, 3, 4, 5, 6, 7]
lst1 = [0, 1, 2, 3, 4, 5, 6, 7]
lst1.pop() # 不指定参数,删最后一个
print(lst1) # [0, 1, 2, 3, 4, 5, 6]
lst1 = [0, 1, 2, 3, 4, 5, 6, 7] # 索引 :0-7
lst1.pop(8) # 索引越界报错 IndexError: pop index out of range
remove() 函数
lst1 = [0, 1, 2, 5, 4, 5, 6, 7]
lst1.remove(5) # 移除列表里第一次遇到的该元素 参数:需移除的元素
print(lst1) # [0, 1, 2, 4, 5, 6, 7]
lst1 = [0, 1, 2, 5, 4, 5, 6, 7]
lst1.remove(100) # ValueError: list.remove(x): x not in list
clear() 函数
lst1 = [0, 1, 2, 3, 4, 5, 6, 7]
lst1.clear() # 清除列表内的元素
print(lst1) # []
del
lst1 = [0, 1, 2, 3, 4, 5, 6, 7]
del lst1 # 删除变量 其他类型的变量同样适用
print(lst1) # NameError: name 'lst1' is not defined
切片操作
lst2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# list[start : stop : step] start默认0 stop默认maxsize-1 step 默认 1
# step为负数时,逆序输出,这时的start应从maxsize-1开始
print(lst2[::]) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# start默认maxsize-1 stop默认0 step=-1 逆序输出
print(lst2[::-1]) # [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(lst2[19::-2]) # [19, 17, 15, 13, 11, 9, 7, 5, 3, 1]
print(lst2[0:10:2]) # [0, 2, 4, 6, 8]
判断是否在列表中
lst2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
judge = 15 in lst2
print(judge) # True
更改元素
lst2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
lst2[1:3] = [10, 20, 30] # 把start到stop-1中间的元素切掉并用后面的list填充原来的位置
print(lst2) # [0, 10, 20, 30, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# 删除 0-9
lst2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
lst2[0:10] = [] # 可替换为空列表
print(lst2) # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
index函数
lst1 = ['abc', 123, 123.456, False, "字符串"]
index_123 = lst1.index(123) # 查找索引
print(index_123) # 1
sort函数
lst3 = [8, 4, 6, 9, 7, 2, 1, 3, 0, 5, 11, 98, 46, 87, 13, 65, 99]
lst3.sort() # 排序不产生新列表
print(lst3) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 46, 65, 87, 98, 99]
lst3 = [8, 4, 6, 9, 7, 2, 1, 3, 0, 5, 11, 98, 46, 87, 13, 65, 99]
lst3.sort(reverse=True) # 降序排序 默认False
print(lst3) # [99, 98, 87, 65, 46, 13, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
sorted函数
# 该函数为内置函数 会产生新列表
lst3 = [8, 4, 6, 9, 7, 2, 1, 3, 0, 5, 11, 98, 46, 87, 13, 65, 99]
new_list = sorted(lst3, reverse=True)
print(lst3) # [8, 4, 6, 9, 7, 2, 1, 3, 0, 5, 11, 98, 46, 87, 13, 65, 99]
print(new_list) # [99, 98, 87, 65, 46, 13, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
列表生成式
list = [i for i in range(1, 10)]
print(list) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = [i*2 for i in range(1, 10)]
print(list) # [2, 4, 6, 8, 10, 12, 14, 16, 18]