我给航母来取名,奶牛养殖可行性报告,ipz-225
python 2.7 的所有内置函数共有80个。熟练记住和使用这些内置函数,将大大提高写python代码的速度和代码的优雅程度。
以下代码示例用的是ipython,一个比官方解释器好很多的解释器,值的学习和使用。
in [18]: abs(3.14) out[18]: 3.14 in [19]: abs(-3.14) out[19]: 3.14
in [135]: complex(1,3) out[135]: (1+3j)
in [143]: divmod(12, 7) out[143]: (1, 5)
in [157]: max([(1,2,3), (4,5,6), (23,4,1,)], key=lambda a: a[-1]) out[157]: (4, 5, 6) in [158]: max(1,2,3,4,4,5) out[158]: 5 in [159]: max([(1,2,3), (4,5,6), (23,4,1,)]) out[159]: (23, 4, 1) in [160]: max([(1,2,3), (4,5,6), (23,4,1,)], key=lambda a: a[-1]) out[160]: (4, 5, 6) in [161]: max([{'age':10, 'name': 'aaa'}, {'age': 12, 'name': 'bb'}], key=lambda a: a['age']) out[161]: {'age': 12, 'name': 'bb'}
in [166]: pow(2,3) out[166]: 8 in [167]: pow(2,3,5) out[167]: 3
in [170]: round(3.45) out[170]: 3.0 in [171]: round(3.55) out[171]: 4.0 in [172]: round(3.55345, 3) out[172]: 3.553
in [175]: sum([1,2,3,4]) out[175]: 10
in [204]: print bin(20), hex(16), oct(9) 0b10100 0x10 011
in [184]: print bool(3), bool('a') true true in [185]: print bool(0), bool(''), bool(none) false false false
in [188]: chr(320) --------------------------------------------------------------------------- valueerror traceback (most recent call last) <ipython-input-188-5b2996ffe50c> in <module>() ----> 1 chr(320) valueerror: chr() arg not in range(256) in [189]: chr(65) out[189]: 'a' in [190]: chr(0) out[190]: '\x00'
in [225]: unichr(1245) out[225]: u'\u04dd'
in [192]: ord('a') out[192]: 97 in [193]: ord('\x23') out[193]: 35
in [196]: print float('13'), float(13) 13.0 13.0 in [197]: print int('14'), int(14) 14 14 in [198]: print long('15'), long(15) 15 15
in [212]: format(123, '05d') out[212]: '00123'
以上等同于 print ‘%05d’ % 123
in [218]: hash(123) out[218]: 123 in [219]: hash('abc') out[219]: 1453079729188098211
in [221]: str(123) out[221]: '123' in [222]: str([1,2,3]) out[222]: '[1, 2, 3]' in [223]: str({'a': 1, 'b': 2}) out[223]: "{'a': 1, 'b': 2}"
in [251]: file('abc.txt', 'w') out[251]: <open file 'abc.txt', mode 'w' at 0x7f93e727a660> in [252]: open('abc.txt', 'w') out[252]: <open file 'abc.txt', mode 'w' at 0x7f93e727a780>
in [253]: input('pls input a number >>') pls input a number >>123 out[253]: 123
in [255]: all([1,2,3,4]) out[255]: true in [256]: all([1,2,3,4, 0]) out[256]: false in [257]: any([1,2,3,4, 0]) out[257]: true
in [261]: for i, value in enumerate(['a', 'b', 'c']): .....: print i, value .....: 0 a 1 b 2 c
in [263]: filter(lambda x: x>3, [1,2,3,4,5]) out[263]: [4, 5]
读取文件的时候比较有用:
with open("mydata.txt") as fp: for line in iter(fp.readline, "stop"): process_line(line)
in [267]: len('abc'), len([1,2,3]) out[267]: (3, 3)
in [269]: map(lambda x: x+3, [1,2,3]) out[269]: [4, 5, 6] in [270]: a = [1,2]; b = ['a', 'b']; c = ('x', 'y') in [271]: map(none, a, b, c) out[271]: [(1, 'a', 'x'), (2, 'b', 'y')]
in [281]: reduce(lambda a, b: a-b, [1,2,3]) out[281]: -4
in [283]: zip([1,2,3], ('a', 'b', 'c')) out[283]: [(1, 'a'), (2, 'b'), (3, 'c')] range() xrange() 返回一个整数序列 in [274]: [x for x in xrange(10)] out[274]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in [275]: [x for x in xrange(5, 10)] out[275]: [5, 6, 7, 8, 9] in [276]: [x for x in xrange(5, 10, 2)] out[276]: [5, 7, 9] in [277]: range(10) out[277]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] in [278]: range(5, 10) out[278]: [5, 6, 7, 8, 9] in [279]: range(5, 10, 2) out[279]: [5, 7, 9]
可选参数cmp、key和reverse与list.sort()方法的参数含义相同(在可变的序列类型一节描述)。
cmp指定一个自定义的带有两个参数的比较函数(可迭代的元素),它应该根据第一个参数是小于、等于还是大于第二个参数返回负数、零或者正数:cmp=lambda x,y: cmp(x.lower(), y.lower())。默认值是none。
key指定一个带有一个参数的函数,它用于从每个列表元素选择一个比较的关键字:key=str.lower。默认值是none(直接比较元素)。
reverse是一个布尔值。如果设置为true,那么列表元素以反向比较排序。
通常情况下,key和reverse转换处理比指定一个等同的cmp函数要快得多。这是因为cmp为每个元素调用多次但是key和reverse只会触摸每个元素一次。使用functools.cmp_to_key()来转换旧式的cmp函数为key函数。
in [288]: sorted(d.items(), key=lambda a: a[1]) out[288]: [('a', 3), ('b', 4)] in [289]: sorted(d.items(), key=lambda a: a[1], rev) in [289]: sorted(d.items(), key=lambda a: a[1], reverse=true) out[289]: [('b', 4), ('a', 3)] in [290]: sorted(d.items(), cmp=lambda a, b: cmp(a[1], b[1])) out[290]: [('a', 3), ('b', 4)]
bytearray() dict() frozenset() list() set() tuple()
python里面常用的数据结构有列表(list)、字典(dict)、集合(set)、元组(tuple)
以下是一些类(class)和类型相关的函数,比较不常用,可以查看手册详细了解。
basestring() callable() classmethod() staticmethod() property() cmp() compile() delattr() getattr() setattr() hasattr() dir() globals() locals() vars() help() id() isinstance() issubclass() object() memoryview() repr() super() type() unicode() import() eval() execfile()
apply() buffer() coerce() intern()
ipython是一个非常好的交互式python解释器,它查看一个函数或类的用法的方法有:
in [292]: import time in [293]: time. time.accept2dyear time.clock time.gmtime time.sleep time.struct_time time.tzname time.altzone time.ctime time.localtime time.strftime time.time time.tzset time.asctime time.daylight time.mktime time.strptime time.timezone in [293]: time.ti time.time time.timezone in [293]: time.time? docstring: time() -> floating point number return the current time in seconds since the epoch. fractions of a second may be present if the system clock provides them. type: builtin_function_or_method
文章首发于我的个人博客:
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论