转载自:http://www.xwy2.com/article.asp?id=125
多变量赋值
Code
>>>>>> a = b = c = 1
>>>>>> print a, b, c
1 1 1
>>>>>> (a, b, c) = range(3)
>>>>>> print a, b, c
0 1 2
>>>>>> (a, b, c) = ("a", "b", "c")
>>>>>> print a, b, c
a b c
连接字符串
Code
>>>>>> (a, b, c) = ("a", "b", "c")
>>>>>> print a, b, c
a b c
>>>>>> s = ("a", "b", "c")
>>>>>> ",".join(s)
'a,b,c'
>>>>>> s = {1:"a", 2:"b", 3:"c"}
>>>>>> ",".join(["%d:%s" % (k, v) for k, v in s.items()])
'1:a,2:b,3:c'
使用 0 < i < 10 要比 使用 i > 0 and i < 10 性能更好一些
Code
>>>>>> def test(i):
if (0 < i < 10):
print "ok"
else:
print ""
>>>>>> test(4)
ok
>>>>>> test(12)
>>>>>> test(0)
不要使用 ++i 来递增
Python 虽然支持 ++i,但实际表示的意思是 +(+i),我们可以使用 i += 1 来实现递增。
Code
>>>>>> i = -10
>>>>>> ++i
-10
>>>>>> i += 1
>>>>>> i
-9