今天整理的内容是Python的一些内置函数。下面这张图是老师在课堂上的一张PPT:
从图中我们可以看到,Python有68个内置函数,其中标有下划线的是老师要求必须记牢的,其实这些也是我们在使用Python时会用到最多的一些内置函数。图片下面的链接是老师推荐给我们去学习了解的关于Python内置函数的一篇博客。看到有博主将这68个内置函数归为10类,感觉很细致很清晰,在此就仿照这种格式来进行汇总了。
>>>abs(-1)
1
>>>abs(1)
1
>>>abs(0)
0
>>>divmod(15,2)
(7, 1)
>>>divmod(2.7,2) #若为浮点数,则返回的商和余的值也均为浮点数
(1.0, 0.7)
>>>sum(1,2,3,4)
10
>>>pow(2, 10) #等价于 2 ** 10
1024
>>>pow(2, 3, 5) #等价于 (2 ** 3) % 5
3
>>>round(3.14159) #若没有参数,则默认保留0位小数
3
>>>round(3.14159,2) # 表示保留2位小数
3.14
>>>round(2586,-2)
2600 #表示从小数点左边第2位四舍五入
>>>min(1,2,3,4,5)
1
>>>min('12345')
'1'
>>>min(-1, -2, key = abs) #比较两数的绝对值大小,返回绝对值小的数
-1
>>> int() #不传入参数时,得到结果0
0
>>> int(3)
3
>>> int(3.7)
3
>>> float() #不传入参数的时候,得到结果0.0
0.0
>>> float(3)
3.0
>>> float('3')
3.0
>>> complex()
0j
>>> complex(1, 2)
(1+2j)
>>> complex('1+2j')
(1+2j)
>>> str()
''
>>> str(None)
'None'
>>> str(abc)
'abc'
>>> str(123)
'123'
>>> ord('a')
97
>>> chr(97) #参数类型为整数
'a'
>>>bool()
False
>>>bool(0)
False
>>>bool(1)
True
>>>bool(7)
True
#参数在空、0、False等值时才会返回False
>>> bin(2)
'0b10'
>>> oct(11)
'0o13'
>>> hex(15)
'0xf'
>>> tuple() #不传入参数时创建空元组
()
>>> tuple('123') #传入可迭代对象。使用其元素创建新的元组
('1','2','3')
>>>list() #不传入参数时创建空列表
[]
>>> list('abc') #传入可迭代对象,使用其元素创建新的列表
['a','b','c']
>>> dict() #不传入任何参数时返回空字典。
{}
>>> dict(a = 1,b = 2) #可以传入键值对创建字典。
{'b': 2,'a': 1}
>>> dict(zip(['a','b'],[1,2])) #可以传入映射函数创建字典。
{'b': 2,'a': 1}
>>> dict((('a',1),('b',2))) #可以传入可迭代对象创建字典。
{'b': 2,'a': 1}
>>>set() #不传入参数时创建空集合
set()
>>> a = set(range(5)) #传入可迭代对象,创建集合
>>> a
{0,1,2,3,4}
>>> a = range(5); a
range(0, 5)
>>> list(a)
[0, 1, 2, 3, 4]
>>> all([1,2]) #列表中每个元素逻辑值均为True,返回True
True
>>> all([0,1,2]) #列表中0的逻辑值为False,返回False
False
>>> any([0,1,2]) #列表元素有一个为True,则返回True
True
>>> any([0,0]) #列表元素全部为False,则返回False
False
>>> a = list(range(1,10)) #定义序列
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> def if_even(x): #定义偶数判断函数
return x%2==0
>>> list(filter(if_even,a)) #筛选序列中的偶数
[2,4,6,8]
>>> 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.
>>> dir(int)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
>>> a = 'today'
>>> id(a)
51891680
>>> hash('Python')
-161550816
>>> len('abc123$,.')
9
>>> len({'a':1, 'b':2, 'c':3})
3
>>> len(range(5))
5
>>> format(5,'b') #转换成二进制
'110'
>>> format(97,'c') #转换unicode成字符
'a'
>>> format(77,'d') #转换成10进制
'77'
>>> format(17,'o') #转换成8进制
'21'
>>> type(1.7) # 返回对象的类型
__import__
():动态导入模块index = __import__('index')
index.sayHello()
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': , 'a': 'today'}
>>> eval('1+2+3')
6
>>> exec('a=1+2+3') #执行语句
>>> a
6
内容来源于:1.Python内置函数功能汇总
2.Python内置函数详解——总结篇
3.图片中博客