首页 > 编程语言 > 详细

python tricks

时间:2017-10-11 12:06:46      阅读:220      评论:0      收藏:0      [点我收藏+]

1.

cities = [Marseille, Amsterdam, New York, Londom]

# the good way
for i, city in enumerate(cities):
    print(i, city)

2.

x_list = [1, 2, 3]
y_list = [2, 4, 6]

# the good way
for x, y in zip(x_list, y_list):
    print (x, y)

3.

x = 10
y = -10

# the good way
x, y = y, x
print (After: x = %d, y = %d % (x, y))

4.

ages = {
    Mary      : 31,
    Honathan : 28
}

# the good way
age = ages.get(Dick, Unknow)
print (Dick is %s years old % age)

5.

needle = d
haystack = [a, b, c, d]
# the good way
for letter in haystack:
    if needle == letter:
        print (Found!)
        break
else:
    print (Not found!)

6.

# the good way
with open(pimpin-aint-easy.txt) as f:
    for line in f:
        print (line)

7.

print (Converting!)
try:
    print (int(1))
except:
    print (Conversion failed!)
else:
    print (Conversion uccessful!)
finally:
    print (Done!)

 

python tricks

原文:http://www.cnblogs.com/xuanyuyt/p/7649720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!