利用dir(builtins)函数可查看python中的内置函数列表
>>>import builtins #导入内置模块
>>>dir(builtins) #与dir(__builtins__)等价,如果使用dir(__builtins__)的方式,则不用import builtins导入模块
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
>>>abs(5)
5
>>>abs(-5)
5
>>>abs(10+1j)
10.04987562112089
>>>abs(-10+1j)
10.04987562112089
>>>max(10,5,-10,20)
20
>>>min(10,5,-10,20)
-10
>>>str1 = 'hello'
>>>len(str1)
5
>>>len('err13444')
8
>>>list1 = [1,2,3,4,5,'a']
>>>len(list1)
6
>>>map1 = {2:5,'s':6,'b':10}
>>>len(map1)
3
>>>max(1,4,5,6,7,8,10)
10
>>>list1 = [-1,2,4,5,7,8,10,15]
>>>max(list1)
15
>>>min(list1)
-1
>>>list1.append('s')
>>>list1
[-1, 2, 4, 5, 7, 8, 10, 15, 's']
>>>max(list1) #list1类型错误,包含有非数值类型
Traceback (most recent call last):
File "" , line 1, in
max(list1)
TypeError: unorderable types: str() > int()
>>>list1.pop()
's'
>>>list1.append(-2.5)
>>>list1
[-1, 2, 4, 5, 7, 8, 10, 15, -2.5]
>>>min(list1)
-2.5
>>>list1 = [0,1,2,3,4,5,6,7,8,9]
>>>sum(list1)
45
>>>sum(list1,5) #返回序列里的值加上5的值
50
>>>list1.append('s')
>>>sum(list1) #报list1类型错误,包含有非数值类型
Traceback (most recent call last):
File "" , line 1, in
sum(list1)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>sum(1,2) #报类型错误,参数1是int类型,不是一个可迭代的对象
Traceback (most recent call last):
File "" , line 1, in
sum(1,3)
TypeError: 'int' object is not iterable
>>>list1 = [0,1,2,3]
>>>iter1 = reversed(list1)
>>>next(iter1)
3
>>>next(iter1)
2
>>>next(iter1)
1
>>>next(iter1)
0
>>>next(iter1) #迭代对象迭代完成最后一个元素后,报错StopIteration
Traceback (most recent call last):
File "" , line 1, in
next(iter1)
StopIteration
>>>list1 = ['s','h','0']
>>>str1 = '123'
>>>list2 = [9,8,7,6]
>>>zip1 = zip(str1,list1,list2)
>>>next(zip1)
('1', 's', 9)
>>>next(zip1)
('2', 'h', 8)
>>>next(zip1)
('3', '0', 7)
>>>next(zip1) #迭代对象迭代完成最后一个元素后,报错StopIteration
Traceback (most recent call last):
File "" , line 1, in
next(z1)
StopIteration
>>>hex(100)
'0x64'
>>>bin(100)
'0b1100100'
>>>oct(100)
'0o144'
>>>int(100.5) #float型也会转换成去掉小数部分的整形
100
>>>i1 = int(100) #第一种情况,整形
>>>i1
100
>>>type(i1)
<class 'int'>
>>>s1 = hex(i1)
>>>s1
'0x64'
>>>int(s1,base = 16) #第二种情况,字符串,必须指定该字符串的表现形式
100
>>>int(s1,base = 8) #指定的base与字符串不符时,报ValueError错误
Traceback (most recent call last):
File "" , line 1, in
int(s1,base = 8)
ValueError: invalid literal for int() with base 8: '0x64'