######python 切片
#### list或tuple的切片 以list为例子
L=list(range(50))
##去前N 或者N-M为元素
print (L[2:9])
## 没隔X位取一个元素
print (L[::2])
print (L[10:50:5])
#####字符串也是一样的
S='AHGJHGWUYIIDJNJNDSUWIMM'
print (S[::2])
执行效果:
[2, 3, 4, 5, 6, 7, 8]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
[10, 15, 20, 25, 30, 35, 40, 45]
AGHWYIJJDUIM
###### 迭代
###list 迭代
for s in list(range(10)):
print (s)
### 字符串迭代
for s in 'sdfjkshhsd':
print (s)
#### dict 迭代
d={'a':99,'b':23,'c':56}
for key in d:
print (key)
for value in d.values():
print (value)
for key,value in d.items():
print (key,value)
for x, y in [(1, 2), (2, 3), (3, 4)]:
print (x,y)
执行效果
0
1
2
3
4
5
6
7
8
9
s
d
f
j
k
s
h
h
s
d
a
b
c
99
23
56
a 99
b 23
c 56
1 2
2 3
3 4