python學習筆記(三)

不支持char,byte

repr 相當於反引號

id(x)

id(a)==id(b)     <=>    a is b


foostr='abcde'

foostr[::-1]='edcba'

foostr[::-2]='eca'  //隔一個取一個



############    r'\n'   '\n'################

>>> '\n'

'\n'

>>> print '\n'



>>> print r'\n'

\n


################   re  正則  ##################

import re

>>> m=re.search(r'\\[rtfvn]',r'Hello World\n')


>>> if m is not None:m.group()

... 

'\\n'



#############    程序中出現字符串時加個u   ################

>>> u'\1234'

u'S4'

>>> u'\u1234'

u'\u1234'

>>> str2='lmn'

>>> str3='xyz'

>>> max(str2)

'n'

>>> max(str2) 

'n'

>>> min(str3)

'x'

>>> str='ab12MN'

>>> max(str)

'b'


###############   enumerate  ##################

>>> for i,t in enumerate(s):

...     print i,t

... 

0 f

1 o

2 o

3 b

4 a

5 r


>>> user_input=raw_input('Enter u name:')

Enter u name:wangweiwei

>>> print user_input

wangweiwei



##############   isinstance  ###################

>>> not isinstance('foo',unicode)

True

>>> isinstance(u'\0xAB',str)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

>>> isinstance(u'',basestring)

True

>>> isinstance('foo',basestring)

True

>>> 


#########      capitalize,center,count,isspace,upper,lower   ########################

>>> s='abc'

>>> s.capitalize()

'Abc'

>>> s.center(10)

'   abc    '

>>> s.count(s,0,12)

1


>>> s.isspace()

False

>>> s.upper()

'ABC'

>>> s.lower()

'abc'



#############    split,   join   ########################

>>> queset='What\'s u favorite color?'

>>> queset.split()

["What's", 'u', 'favorite', 'color?']

>>> ':'.join(queset.split())

"What's:u:favorite:color?"


###########reversed#####################

s=['they','are','flowers']

>>> for t in reversed(s):

...     print t

... 

flowers

are

they

>>> sorted(s)

['are', 'flowers', 'they']



############  將要使用str,chr的地方都使用unicode(),unichr()


#!/usr/bin/python

codec='utf-8'

filename='file.txt'

fp=open(filename,'w')

str=u'Hello World!\n'

str=str.encode(codec)

print str

fp.write(str)

fp.close()


hp=open(filename,'r')

str=hp.read()

hp.close()

str=str.decode(codec)

print str



################## append(),pop()





>>> cmp((4,2),(3,5))

1


##########   淺拷貝=>   person[:] :拷貝,copy()  深拷貝:import copy   copy.deepcopy()

>>> person=['a','b','c']

>>> habby=person[:]

>>> print habby

['a', 'b', 'c']

>>> 





###########   dict():創建字典           fromkeys():內建的創建字典的方法



fdict=dict((['x',1],['y',2]))



>>> ddict={}.fromkeys(('x','y'),-1)

>>> ddict

{'y': -1, 'x': -1}


#############訪問字典中的鍵

>>> for key in ddict.keys():

...     print 'key=%s,value=%s' %(key,ddict[key])

... 

key=y,value=-1

key=x,value=-1



###############刪除字典值del ddict['x'],ddict.clear()



###############dict.get()


############# dict.itere()

#############  iteritems() , iterkeys() ,itervalues()




#####################   字典的update方法

>>> dic2={'a':'1','b':'2'}

>>> dic3={'c':'3','d':'4'}

>>> dic2.update(dic3)

>>> print dic2

{'a': '1', 'c': '3', 'b': '2', 'd': '4'}

>>> dic2.clear()

>>> print dic2

{}

>>> 




########################setdefault():若字典中不存在此值,可當即設置上,若存在,則可以直接取到



##################fromkeys()


>>> {}.fromkeys('xyz')

{'y': None, 'x': None, 'z': None}

>>> {}.fromkeys(('love','honor'),True)

{'love': True, 'honor': True}






###########只有程序一運行便可以訪問這三個文件sys.stdin,sys.stdout,sys.stderr




tempfile 用於生成臨時的文件名

shutil 提供高級的文件訪問功能










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章