Python内置函数:

官方帮助文档:

https://docs.python.org/2.7/

返回数字的绝对值.

def fun(x):

if x < 0:

return -x

return x

print fun(10)

常用函数:

abs()

>>> abs(-100)

100

取列表最大值和最小值

max()

>>> max('1235',123)

'1235'

min()

>>> min('asdfq3w45')

'3'

len()

>>> len('abcdf')

5

>>> len([1,3,4,5])

4

>>> len((1,3,4,5))

4

>>> len({1:3,2:5})

2

divmod()

>>> help(divmod)

Help on built-in function divmod in module __builtin__:

divmod(...)

divmod(x, y) -> (quotient, remainder)

Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.

>>> divmod(5,2)

(2, 1)

pow()

pow(...)

pow(x, y[, z]) -> number

With two arguments, equivalent to x**y.  With three arguments,

equivalent to (x**y) % z, but may be more efficient (e.g. for longs).

>>> pow(2,3)

8

>>> pow(2,3,3)

2

round()

round(...)

round(number[, ndigits]) -> floating point number

Round a number to a given precision in decimal digits (default 0 digits).

This always returns a floating point number.  Precision may be negative.

>>> round(12.2)

12.0

>>> round(12.23)

12.0

>>> round(12.233,2)

12.23

callable()

是否是可调用对象

>>> a = 123

>>> callable(a)

False

>>> def a():

...     pass

...

>>> callable(a)

True

>>> class A(object):

...     pass

...

>>> callable(A)

True

type()

判断类型

>>> type(a)

<type 'function'>

isinstance()

判断类型,

>>> l =[1,2,3]

>>> isinstance(l,list)

True

>>> isinstance(l,str)

False

>>> isinstance(l,(list,str))

True

判断是不是一个类

>>> A

<class 'A'>

>>> a = A()

>>> a

<A object at 0x0379BE70>

>>> isinstance(a,A)

True

cmp()

>>> cmp(1,2)

-1

>>> cmp(1,0)

1

>>> cmp(1,1)

0

>>> cmp('a','ab')

-1

>>> cmp('a','a')

0

>>> cmp('helloa','hello')

1

range()

>>> a = range(10)

>>> a

xrange()

效率更高,不用时候不在内存中产生值

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> b = xrange(10)

>>> b

xrange(10)

>>> for i in b:print i

...

0

1

2

3

4

5

6

7

8

9

int()

>>> int(123.33)

123

long()

>>> long(200)

200L

float()

>>> float('123')

123.0

>>> float('123.0022')

123.0022

>>> float(123.0034)

123.0034

>>> float(123)

123.0

complex()

转换成复数

>>> complex(123)

(123+0j)

>>> complex(3.1415926)

(3.1415926+0j)

str()

>>> str('123')

'123'

list()

>>> list('123')

['1', '2', '3']

tuple()

>>> tuple('123')

('1', '2', '3')

hex()

变为16进制

>>> hex(10)

'0xa'

>>> hex(10L)

'0xaL'

>>> int(0xaL)

10

eval()

把字符串当成有效表达式求值。

>>> eval('0xaL')

10L

>>> eval("[1,23,'a']")

[1, 23, 'a']

oct()

10进制转成8进制

>>> oct(10)

'012'

>>> oct(8)

'010'

chr()

查ASSIC码对应值:

>>> chr(97)

'a'

>>> chr(65)

'A'

ord()

>>> ord('A')

65

字符串处理的函数:

str.capitalize()

首字母变大写:

capitalize(...)

S.capitalize() -> string

Return a copy of the string S with only its first character

capitalized.

>>> s

'hello'

>>> s.capitalize()

'Hello'

str.replace()

replace(...)

S.replace(old, new[, count]) -> string

Return a copy of string S with all occurrences of substring

old replaced by new.  If the optional argument count is

given, only the first count occurrences are replaced.

>>> s = 'hello,h'

>>> s.replace('h','H')

'Hello,H'

split()

split(...)

S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the

delimiter string.  If maxsplit is given, at most maxsplit

splits are done. If sep is not specified or is None, any

whitespace string is a separator and empty strings are removed

from the result.

>>> s = 'hello a\tb\nc'

>>> s

'hello a\tb\nc'

>>> s.split()

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

>> s

'hello a\tb\nc'

>>> s.split(' ')

['hello', 'a\tb\nc']

>>> s.split('\t')

['hello a', 'b\nc']

>>> ip = '192.168.1.1'

>>> ip.split('.')

['192', '168', '1', '1']

>>> ip.split('.',1)

['192', '168.1.1']

>>> ip.split('.',2)

['192', '168', '1.1']

join()

>>> range(10)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> ''.join(str(i) for i in range(10))

'0123456789'

>>> int(''.join(str(i) for i in range(10)))

123456789

string:

import string

string.lower

>>> string.lower('Kfdfa')

'kfdfa'

string.upper

>>> string.upper('Kfdfa')

'KFDFA'

    string.capitalize()

    >>> string.capitalize('adfafgh')

'Adfafgh'

    string.replace()

>>> string.replace('afkgha','a','A')

'AfkghA'

序列处理函数:

len()

max()

min()

filter()

filter(...)

filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If

function is None, return the items that are true.  If sequence is a tuple

or string, return the same type, else return a list.

>>> filter(None,range(10))

[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> def f(x):

...     if x % 2 == 0:

...         return True

...

>>> filter(f,range(10))

[0, 2, 4, 6, 8]

>>> filter(lambda x: x%2==0,range(10))

[0, 2, 4, 6, 8]

zip()

zip(...)

zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th element

from each of the argument sequences.  The returned list is truncated

in length to the length of the shortest argument sequence.

>>> a1 = [1,3,4]

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

>>> zip(a1,a2)

[(1, 'a'), (3, 'b'), (4, 'c')]

>>> dict(zip(a1,a2))

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

>>> dict(zip(a2,a1))

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

>>> a3 = ['x','y','z']

>>> zip(a1,a2,a3)

[(1, 'a', 'x'), (3, 'b', 'y'), (4, 'c', 'z')]

>>> zip(a1,a3)

[(1, 'x'), (3, 'y'), (4, 'z')]

>>> a3 = ['x','y']

>>> zip(a1,a3)

[(1, 'x'), (3, 'y')]

>>> zip(a1,a2,a3)

[(1, 'a', 'x'), (3, 'b', 'y')]

map()

map(...)

map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of

the argument sequence(s).  If more than one sequence is given, the

function is called with an argument list consisting of the corresponding

item of each sequence, substituting None for missing values when not all

sequences have the same length.  If the function is None, return a list of

the items of the sequence (or a list of tuples if more than one sequence).

参数有几个,函数里的参数也应该对应有几个

>>> map(None,a1,a2,a3)

[(1, 'a', 'x'), (3, 'b', 'y'), (4, 'c', None)]

>>> def f(x):

...     return x**2

...

>>> map(f,a1)

[1, 9, 16]

>>> a1

[1, 3, 4]

>>> a1

[1, 3, 4]

>>> a2

[2, 5, 6]

>>> def f(x,y):

...     return x*y

...

>>> map(f,a1,a2)

[2, 15, 24]

>>> map(lambda x,y: x*y ,range(1,10),range(1,10))

[1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce()

reduce(...)

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,

from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5).  If initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.

>>> def f(x,y):

...     return x + y

...

>>> reduce(f,range(1,101))

5050

列表表达式:

[i*2 for i in range(10)]

>>> [i*2 for i in range(10)]

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

>>> [i*2+10 for i in range(10)]

[10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

[i*2+10 for i in range(10) if i%3 == 0]

>>> [i*2+10 for i in range(10) if i%3 == 0]

[10, 16, 22, 28]

转载于:https://blog.51cto.com/fengyunshan911/2058835

Python 内建函数相关推荐

  1. python内建函数有哪些_享学课堂浅谈Python序列内建函数都有哪些

    python的内建函数和库函数的区别是什么?人在得不到的时候,什么都可以不介意.得到之后,什么都有点介意. python.什么是内建函数? python.什么是内建函数?新手小白自学python.不懂 ...

  2. Python内建函数与对象方法

    博客 学院 下载 GitChat 论坛 问答 商城 VIP 活动 招聘 ITeye 码云 CSTO 写博客 发Chat Python内建函数与对象方法 转载 2016年06月16日 22:14:20 ...

  3. python内建函数是什么意思_python、什么是内建函数?

    展开全部 python内建函数总结 1. abs(x) abs()函数返回数字2113(可为普通型.长整5261型或浮点型)的绝对值.如果给出复4102数,1653返回值就是该复数的模.例如: > ...

  4. Python内建函数之——filter,map,reduce

    原文链接:http://blog.csdn.net/prince2270/article/details/4681299 在讲述filter,map和reduce之前,首先介绍一下匿名函数lambda ...

  5. Python 内建函数 max/min的高级用法

    max/min 函数 Python 中的 max/min函数是用来获取传入参数的最大值或最小值的,其运行机制都一样,所以这里主要描述 max 的运行机制以及使用方法. 最简单的用法,就是直接传入两个要 ...

  6. python内建函数是什么意思_Python3内建函数简介,Python3内建函数详解

    Python3的内建函数有数十个之多,内建函数是区别于我们的自定义函数,是Python自带的函数,可直接使用. (1)abs()函数 用途:abs函数返回数字的绝对值. 用法:abs(x) 参数:参数 ...

  7. python内建函数是什么意思_Python 自省相关的内建函数和属性

    见贤思齐焉,见不贤而内自省也. –<论语·里仁> 在计算机编程中,自省是指这种能力:检查某些事物以确定它是什么.它知道什么以及它能做什么.这里简单列举和介绍 Python 自省相关的内建函 ...

  8. python内建函数测试对象身份_Python学习笔记 03 Python对象

    1.Python对象 Python对象都拥有三个特性:身份.类型和值. 身份:每一个对象都有一个唯一的身份标识自己,任何对象的身份都可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址. ...

  9. Python学习日记(3)Python内建函数map()、filter()和匿名函数表达式

    内建函数map() 这个函数接收一个函数和一个可迭代对象作为参数,并以某种方式应用这个函数. old_list = [11,22,33] old_str = "test"new_l ...

  10. python内建函数是什么意思_python内建函数是什么意思

    python的内建函数即是python自带的函数,这种函数不需要定义,并且不同的内建函数具有不同的功能,可以直接使用. 以下是部分内建函数用法及说明 1.abs(),返回数字的绝对值. 2.all() ...

最新文章

  1. JVM调优总结(7):调优方法
  2. MapReduce计数器
  3. 大数据笔记11:MapReduce的运行流程
  4. 小觅双目相机如何使用_MYNT EYE S小觅双目摄像头标准(彩色)版结构光双目深度惯导相机3D...
  5. android 透明主题 crash,Android 8.0 的填坑(透明的activity崩溃)
  6. 笨办法学 Python · 续 第四部分:进阶项目
  7. 推荐安卓开发神器(里面有各种UI特效和实例)
  8. 疫情之下 SaaS 市场两极分化,SaaS 厂商如何突围严峻形势?
  9. 5G 还未商用,6G 研究已开始?!
  10. 谷歌研究员发现新的 iOS 安全系统
  11. 10款概念手机,哪款是你的最爱
  12. linux nfs限制连接数,linux – 对NFS有一个有效的稳定性参数吗?
  13. 一篇述说“山寨”的文章,转过来大家看看。
  14. 【折腾向】手动更换笔记本散热铜管
  15. 先写接口文档还是先开发
  16. win10锁屏壁纸提取保存
  17. 前端——阿里图标的使用详解
  18. 尤雨溪:先学算法,再学源码!
  19. Mybatis-----实验小结
  20. wepy(minUI)框架学习

热门文章

  1. Javascript面向对象编程(一):封装
  2. Win32中GDI+应用(三)---Graphics类
  3. HTML5学习笔记三
  4. 韩军为花荣的《操盘手》写的序,不错!很有枭雄味道
  5. WCF扩展:行为扩展Behavior Extension一
  6. C# 要调用VC++ DLL时参数问题请看这个
  7. Linux 中如何复制和删除文件夹中的所有文件?
  8. [Hadoop in China 2011] 何鹏:Hadoop在海量网页搜索中应用分析
  9. 基于clang插件的一种iOS包大小瘦身方案
  10. lucene索引合并与增量索引