随笔分类 - Python
摘要:class TestCase: def run(self, result=None): if result is None: result=self.defaultTestResult() result.startTest(self) t...
阅读全文
摘要:open(file_name, access_mode='r',buffering=-1) r+,w+,a+ 打开read()读取 如果没有给定size参数(默认-1)或者size为负,文件被读取到末尾readline()读取打开文件的一行readlines()读取所有的行让后把他们作为一个字...
阅读全文
摘要:#!/usr/bin/env pythondef MaxFactor(num): count= num/2 while count >1: if num%count==0: print 'larget factor of %d is...
阅读全文
摘要:copy(),>>> help(copy) import copy x = copy.copy(y) # make a shallow copy of y浅拷贝 x = copy.deepcopy(y) # make a dee...
阅读全文
摘要:list.append(obj) #添加一个对象obj>>> list=[123]>>> list.append(123)>>> list[123, 123]list.count(obj)#返回一个对象obj在列表中出现的次数>>> list.count(123)2list.extend(seq)#...
阅读全文
摘要:len()#返回元素个数,max()#最大字符(字典序)min()#最小字符(字典序)sum()#求和reversed():#字典序排列list():浅拷贝创建新列表tuple():浅拷贝创建新元组>>> s1=['name',"they're",'age','The','Ta']>>> for t...
阅读全文
摘要:>>> string='abcd'>>> len(string)4>>> string[2:4]'cd'>>> string[-1]'d'>>> string[-3:-1]'bc'
阅读全文
摘要:abs()#返回去绝对值,如果是复数就返回math.sqrt(num.real2+num.imag2)>>> abs(-1)1>>> abs(1.2-2.1j)2.4186773244895647coerce()#3.3.5已经不存在dimod()#返回求余和求除的值>>> divmod(10,3)...
阅读全文
摘要:int() #int(obj,base=10)>>> int(4.5)4float()#(obj)>>> float(4)4.0complex()#复数complex()str or complex(real,inag=0.0)>>> complex(4)(4+0j)>>> complex(2.8e...
阅读全文
摘要:>>> a=10>>> b=10>>> a is bTrue>>> c=10.0>>> d=10.0>>> c is dFalse>>> a==bTrue>>> c==dTrue>>> id(c)47464176>>> id(d)47460624>>> id(a)505911152>>> id(b)...
阅读全文
摘要:type()和isinstance()判断类型>>> a=111>>> type(a)>>> a=111>>> isinstance(a,int)True一个明显的区别是判断子类。type()不会认为子类是一种父类类型。isinstance()会认为子类是一种父类类型。>>> class A: pass>>> class B(A): pass>>> isinstance(A(),A)True>>> type(A())==ATrue>>>
阅读全文
摘要:#!/user/bin/env pythonfname=raw_input('Enter filename:')printtry: fobj= open(fname,'r')except IOError,e: print "xxx file open error :",eelse: for eachline in fobj: print eachline, fobj.close()
阅读全文
摘要:num = raw_input('input is :' )#用户输入for i in range(len(num)): #循环取字符输出 print num[i]for i,ch in enumerate(num):#使用enumerate()函数 print ch
阅读全文
浙公网安备 33010602011771号