python的常用内置函数
①abs() 函数返回数字的绝对值
dict()
{} ? ? ?#创建一个空字典类似于u={},字典的存取方式一般为key-value
help('math')查看math模块的用处
help(a)查看列表list帮助信息
dir(help)
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
a
①.0
next(it)
id(a)
a=["tom","marry","leblan"]
list(enumerate(a))
oct(10)
①.0. bin() 返回一个整数 int 或者长整数 long int 的二进制表示
bin(10)
'0b1010'
'0b1111'
①.1.eval() 函数用来执行一个字符串表达式,并返回表达式的值
f=open('test.txt')
bool()
False
bool(1)
True
bool(10)
bool(10.0)
isinstance(a,int)
isinstance(a,str)
class ? User(object):
? ? def__init__(self):
class Persons(User):
? ? ? ? ? super(Persons,self).__init__()
float(1)
①0
float(10)
①.0.0
iter(a)
for i in iter(a):
... ? ? ? ? print(i)
...
tuple(a)
s = "playbasketball"
len(s)
len(a)
class User(object):
? ?def __init__(self,name):
? ? ? ? ? ? self.name = name
? def get_name(self):
? ? ? ? ? ? return self.get_name
? @property
? ?def name(self):
? ? ? ? ? ?return self_name
list(b)
range(10)
range(0, 10)
class w(object):
a = w()
getattr(a,'s')
complex(1)
(1◆0j)
complex("1")
max(b)
class Num(object):
...? ? a=1
.. print1 = Num()
print('a=',print1.a)
a= 1
print('b=',print1.b)
print('c=',print1.c)
delattr(Num,'b')
Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'
hash("tom")
a= set("tom")
b = set("marrt")
a,b
({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})
ab#交集
{'t', 'm'}
a|b#并集
{'t', 'm', 'r', 'o', 'a'}
a-b#差集
{'o'}
用Python把某一目录下的文件复制到指定目录中,代码如下:
①.、首先插入必要的库:
import?os?
import?os.path?
import?shutil?
import?time,?datetime
def?copyFiles(sourceDir,targetDir):?
if?sourceDir.find(".svn")?0:?
return?
for?file?in?os.listdir(sourceDir):?
sourceFile?=?os.path.join(sourceDir,file)?
targetFile?=?os.path.join(targetDir,file)?
if?os.path.isfile(sourceFile):?
if?not?os.path.exists(targetDir):?
os.makedirs(targetDir)?
if?not?os.path.exists(targetFile)?or(os.path.exists(targetFile)?and?(os.path.getsize(targetFile)?!=?os.path.getsize(sourceFile))):?
open(targetFile,"wb").write(open(sourceFile,"rb").read())?
if?os.path.isdir(sourceFile):?
First_Directory?=?False?
copyFiles(sourceFile,?targetFile)
if?__name__?=="__main__":?
print?"Start(S)?or?Quilt(Q)?\n"?
flag?=?True?
while?(flag):?
answer?=?raw_input()?
if?'Q'?==?answer:?
flag?=?False?
elif?'S'==?answer?:?
formatTime?=?getCurTime()?
targetFoldername?=?"Build?"?◆?formatTime?◆?"-01"?
Target_File_Path?◆=?targetFoldername
copyFiles(Debug_File_Path,Target_File_Path)?
removeFileInFirstDir(Target_File_Path)?
coverFiles(Release_File_Path,Target_File_Path)?
moveFileto(Firebird_File_Path,Target_File_Path)?
moveFileto(AssistantGui_File_Path,Target_File_Path)?
writeVersionInfo(Target_File_Path◆"\\ReadMe.txt")?
print?"all?sucess"?
else:?
print?"not?the?correct?command"
我们都知道python中可以是threading模块实现多线程, 但是模块并没有提供暂停, 恢复和停止线程的方法, 一旦线程对象调用start方法后, 只能等到对应的方法函数运行完毕. 也就是说一旦start后, 线程就属于失控状态.
我们日常所说的复制(自己在电脑硬盘上的复制)就是深复制(deepcopy),即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以改变原有被复制对象不会对已经复制出来的新对象产生影响.
而浅复制(copy)并不会产生一个独立的对象单独存在,他只是将原有的数据块打上一个新标签,所以当其中一个标签指向的数据块就会发生变化,另一个标签也会随之改变.这就和我们寻常意义上的复制有所不同了.
字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射.字典类型是Python中唯一内建的映射类型,基本的操作包括如下:
(1)len():返回字典中键—值对的数量;
(10)has_key函数:检查字典中是否含有给出的键
(11)items和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表
第一段:字典的创建
①1 直接创建字典
printd
printd['two']
printd['three']
运算结果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
printu'items中的内容:'
printitems
printu'利用dict创建字典,输出字典内容:'
d=dict(items)
printu'查询字典中的内容:'
printd['one']
items中的内容:
利用dict创建字典,输出字典内容:
查询字典中的内容:
或者通过关键字创建字典
printu'输出字典内容:'
输出字典内容:
第二段:字典的格式化字符串
print"three is %(three)s."%d
第三段:字典方法
d.clear()
{}
请看下面两个例子
d={}
dd=d
d['one']=1
printdd
printu'初始X字典:'
printx
printu'X复制到Y:'
y=x.copy()
printu'Y字典:'
printy
printu'修改Y中的值,观察输出:'
printu'删除Y中的值,观察输出'
y['test'].remove('c')
初始X字典:
X复制到Y:
Y字典:
修改Y中的值,观察输出:
删除Y中的值,观察输出
注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改.deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题.
fromcopyimportdeepcopy
x={}
x['test']=['a','b','c','d']
z=deepcopy(x)
printu'输出:'
printz
printu'修改后输出:'
x['test'].append('e')
运算输出:
输出:
{'test': ['a','b','c','d']}
修改后输出:
{'test': ['a','b','c','d','e']}
d=dict.fromkeys(['one','two','three'])
{'three':None,'two':None,'one':None}
或者指定默认的对应值
d=dict.fromkeys(['one','two','three'],'unknow')
{'three':'unknow','two':'unknow','one':'unknow'}
printd.get('one')
printd.get('four')
None
注:get函数可以访问字典中不存在的键,当该键不存在是返回None
printd.has_key('one')
printd.has_key('four')
list=d.items()
forkey,valueinlist:
printkey,':',value
one :1
it=d.iteritems()
fork,vinit:
print"d[%s]="%k,v
d[one]=1
printu'keys方法:'
list=d.keys()
printlist
printu'\niterkeys方法:'
it=d.iterkeys()
forxinit:
keys方法:
['three','two','one']
iterkeys方法:
three
two
one
d.pop('one')
d.popitem()
printd.setdefault('one',1)
d={
}
x={'one':1}
d.update(x)
printd.values()
以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!