码迷,mamicode.com
首页 > 编程语言 > 详细

python tricks

时间:2017-10-11 13:02:04      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:imp   not found   ack   col   line   tricks   print   lin   letter   

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

标签:imp   not found   ack   col   line   tricks   print   lin   letter   

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!