Python有许多内置函数,共有66个。以下是这些内置函数的详细解释和示例代码:
1.abs(x): 返回一个数的绝对值。
2.all(iterable): 如果可迭代对象中所有元素都为真,则返回True;否则返回False。
1 2
| iterable = [True, True, False] print(all(iterable))
|
3.any(iterable): 如果可迭代对象中任何一个元素为真,则返回True;否则返回False。
1 2
| iterable = [False, False, True] print(any(iterable))
|
4.bin(x): 将一个整数转换为二进制字符串。
5.bool(x): 将一个值转换为布尔类型。
6.bytearray([source[, encoding[, errors]]]): 创建一个可变的字节数组对象。
1 2 3
| source = b'Hello' arr = bytearray(source) print(arr)
|
7.bytes([source[, encoding[, errors]]]): 创建一个不可变的字节对象。
1 2 3
| source = 'Hello' b = bytes(source, encoding='utf-8') print(b)
|
8.callable(object): 检查一个对象是否可调用(函数、方法等)。
1 2 3 4
| def func(): pass
print(callable(func))
|
9.chr(i): 返回一个Unicode编码的整数对应的字符。
10.classmethod(func): 将一个普通方法转换为类方法。
1 2 3 4 5 6 7 8
| class MyClass: attr = 10 @classmethod def class_method(cls): print(cls.attr)
MyClass.class_method()
|
11.compile(source, filename, mode[, flags[, dont_inherit]]): 编译源代码为代码或AST对象。
1 2 3
| source = "print('Hello, World!')" code = compile(source, filename="", mode="exec") exec(code)
|
12.complex(real[, imag]): 创建一个复数。
1 2 3 4
| real = 3 imag = 4 c = complex(real, imag) print(c)
|
13.delattr(object, name): 删除对象的属性。
1 2 3 4 5 6
| class MyClass: attr = 10
obj = MyClass() delattr(obj, 'attr') print(hasattr(obj, 'attr'))
|
14.dict([arg]): 创建一个字典。
1 2
| d = dict(a=1, b=2, c=3) print(d)
|
15.dir([object]): 返回一个包含对象所有属性和方法名的列表。
1 2 3 4 5 6 7
| class MyClass: attr = 10 def method(self): pass
obj = MyClass() print(dir(obj))
|
16.divmod(a, b): 返回a除以b的商和余数。
1 2 3 4
| a = 10 b = 3 result = divmod(a, b) print(result)
|
17.enumerate(iterable, start=0): 返回一个枚举对象,包含索引和值。
1 2 3 4 5 6 7
| iterable = ['a', 'b', 'c'] for i, value in enumerate(iterable): print(i, value)
|
18.eval(expression[, globals[, locals]]): 执行一个字符串表达式,并返回结果。
1 2 3
| expression = "2 + 3" result = eval(expression) print(result)
|
19.exec(object[, globals[, locals]]): 执行Python代码。
1 2 3 4 5 6 7 8
| code =""" x = 5 if x > 0: print("Positive") else: print("Non-positive") """ exec(code)
|
20.filter(function, iterable): 使用给定的函数对可迭代对象进行过滤。
1 2 3 4 5 6
| def is_positive(x): return x > 0
numbers = [1, -2, 3, -4, 5] result = filter(is_positive, numbers) print(list(result))
|
21.float(x): 将一个数转换为浮点数。
1 2 3
| value = 3.14159 result = format(value, ".2f") print(result)
|
23.frozenset([iterable]): 创建一个不可变的集合。
1 2 3
| iterable = [1, 2, 3] fs = frozenset(iterable) print(fs)
|
24.getattr(object, name[, default]): 返回对象的属性值。
1 2 3 4 5
| class MyClass: attr = 10
obj = MyClass() print(getattr(obj, 'attr'))
|
25.globals(): 返回当前全局作用域的字典。
26.hasattr(object, name): 检查对象是否有指定的属性。
1 2 3 4 5
| class MyClass: attr = 10
obj = MyClass() print(hasattr(obj, 'attr'))
|
27.hash(object): 返回对象的哈希值。
28.help([object]): 获取对象的帮助信息。
29.hex(x): 将一个整数转换为十六进制字符串。
30.id(object): 返回对象的唯一标识符。
1 2
| name = input("请输入您的姓名:") print("您好," + name + "!")
|
32.int(x=0): 将一个数转换为整数。
33.isinstance(object, classinfo): 检查一个对象是否为指定类或类型元组的实例。
1 2 3 4 5
| class MyClass: pass
obj = MyClass() print(isinstance(obj, MyClass))
|
34.issubclass(class, classinfo): 检查一个类是否为另一个类的子类。
1 2 3 4 5 6 7
| class Parent: pass
class Child(Parent): pass
print(issubclass(Child, Parent))
|
35.iter(iterable[, sentinel]): 创建一个迭代器对象。
1 2 3
| iterable = [1, 2, 3] iterator = iter(iterable) print(next(iterator))
|
36.len(s): 返回一个对象的长度(元素个数)。
1 2
| s = "Hello" print(len(s))
|
37.list([iterable]): 创建一个列表。
1 2 3
| iterable = (1, 2, 3) lst = list(iterable) print(lst)
|
38.locals(): 返回当前局部作用域的字典。
39.map(function, iterable, …): 对可迭代对象中的每个元素应用一个函数。
1 2 3 4 5 6
| def square(x): return x ** 2
numbers = [1, 2, 3, 4, 5] result = map(square, numbers) print(list(result))
|
40.max(iterable[, key]): 返回可迭代对象中的最大值。
1 2
| numbers = [3, 1, 4, 2, 5] print(max(numbers))
|
41.memoryview(obj): 创建一个内存视图对象,用于访问其他对象的内存。
1 2 3
| lst = [1, 2, 3, 4, 5] mv = memoryview(lst) print(mv[0])
|
42.min(iterable[, key]): 返回可迭代对象中的最小值。
1 2
| numbers = [3, 1, 4, 2, 5] print(min(numbers))
|
43.next(iterator[, default]): 返回迭代器中的下一个元素。
1 2 3
| iterable = [1, 2, 3] iterator = iter(iterable) print(next(iterator))
|
44.object(): 返回一个新的空对象。
1 2 3 4 5 6
| obj = object() print(obj) 45.oct(x): 将一个整数转换为八进制字符串。
x = 8 print(oct(x))
|
46.open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None): 打开一个文件,并返回文件对象。
1 2 3
| file = open("example.txt", "w") file.write("Hello, World!") file.close()
|
47.ord(c): 返回一个字符的Unicode编码。
48.pow(x, y[, z]): 返回x的y次幂,如果提供z,则返回x的y次幂对z取模的结果。
1 2 3 4 5
| x = 2 y = 3 z = 5 result = pow(x, y, z) print(result)
|
49.print(objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False): 打印输出到控制台。
1 2 3 4
| name = "Alice" age = 25 print("My name is", name, "and I am", age, "years old.")
|
50.property(fget=None, fset=None, fdel=None, doc=None): 创建一个属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class MyClass: def __init__(self): self._attr = 0 @property def attr(self): return self._attr @attr.setter def attr(self, value): self._attr = value
obj = MyClass() obj.attr = 10 print(obj.attr)
|
51.range(stop): 返回一个包含从0到stop-1的整数序列的可迭代对象。
1 2 3 4 5 6 7 8
| for i in range(5): print(i)
|
52.repr(object): 返回一个对象的字符串表示形式。
1 2
| s = "Hello" print(repr(s))
|
53.reversed(seq): **返回一个反向的迭代器对象。
1 2 3
| seq = [1, 2, 3] rev_seq = reversed(seq) print(list(rev_seq))
|
54.round(number[, ndigits]): 对一个数进行四舍五入。
1 2
| x = 3.14159 print(round(x, 2))
|
55.set([iterable]): 创建一个集合。
1 2 3
| iterable = [1, 2, 3] s = set(iterable) print(s)
|
56.setattr(object, name, value): 设置对象的属性值。
1 2 3 4 5 6
| class MyClass: attr = 10
obj = MyClass() setattr(obj, 'attr', 20) print(obj.attr)
|
57.slice(stop): 创建一个切片对象,用于切片操作。
1 2 3
| numbers = [1, 2, 3, 4, 5] s = slice(2) print(numbers[s])
|
58.sorted(iterable[, key][, reverse]): 返回一个排序后的列表。
1 2 3
| numbers = [3, 1, 4, 2, 5] sorted_numbers = sorted(numbers) print(sorted_numbers)
|
59.staticmethod(function): 将一个函数转换为静态方法。
1 2 3 4 5 6
| class MyClass: @staticmethod def my_method(): print("This is a static method.")
MyClass.my_method()
|
60.str(object=’’): 将一个对象转换为字符串。
61.sum(iterable[, start]): 返回可迭代对象中所有元素的总和。
1 2
| numbers = [1, 2, 3, 4, 5] print(sum(numbers))
|
62.super([type[, object-or-type]]): 返回父类的对象。
1 2 3 4 5 6 7 8 9 10
| class Parent: def __init__(self): self.attr = 10
class Child(Parent): def __init__(self): super().__init__()
child = Child() print(child.attr)
|
63.tuple([iterable]): 创建一个元组。
1 2 3
| iterable = [1, 2, 3] t = tuple(iterable) print(t)
|
64.type(object): 返回一个对象的类型。
65.vars([object]): 返回对象的属性和属性值的字典。
1 2 3 4 5
| class MyClass: attr = 10
obj = MyClass() print(vars(obj))
|
66.zip(iterables): 将多个可迭代对象中对应位置的元素打包成元组,并返回一个由这些元组组成的可迭代对象。
1 2 3 4
| numbers = [1, 2, 3] letters = ['a', 'b', 'c'] zipped = zip(numbers, letters) print(list(zipped))
|