4.8Python内置函数
来源:互联网 发布:鸟人安德森知乎 编辑:程序博客网 时间:2024/05/28 16:18
4.8Python内置函数
example4.8.1
类型转化函数
type()
int()
long()
float()
complex() 复数
类型转化函数
str()
list()
tuple()
hex()
oct()
chr()
ord()
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 -xreturn 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 + 520Traceback (most recent call last): File "<pyshell#75>", line 1, in <module> s + 520TypeError: Can't convert 'int' object to str implicitly>>> int(s)520>>> int(s) + 5201040>>>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
0 0
- 4.8Python内置函数
- python函数: 内置函数
- python 内置函数
- python内置函数
- Python常用内置函数
- Python常用内置函数
- python内置函数
- python 内置函数
- Python中 内置函数
- python 内置函数
- Python内置函数清单
- python的内置函数
- python模块内置函数
- 【python】内置函数
- python内置函数
- python 内置函数
- python内置函数
- Python内置函数
- UVa11729 Commando War(贪心)
- opencv编程时的三个错误
- Linux ps命令详解
- python理论学习(八)
- 整理音乐
- 4.8Python内置函数
- GitHub的使用(上)—— 创建和更新
- 设计模式六大原则之我见
- 设计模式初探-访问者模式
- java基础之多态
- GitHub的使用(下)—— 如何下载一个已存在的 Repository
- FC协议常见十问
- ASP.NET中前台调用后台、后台调用前台的变量或方法
- read it later的存在必要感