优先掌握部分
切片
l=['a','b','c','d','e','f']
print(l[1:5])
print(l[1:5:2])
print(l[2:5])
print(l[-1])
了解
print(l[-1:-4])
print(l[-4:])
l=['a','b','c','d','e','f']
print(l[-2:])
追加
hobbies=['play','eat','sleep','study']
hobbies.append('girls')
print(hobbies)
删除
hobbies=['play','eat','sleep','study']
x=hobbies.pop(1) #不是单纯的删除,是删除并且把删除的元素返回,我们可以用一个变量名去接收该返回值
print(x)
print(hobbies)
x=hobbies.pop(0)
print(x)
x=hobbies.pop(0)
print(x)
队列:先进先出
queue_l=[]
入队
queue_l.append('first')
queue_l.append('second')
queue_l.append('third')
print(queue_l)
出队
print(queue_l.pop(0))
print(queue_l.pop(0))
print(queue_l.pop(0))
堆栈:先进后出,后进先出
l=[]
入栈
l.append('first')
l.append('second')
l.append('third')
出栈
print(l)
print(l.pop())
print(l.pop())
print(l.pop())
了解
del hobbies[1] 单纯的删除
hobbies.remove('eat') 单纯的删除,并且是指定元素去删除
长度
hobbies=['play','eat','sleep','study']
print(len(hobbies))
包含in
hobbies=['play','eat','sleep','study']
print('sleep' in hobbies)
msg='hello world egon'
print('egon' in msg)
掌握部分
hobbies=['play','eat','sleep','study','eat','eat']
hobbies.insert(1,'walk')
hobbies.insert(1,['walk1','walk2','walk3'])
print(hobbies)
print(hobbies.count('eat'))
print(hobbies)
hobbies.extend(['walk1','walk2','walk3'])
print(hobbies)
hobbies=['play','eat','sleep','study','eat','eat']
print(hobbies.index('eat'))
了解部分
hobbies=['play','eat','sleep','study','eat','eat']
hobbies.clear()
print(hobbies)
l=hobbies.copy()
print(l)
l=[1,2,3,4,5]
l.reverse()
print(l)
l=[100,9,-2,11,32]
l.sort(reverse=True)
print(l)
循环
msg_dic={
'apple':10,
'tesla':1000000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
index=0
while index < len(msg):
print(msg[index])
index+=1
不依赖索引的取值
msg_dic={
'apple':10,
'tesla':1000000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
for item in msg_dic:
print(item,msg_dic[item])
补充
range:顾头不顾尾,默认从0开始
print(type(range(1,2)))
for i in range(10):
print(i,type(i))
break
continue
for i in range(10):
if i == 4:
# break
continue
print(i)
for+else
for i in range(10):
print(i)
break
else:
print('===>')