Python语言技巧 2014-10-11 13:00

基本介绍

  • 带索引的列表迭代:
1
2
3
4
5
6
7
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
    print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
  • 数值比较:
1
2
3
4
5
6
7
x = 2
if 3 > x > 1:
   print x
>>> 2
if 1 < x > 0:
   print x
>>> 2
  • 变量交换:
1
2
3
4
5
6
7
x = 6
y = 5
x, y = y, x
print x
>>> 5
print y
>>> 6

参考

Tags: #Python    Post on Python