image.png
image.png
image.png
image.png
break 直接跳出循环并结束
continue 跳出本次循环
>>> i=0
>>> while i < 10:
i=i+1
if i % 2 ==0:
continue
print (i)
1
3
5
7
9
角标
>>> i = 0
>>> while i < len("qijia"):
print("qijia[i]")
i= i + 1
qijia[i]
qijia[i]
qijia[i]
qijia[i]
qijia[i]
>>> i= 0
>>> while i < len("qijia"):
print("qijia"[i])
i = i + 1
q
i
j
i
a
>>> print("nihao"[3])
a
>>> print("nihao"[0])
n
>>> print("nihao"[4])
o
列表
切片
步长
倒序
append 列表末尾添加元素(一次只能添加一个元素)
extend
s = [ 1, 2, 3, 4, 5]
s[len(s):]=[6]
s[len(s):]=[7,8,9]
inster
remove 多个同样的元素存在 只删除第一个(角标最小的)
pop 通过角标删除
clear 清空列表
元素替换
>>> heros = ["a","b","c"]
>>> heros
['a', 'b', 'c']
>>> heros[2]="www"
>>> heros
['a', 'b', 'www']
批量替换
>>> heros=[1,2,3,4,5,6,7]
>>> heros
[1, 2, 3, 4, 5, 6, 7]
>>> heros[3:]=["a","b","c"]
>>> heros
[1, 2, 3, 'a', 'b', 'c']
排序
>>> heros=[3,5,2,1,0]
>>> heros
[3, 5, 2, 1, 0]
>>> heros.sort()
>>> heros
[0, 1, 2, 3, 5]
>>>
倒序
reverse
元素计数
count
某个元素的索引值
index
>>> heros=[3,5,2,1,0]
>>> heros.index(5)
1
>>> heros[heros.index(5)]="c" #这个等同于 heros[1]="c"
>>> heros
[3, 'c', 2, 1, 0]
>>>
复制
copy
heros2=heros.copy()
通过切片也可以实现元素的拷贝
heros2=heros[:]