list与遍历

>>> squares = [x**2 for x in range(1,10)]
>>> [n for n in range(1,100) if n%3==0]
>>> mybag = [' glass',' apple','green leaf ']
>>> [one.strip() for one in mybag]
>>> list(enumerate(mybag))
for i, color in enumerate(colors):
   print i, '--->', color
# 奇数
odd_items = items[1::2]
#拷贝
copy_items = items[::] 或者 items[:]

性能与空间

for key in _dict:
    pass

条件表达式

text = '男' if gender == 'male' else '女'
d = {'name': 'foo'}
d.get("name", "unknow")
类型 False True
布尔 False (与0等价) True (与1等价)
字符串 空字符串,如 "" 非空字符串,如 "blog"
数值 0, 0.0 非0的数值,例如:1, 0.1, -1, 2
容器 [], () 至少有一个元素的容器对象,例如:[0], (None,), ['']
None None 非None对象

字符串

s3 = "welcome to {blog} and following {wechat}".format(blog="foofish.net", wechat="vttalk")
names = ['raymond', 'rachel', 'matthew', 'roger',
        'betty', 'melissa', 'judith', 'charlie']
print ', '.join(names)

本文固定链接: http://www.js-code.com/python/python_51182.html