4.8Python内置函数

4.8Python内置函数
example4.8.1

>>> #返回数字绝对值 abs()
>>> abs(-520)
520
>>> abs
example4.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)
7
example4.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)
6
example4.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)
520
example4.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)
True
example4.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')
1
example4.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 defined
example4.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值