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 提供高级的文件访问功能










本文出自 “life is gonna be better” 博客,转载请与作者联系!

你可能感兴趣的:(python,学习笔记)