Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Python´úÂëÓÅ»¯

Python´úÂëÓÅ»¯--ÉÙ´ò×ÖС¼¼ÇÉ
˵Ã÷£ºÔö¼Ó´úÂëµÄÃèÊöÁ¦£¬¿ÉÒԳɱ¶¼õÉÙÄãµÄLOC£¬×öµ½¼òµ¥£¬²¢ÇÒÕæÇÐÓÐÁ¦
¹Ûµã£ºÉÙ´ò×Ö£½¶à˼¿¼£«ÉÙ³ö´í£¬10´úÂëÐбÈ50ÐиüÄÜÈÃÈËÃ÷°×£¬ÒÔϼ¼ÇÉÓÐÖúÓÚÌá¸ß5±¶¹¤×÷ЧÂÊ
1. ½»»»±äÁ¿ÖµÊ±±ÜÃâʹÓÃÁÙʱ±äÁ¿£º(cookbook1.1)
ÀÏ´úÂ룺ÎÒÃǾ­³£ºÜÊìÁ·ÓÚÏÂÃæµÄ´úÂë
temp = x
x = y
y = temp
´úÂëÒ»£º
u, v, w = w, v, u
ÓÐÈËÌá³ö¿ÉÒÔÀûÓø³ÖµË³ÐòÀ´¼ò»¯ÉÏÃæµÄÈýÐдúÂë³ÉÒ»ÐÐ
´úÂë¶þ£º
u, v = v, u
ÆäʵÀûÓÃPythonÔª×鸳ֵµÄ¸ÅÄ¿É¸ü¼òÃ÷ -- Ôª×é³õʼ»¯ + Ôª×鸳ֵ
2. ¶Á×Öµäʱ±ÜÃâÅжϼüÖµÊÇ·ñ´æÔÚ£º(cookbook1.2)
d = { 'key': 'value' }
ÀÏ´úÂ룺
if 'key' in d: print d['key']
else: print 'not find'
дúÂ룺
print d.get('key', 'not find')
3. Ñ°ÕÒ×îСֵºÍλÖõĴúÂëÓÅ»¯£º
s = [ 4,1,8,3 ]
ÀÏ´úÂ룺
mval, mpos = MAX, 0
for i in xrange(len(s)):
if s[i] < mval: mval, mpos = s[i], i
дúÂ룺
mval, mpos = min([ (s[i], i) for i in xrange(len(s)) ])
Ôª×é±È½ÏµÄÌØÐÔ£¬¿ÉÒÔ·½±ãµÄд×öÒ»ÐÐ
¹ÛµãÒ»£ºÓÃPython±à³Ì£¬ÐèÒªÓГһ×Öǧ½ð”µÄ¸Ð¾õ£»¼ÈȻѡÔñÁËPython£¬¾Í²»ÒªÔÚÒâµ¥ÌõÓï¾äµÄЧÂÊ¡£
ÉÏÃ漸µãÀý×Ӻܻù´¡£¬Êµ¼ÊÖн«Ô­Ê¼´úÂëѹËõ1/5²¢²»ÊDz»¿ÉÄÜ£¬ÎÒÃÇ֮ǰһ¸ö×ÓÏîÄ¿£¬C++´úÂë270K
Öع¹ºóPython´úÂëÖ»ÓÐ67K£¬µ±È»Ê¹ÓÃpythonµÄÈÕ־ģ¿é£¨logging£©£¬¶Áд±í¸ñÎı¾£¨csv£©µÈ£¬Ò²¹¦
²»¿ÉÄ©£¬×îÖÕ´úÂë±ä³ÉÔ­À´µÄ1/4£¬ÎÒ¾õµÃ×Ô¼ºµÄÊÙÃüÑÓ³¤ÁËÈý±¶¡£¡£¡£ÏÂÃæÓÅ»¯¼¸¸ö³£ÓôúÂ룺
4. Îļþ¶ÁÈ¡¹¤×÷µÄ×î¼òµ¥±í´ï£º
ÀÏ´úÂ룺ÎÒÃÇÐèÒª½«Îı¾Îļþ¶ÁÈëµ½ÄÚ´æÖÐ
line = ''
fp = open('text.txt', 'r')
for line in fp: text += line
´úÂëÒ»£º
text = string.join([ line for line in open('text.txt')], '']
´úÂë¶þ£º
text = ''.join([ line for line in open('text.txt')])
´úÂëÈý£º
text = file('text.txt').read()
а汾µÄPython¿ÉÒÔÈÃÄãд³ö±È1£¬2ƯÁÁµÄ´úÂ루openÊÇfileµÄ±ðÃû£¬ÕâÀïfile¸üÖ±¹Û£©
5. ÈçºÎÔÚPythonʵÏÖÈýԪʽ£º
ÀÏ´úÂ룺ÓùßC++,Java,C
²»Ï²»¶Ð´ÏÂÃæ´úÂë
if n >= 0: print 'positive'
else: print 'negitive'
´úÂëÒ»£º¸Ã¼¼ÇÉÔÚ LuaÀïÒ²ºÜ³£¼û
print (n >= 0) and 'positive' or 'negitive'
˵Ã÷£ºÕâÀïµÄ'and'ºÍ'or'Ï൱ÓÚCÖеÄ':'ºÍ'?'µÄ×÷Ó㬵ÀÀíºÜ¼òµ¥£¬ÒòΪÈç¹û±í´


Ïà¹ØÎĵµ£º

PythonÉÏ´«Îļþµ½·þÎñÆ÷

#! /usr/bin/env python
# -*- coding: utf-8 -*-
from ftplib import FTP
from os import path
from optparse import OptionParser
from FtpDownload import spilt_cmd
import socket
import sys
'''
ÉÏ´«ÖÕ¶ËMRÎļþµ½·þÎñÆ÷
V1.0    zhai   2009-07-27
'''
def main():
&nbs ......

PythonÖÐsocketºÍ¶àÏ̵߳ÄÓ¦ÓÃ

ÕâÊÇÎÒÒÔǰдµÄÒ»¸ösocketСӦÓã¬ÆäÖÐҲ˳±ãÓõ½ÁËÒ»µãµã¶àÏ̵߳Ķ«Î÷¡£
Õâ¸ösocket¿Í»§¶ËÊÇÎÒΪÁË·½±ã¹«Ë¾µÄ³äֵϵͳµ÷ÊÔ¶øдµÄÒ»¸öСӦÓá£ÓÉËü¸ú³äֵϵͳ½øÐÐsocketͨѶ£¬×Ô¶¯Íê³É³äÖµµÄ²âÊÔ¹¤×÷¡£
¿´´úÂë°É£º
import socket,thread
def Communicate(s):
phoneno = raw_input("Please input your PhoneNo.:(Pu ......

×î¼òµ¥µÄ python ¶àÏß³ÌʾÀý

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


pythonʹÓÃSocketServers
SocketServersÄ£¿éΪһ×ésocket·þÎñÀඨÒåÁËÒ»¸ö»ùÀ࣬Õâ×éÀàѹËõºÍÒþ²ØÁ˼àÌý¡¢½ÓÊܺʹ¦Àí½øÈëµÄsocketÁ¬½ÓµÄϸ½Ú¡£
1¡¢SocketServers¼Ò×å
TCPServerºÍUDPServer¶¼ÊÇSocketServerµÄ×ÓÀ࣬ËüÃÇ·Ö±ð´¦ÀíTCPºÍUDPÐÅÏ¢¡£
×¢Ò⣺SocketServerÒ²ÌṩUnixStreamServer(TCPServerµÄ×ÓÀà)ºÍUNIXdatag ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ