资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python内置函数作用 python 的内置函数

python内置函数什么用

Python内置函数有很多,下面给你例举了几种python3.4版本的内置函数:

创新互联建站主要从事成都做网站、网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务大余,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

1、abx(x)

返回一个数的绝对值。参数可以是普通的整数,长整数或者浮点数。如果参数是个复数,返回它的模

2、all(iterable)

如果iterable的所有元素为真(或者iterable为空), 返回True。等同于:

def all(iterable):

for element in iterable:

if not element:

return False

return True

3、any(iterable)

如果iterable的任一元素为真,返回True。如果iterable为空,返回False。等同于:

def any(iterable):

for element in iterable:

if element:

return True

return False

4、ascii(object)

这个函数跟repr()函数一样,返回一个可打印的对象字符串方式表示。当遇到非ASCII码时,就会输出\x,\u或\U等字符来表示。与Python 2版本里的repr()是等效的函数。

print(ascii(10), ascii(9000000), ascii('b\31'), ascii('0x\1000'))

10 9000000 'b\x19' '0x@0'

5、bin(x)

将一个整数转化成一个二进制字符串。结果是一个合法的Python表达式。如果x不是一个Python int对象,它必须定义一个返回整数的__index__()方法

6、bool([x])

将一个值转化成布尔值,使用标准的真值测试例程。如果x为假或者没有参数,它返回False;否则它返回True。bool也是一个类,它是int的子类。bool不能被继承。它唯一的实例就是False和True

python内置函数

python内置函数是什么?一起来看下吧:

python内置函数有:

abs:求数值的绝对值

abs(-2) 2

pmod:返回两个数值的商和余数

pmod(5,2) (2,1) pmod(5.5,2) (2.0,1.5)

bool:根据传入的参数的逻辑值创建一个布尔值

bool() #未传入参数 False bool(0) #数值0、空序列等值为False False bool(1) True

all:判断可迭代对象的每个元素是否都为True值

all([1,2]) #列表中每个元素逻辑值均为True,返回True True  all(()) #空元组 True  all({}) #空字典 True

help:返回对象的帮助信息

help(str)  Help on class str in module builtins: class str(object) |  str(object='') - str |  str(bytes_or_buffer[, encoding[, errors]]) - str |   |  Create a new string object from the given object. If encoding or |  errors is specified, then the object must expose a data buffer |  that will be decoded using the given encoding and error handler. |  Otherwise, returns the result of object.__str__() (if defined) |  or repr(object). |  encoding defaults to sys.getdefaultencoding(). |  errors defaults to 'strict'. |   |  Methods defined here: |   |  __add__(self, value, /)           Return self+value.

_import_:动态导入模块

index = __import__('index') index.sayHello()

locals:返回当前作用域内的局部变量和其值组成的字典

def f():     print('before define a ')     print(locals()) #作用域内无变量     a = 1     print('after define a')     print(locals()) #作用域内有一个a变量,值为1  f f() before define a  {}  after define a {'a': 1}

input:读取用户输入值

s = input('please input your name:') please input your name:Ain  s 'Ain'

open:使用指定的模式和编码打开文件,返回文件读写对象

# t为文本读写,b为二进制读写  a = open('test.txt','rt')  a.read() 'some text'  a.close()

eval:执行动态表达式求值

eval('1+2+3+4') 10

除了上述举例的函数之外,内置函数按分类还可分为:

1、数学运算(7个)

2、类型转换(24个)

3、序列操作(8个)

4、对象操作(7个)

5、反射操作(8个)

6、变量操作(2个)

7、交互操作(2个)

8、文件操作(1个)

9、编译操作(4个)

10、装饰器(3个)

python里面pow函数作用是什么?

pow()函数,是Python的内置函数,它计算并返回x的y次方的值。

import math

math.pow( x, y )

这个函数还有一个用法:

pow(x, y, z)

函数是计算 x 的 y 次方,如果 z 在存在,则再对结果进行取模,其结果等效于 pow(x,y) %z。


名称栏目:python内置函数作用 python 的内置函数
本文来源:http://cdkjz.cn/article/doepoej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220