Python入门的36个例子 之 21
源代码下载:下载地址在这里
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# 也可以使用整型作为唯一的编号
dict2 = {
5064001:'Mememe',
5064002:'tutu',
5064003:'thrthr',
5064004:'fofo'
}
print dict2[5064003]
# 添加
dict2[5064000] = 'none'
print dict2[5064000]
del dict2[5064002]
print dict2
for ele in dict2:
print ele
for id, name in dict2.items():
print id, name
output:
thrthr
thrthr
none
{5064000: 'none', 5064001: 'Mememe', 5064003: 'thrthr', 5064004: 'fofo'}
5064000
5064001
5064003
5064004
5064000 none
5064001 Mememe
5064003 thrthr
5064004 fofo
相关文档:
import time,thread
def test(a,b):
for i in range(a,b):
time.sleep(1)
print i
def start():
thread.start_new_thread(test,(1,1001))
thread.start_new_thread(test,(1000,2001))
if __name__=='__main__':
start()
......
python使用SocketServers
SocketServers模块为一组socket服务类定义了一个基类,这组类压缩和隐藏了监听、接受和处理进入的socket连接的细节。
1、SocketServers家族
TCPServer和UDPServer都是SocketServer的子类,它们分别处理TCP和UDP信息。
注意:SocketServer也提供UnixStreamServer(TCPServer的子类)和UNIXdatag ......
djangoproject python 3 prog python code http://wiki.python.org/moin/WebBrowserProgramming http://wiki.woodpecker.org.cn/moin/ http://www.okpython.com/viewlist-fid-5.html http://zh.wikipedia.org/wiki/CPython django doc douban ibm python doc ibm python zope http://www.czug.org/ http://www.apolov ......
Python代码优化--少打字小技巧
说明:增加代码的描述力,可以成倍减少你的LOC,做到简单,并且真切有力
观点:少打字=多思考+少出错,10代码行比50行更能让人明白,以下技巧有助于提高5倍工作效率
1. 交换变量值时避免使用临时变量:(cookbook1.1)
老代码:我们经常很熟练于下面的代码
temp = x
x = y
y = ......
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......