python 内置方法

句柄

f = open('1234567',encoding='utf-8')
print(f)

<_io.TextIOWrapper name='1234567' mode='r' encoding='utf-8'>

f 就是一个文件句柄

handler , 文件操作符 , 文件句柄

内置函数

dir()                             查看一个变量拥有的方法

l = []
print(dir(l))

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

callable()                     查看一个名字是不是函数 是返回True 不是返回False

l = []
def f():
    pass
print(callable(l))
print(callable(f))

False
True

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

import                           调用模块

某个方法属于某个数据类型的变量,就用.调用

如果某个方法不依赖于任何数据类型,就直接调用——内置函数和自定义函数

.writable                       能不能写

.readable                      能不能读

hash                             可哈希

可哈希的能执行输出哈希值(一串数字会输出原数字,不会计算哈希值),不可哈希的会报错

print(hash(123456))
print(hash('123asd'))

123456
-4429422377593575845

print(hash(123456))
print(hash('123asd'))
print(hash([1,2,3,4]))

input()                       等待用户输入,并将用户的输入内容返回,并可以设置提示

ret = input('请输入:')
print(ret)

print()                        输出

g = 123456789
print(g)
print(g)

123456789
123456789

print所包含的:

end = ' '                     指定输出的结束符,默认的是换行

g = 123456789
print(g,end='')
print(g,end='')

123456789123456789

sep = ' '                     指定输出多个值之间的分隔符

print(1,2,3,4,5,6,7,8,9,sep='*')

1*2*3*4*5*6*7*8*9

file = ' '                      输出到指定文件句柄,输出在文件里面

f = open('1234567','w')
print('123456789',file=f)
f.close()

exec                               没有返回值         简单的流程控制

eval                                有返回值            有结果的简单计算

exec和eval都可以执行看起来是字符串的python代码

eval只能用在呢明确知道你要执行的代码是什么

complex 复数

float        浮点数

abs()       绝对值

print(abs(-9))
print(abs(7))

9
7

divmod()                 div除法 mod取余   除余

print(divmod(7,2))
print(divmod(15,6))

(3, 1)
(2, 3)

round()                   做精确值 保留小数点后几位

print(round(3.1415926,4))

3.1416

pow(x,y,z)               x,y做幂运算 并对z取余

print(pow(3,3))
print(pow(3,3,7))

27
6

sum()                   求和

ret = sum([1,2,3,4,5])
print(ret)

15

min()                    求最小值

ret = min([1,2,3,4,5])
print(ret)

1

max()                   求最大值

ret = max([1,2,3,4,5])
print(ret)

5

posted @ 2018-01-04 19:09  GrandDarkness  阅读(206)  评论(0编辑  收藏  举报