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)