您的位置:首页 > 编程语言 > Python开发

Python小白学习之路(十八)—【内置函数三】

2018-11-15 10:23 615 查看

一、对象操作

help()

功能:返回目标对象的帮助信息

举例:
print(help(input))

#执行结果
Help on built-in function input in module builtins:

input(prompt=None, /)
Read a string from standard input. The trailing newline is stripped.

The prompt string, if given, is printed to standard output without a
trailing newline before reading input.

If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.

None

 

dir()

功能:返回对象或者当前作用域内的属性列表

举例:
import math
print(dir(math))

#执行结果
['__doc__', '__loader__', '__name__', '__package__',
'__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh',
'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs',
'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',
'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf',
'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin',
'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

 

id()


功能:返回目标对象的唯一标识符

举例:
a = 'xhg'
b = 'dhg'
print(id(a), id(b))

#执行结果
4512992 4513088

 

hash()

功能:获取目标对象的哈希值

哈希值:

又称:散列函数(或散列算法,又称哈希函数,英语:Hash Function)
是一种从任何一种数据中创建小的数字“指纹”的方法。
散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。
该函数将数据打乱混合,重新创建一个叫做散列值(hash values,hash codes,hash sums,或hashes)的指纹。
散列值通常用一个短的随机字母和数字组成的字符串来代表。
好的散列函数在输入域中很少出现散列冲突。
在散列表和数据处理中,不抑制冲突来区别数据,会使得数据库记录更难找到。

举例:
a = 'xhg'
b = 'dhg'
print(hash(a), hash(b))

#执行结果
-1732189627 760535467

 

type()

功能:
返回对象的类型 type(object) -> the object's type
根据传入的参数创建一个新的类型 type(name, bases, dict) -> a new type

举例:
a = [1, 2, (1, 2), 'xhg']
print(type(a))
#执行结果
<class 'list'>

 

len()

功能:返回对象的长度

举例:
print(len('xhg'), len(bytes('小伙郭','utf-8')), len([1, 2, 3]))
#执行结果
3 9 3

 

ascii()

功能:返回对象的可打印表字符串表现方式

print(ascii('AA'), ascii(123) ,ascii('中国'))
举例:
#执行结果
'AA' 123 '\u4e2d\u56fd'
#‘中国’非ASCII字符

 

format()


功能:格式化
在学习记录九中有详细介绍

vars()

功能:返回当前作用域内的局部变量和其值组成的字典,或者返回对象的属性列表


二、反射操作

isinstance()


功能:判断对象是否是类或者类型元组中任意类元素的实例

print(isinstance('xhg', str))
print(isinstance('xhg', int))

#执行结果
True
False

 

issubclass()


功能:判断类是否是另外一个类或者类型元组中任意类元素的子类

print(issubclass(int, str))
print(issubclass(bool, int))

#执行结果
False
True

 

三、变量操作

globals()

功能:全局变量

locals()

功能:当前作用域变量

四、交互操作

print()

功能:打印输出

input()

功能:读取用户输入值

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐