4.8Python内置函数
2014-02-16 21:45
344 查看
4.8Python内置函数
example4.8.1
类型转化函数
type()
int()
long()
float()
complex() 复数
类型转化函数
str()
list()
tuple()
hex()
oct()
chr()
ord()
注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》;代码执行环境为Win;Python版本为 3.3.3
example4.8.1
>>> #返回数字绝对值 abs() >>> abs(-520) 520 >>> absexample4.8.2
>>>#返回最大值和最小值 max()、min() >>> a = [1,2,3,4,5,520,815] >>> max(a) 815 >>> min(a) 1 >>>example4.8.3
>>> #求字符串长度len() >>> a = [1,2,3,4,5,520,815] >>> s = 'cuixiaohui' >>> len(s) 10 >>> len(a) 7example4.8.4
>>> help(divmod) Help on built-in function divmod in module builtins: divmod(...) divmod(x, y) -> (div, mod) Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. >>>#求商和模divmod() >>> divmod(2,5) (0, 2) >>> divmod(5,2) (2, 1) >>> divmod(100,100) (1, 0)example4.8.5
>>> help(pow) Help on built-in function pow in module builtins: 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 ints). >>> #求次方 >>> pow(3,3) 27 >>> #求次方的模 >>> pow(3,3,7) 6example4.8.6
>>> help(round) Help on built-in function round in module builtins: round(...) round(number[, ndigits]) -> number Round a number to a given precision in decimal digits (default 0 digits). This returns an int when called with one argument, otherwise the same type as the number. ndigits may be negative. >>> round(520) 520example4.8.7
>>> help(callable) Help on built-in function callable in module builtins: callable(...) callable(object) -> bool Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances of classes with a __call__() method. >>> #判断一个对象是否可以用(函数是否定义) >>> callable(f) Traceback (most recent call last): File "<pyshell#52>", line 1, in <module> callable(f) NameError: name 'f' is not defined >>> def f(x): if x< 0: return -x return x >>> callable(f) Trueexample4.8.8
>>> help(isinstance) Help on built-in function isinstance in module builtins: isinstance(...) isinstance(object, class-or-type-or-tuple) -> bool Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for isinstance(x, A) or isinstance(x, B) or ... (etc.). >>> #判断某一对象的类型 >>> a=[1, 2, 3, 4, 5, 520, 815] >>> type(a) <class 'list'> >>> isinstance(a,list) True >>> isinstance(a,int) False >>>example4.8.9
#cmp()在python3.3.3未定义 >>> help(cmp) Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> help(cmp) NameError: name 'cmp' is not defined >>> cmp('lixiaotao','lixiao') Traceback (most recent call last): File "<pyshell#65>", line 1, in <module> cmp('lixiaotao','lixiao') NameError: name 'cmp' is not defined >>> #cmp()是比较两个字符串是否一样 #假设已经定义,举例如下 >>> cmp('hello','hello') 0 >>> cmp('h','hello') -1 >>> cmp('hhhhhhh','hello') 1example4.8.10
#快速生成一个序列range() #xrange()在python3.3.3未定义 >>> range(10) range(0, 10) >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a virtual sequence of numbers from start to stop by step. | | Methods defined here: | | __contains__(...) | x.__contains__(y) <==> y in x | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __iter__(...) | x.__iter__() <==> iter(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __ne__(...) | x.__ne__(y) <==> x!=y | | __reduce__(...) | | __repr__(...) | x.__repr__() <==> repr(x) | | __reversed__(...) | Return a reverse iterator. | | count(...) | rangeobject.count(value) -> integer -- return number of occurrences of value | | index(...) | rangeobject.index(value, [start, [stop]]) -> integer -- return index of value. | Raise ValueError if the value is not present. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | start | | step | | stop | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T >>> help(xrange) Traceback (most recent call last): File "<pyshell#68>", line 1, in <module> help(xrange) NameError: name 'xrange' is not definedexample4.8.11
类型转化函数
type()
int()
long()
float()
complex() 复数
>>>#举例 >>> s='520' >>> type(s) <class 'str'> >>> s + 520 Traceback (most recent call last): File "<pyshell#75>", line 1, in <module> s + 520 TypeError: Can't convert 'int' object to str implicitly >>> int(s) 520 >>> int(s) + 520 1040 >>>example4.8.12
类型转化函数
str()
list()
tuple()
hex()
oct()
chr()
ord()
>>> help(hex) Help on built-in function hex in module builtins: hex(...) hex(number) -> string Return the hexadecimal representation of an integer. >>> help(oct) Help on built-in function oct in module builtins: oct(...) oct(number) -> string Return the octal representation of an integer. >>> help(chr) Help on built-in function chr in module builtins: chr(...) chr(i) -> Unicode character Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. >>> help(ord) Help on built-in function ord in module builtins: ord(...) ord(c) -> integer Return the integer ordinal of a one-character string. >>>
注:学习内容来源于网易云课堂《疯狂的Python:快速入门精讲》;代码执行环境为Win;Python版本为 3.3.3
相关文章推荐
- python模块内置函数 分类: python 2013-04-15 15:17 297人阅读 评论(0) 收藏
- Python标准库:内置函数__import__(name, globals=None, locals=None, fromlist=(), level=0)
- python 内置函数 enumerate
- Python标准库:内置函数object()
- Python两个内置函数——locals 和globals(转)
- python中内置函数和json、pickle数据序列化
- python内置函数|类中的内置方法|字符串内置方法大全
- python 内置函数 : compile()
- Python内置函数(40)——map
- Python内置函数
- python学习笔记011——内置函数filter()
- python(11-3 数据库 表复制 表索引 视图 内置函数)
- Python 的内置函数__import__
- Python标准库:内置函数setattr(object, name, value)
- Python学习-函数补充2-内置函数
- Python内置函数大全
- python 内置函数
- Python开发基础-Day10生成器表达式形式、面向过程编程、内置函数部分
- 第七篇 python基础之函数,递归,内置函数
- [Python学习笔记][Python内置函数]