´ô´ôµÄPython ±Ê¼Ç
һЩ×ۺϵÄÐÅÏ¢
Python
ÀËõ½øºÜÖØÒª¡£Ã»ÓмâÀ¨ºÅ²»Òª½ô£¬
Python
¸ù¾ÝËõ½øÀ´·Ö¸îÓï¾ä¿é¡£
²ÎÊý²»ÐèÒª¶¨Ò壬¿ÉÒÔÖ±½ÓʹÓá£
Help(var)
²é¿´
var
µÄ°ïÖú¡£
Var
¿ÉÒÔΪÈκζ«Î÷£¬º¯Êý£¬Ä£¿é£¬Àà¡£
Python
ÖеÄ×Ö·û´®ÊDz»¿É±äµÄ¡£
Pass
±íʾ¿ÕÓï¾ä¿é¡£
# ×¢ÊÍ
String
r‘I’m a string’
×ÔÈ»×Ö·û´®£¬²»½øÐÐתÒå¡£
‘I’m a string’
תÒå×Ö·û´®¡£
ÓÃ
+
¿ÉÁ¬½Ó×Ö·û´®
target = today + os.sep
+ now + '_' + \
# \
ÓÃÀ´Á¬½Ó»»ÐеÄ×Ö·û´®
comment.replace(' ', '_') + '.zip'
º¯Êý
º¯ÊýµÄ¶¨Ò壺
Def function():
…
DocString
'''Prints the maximum
of two numbers.
The two values must be integers.'''
¾ÍÊǸö×¢ÊÍ¡£¡£¡£µ«ÊÇ¿ÉÒÔͨ¹ýº¯ÊýÃû
.__doc__
À´Êä³ö¡£
help()
Ò²¿ÉÒԴﵽͬÑùµÄ×÷Óá£
Ä£¿éÏà¹Ø²Ù×÷
import moduleName –
ÒýÈë
moduleName
Ä£¿é£¬
Ö»Òª¸ÃÄ£¿éÔÚ
python
ϵͳ·¾¶Ï¡£
sys
Ä£¿é°üº¬ÁËÓë
Python
½âÊÍÆ÷ºÍËüµÄ»·¾³Óйصĺ¯Êý¡£
sys.argv
ÖаüÀ¨ÁËÎļþÃû¼°²ÎÊý¡£
sys.path
Öаüº¬ÊäÈëÄ£¿éµÄĿ¼ÃûÁбí
dir(ModuleName)
ö¾ÙÄ£¿éÖеÄËùÓÐÊôÐÔ£¬²ÎÊýΪ¿Õʱ·µ»Øµ±Ç°Ä£¿éµÄËùÓÐÊôÐÔ£¬ÊäÈëµÄÄ£¿é±ØΪµ±Ç°Ä£¿éÊôÐÔÁбíµÄÒ»Ô±¡£
*
¿ÉÒÔÖ»µ¥¶ÀÔËÐÐÄ£¿éŶ
=v=
pyc
Îļþ
×Ö½Ú±àÒëµÄÎļþ¡£ÓÃÓÚʹģ¿éÊäÈë¸ü¼Ó¿ì½Ý¡£Óëƽ̨Î޹ء£
__name__
ÅжÏÊÇ·ñÊÇÖ÷Ä£¿é
if __name__ ==
'__main__':
Áбí
List
list = [a,b,c]
len(list)
ÁÐ±í³¤¶È
list.sort()
ÁбíÅÅÐò
list.append(t)
Áбíĩβ׷¼Ó
…
Ôª×é
Tuple
tuple = (a,b,c)
Ôª×éºÍÁбíÊ®·ÖÀàËÆ£¬Ö»²»¹ýÔª×éºÍ×Ö·û´®Ò»ÑùÊDz»¿É±äµÄ£¬¼´Äã²»ÄÜÐÞ¸ÄÔª×é¡£
³£ÓÃÓÚÊä³ö
print '%s is %d years
old' % (name, age)
Dict
ͬ
map
ÐòÁÐ
sequence
seq = [a,b,c,d,e]
ÓëÆäËû¼¸ÖÖÈÝÆ÷µÄ²»Í¬ÊÇË÷ÒýºÍÇÐƬ¡£
Ë÷Òý£ºµÃµ½Ò»¸öÌض¨ÏîÄ¿
seq[2]
ÇÐƬ£º¿ÉÒԵõ
Ïà¹ØÎĵµ£º
# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
page = urllib2.urlopen("http://bj.ganji.com/piao/zz_%E5%8C%97%E4%BA%AC-%E5%8D%97%E6%98%8C/20100210/")
soup = BeautifulSoup(page)
#ss = soup.findAll('a', href=re.compile(r"^/piao/100.&qu ......
>>> import string
>>> s='adbecf'
>>>
tt=string.maketrans("abc","ABC")
>>> s.translate(tt,"")
'AdBeCf'
>>>
s.translate(tt,"")
ºóÃæµÄÄǸö¿Õ×Ö·û´´¾ÍÊÇÄãҪɾ³ýµÄ×Ö·û£¬±ÈÈçҪɾ³ý»»ÐоÍÊÇs.translate(tt,"\n&q ......
PythonÖÐÖ´ÐÐϵͳÃüÁî³£¼û·½·¨ÓÐÁ½ÖÖ£º
Á½Õß¾ùÐè import os
(1) os.system
# ½ö½öÔÚÒ»¸ö×ÓÖÕ¶ËÔËÐÐϵͳÃüÁ¶ø²»ÄÜ»ñÈ¡ÃüÁîÖ´ÐкóµÄ·µ»ØÐÅÏ¢
system(command) -> exit_status
Execute the command (a string) in a subshell.
# Èç¹ûÔÙÃüÁîÐÐÏÂÖ´ÐУ¬½á¹ûÖ±½Ó´òÓ¡³öÀ´
>>> os. ......
ÓõķֱðÊÇtimeºÍdatetimeº¯Êý
'''
Created on 2009-9-2
@author: jiangqh
'''
import time,datetime
# date to str
print time.strftime("%Y-%m-%d %X", time.localtime())
#str to date
t = time.strptime("2009 - 08 - 08", "%Y - %m - %d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)
Êä³öµ±Ç°Ê±¼ä ......
Ê×ÏÈ, ˵˵pythonÀïÃæµÄ±Õ°ü°É:
1. ÐèÒªº¯ÊýǶÌ×, ¾ÍÊÇÒ»¸öº¯ÊýÀïÃæÔÙдһ¸öº¯Êý.
2. Íⲿº¯ÊýÐèÒª·µ»ØÒ»¸öÄÚ²¿º¯ÊýµÄÒýÓÃ
3. Íⲿº¯ÊýÖÐÓÐһЩ¾Ö²¿±äÁ¿, ²¢ÇÒ, ÕâЩ¾Ö²¿±äÁ¿ÔÚÄÚ²¿º¯ÊýÖÐÓÐʹÓÃ
¸ÅÄî:
1. ×ÔÓɱäÁ¿: Íⲿº¯ÊýÖж¨ÒåµÄ¾Ö²¿±äÁ¿, ²¢ÇÒÔÚÄÚ²¿º¯ÊýÖб»Ê¹ÓÃ.
2. ±Õ°ü: ÄǸöʹÓÃÁË×ÔÓɱäÁ¿²¢±»·µ»ØµÄÄÚ²¿º¯Ê ......