当前位置:首页>python>62个Python常用函数,从基础到进阶(二)

62个Python常用函数,从基础到进阶(二)

  • 2026-08-27 08:10:06
62个Python常用函数,从基础到进阶(二)
六、对象与属性
32.isinstance():检查对象是否为指定类型的实例
number = 42 print(isinstance(number, int))  # 输出: True
33.callable():检查对象是否可调用(函数、方法等)
def say_hello():     print("Hello!") print(callable(say_hello))  # 输出: True
34.getattr():返回对象的属性值
class Person:     name = "Alice" person = Person() name = getattr(person, "name") print(name)  # 输出: Alice
35.setattr():设置对象的属性值
class Person:     name = "" person = Person() setattr(person, "name", "Alice") print(person.name)  # 输出: Alice
36.delattr():删除对象的属性
class Person:     name = "Alice" person = Person() delattr(person, "name") print(hasattr(person, "name"))  # 输出: False
七、文件操作
37.open():打开文件并返回文件对象
file = open("example.txt", "r") content = file.read() print(content) file.close()
38.close():关闭文件
file = open("example.txt", "r") content = file.read() file.close()
39.read():读取文件内容
file = open("example.txt", "r") content = file.read() print(content) file.close()
40.write():将内容写入文件
file = open("example.txt", "w") file.write("Hello, World!") file.close()
八、列表操作
41.append():在列表末尾添加元素
numbers = [1, 2, 3] numbers.append(4) print(numbers)  # 输出: [1, 2, 3, 4]
42.extend():将可迭代对象中的元素添加到列表末尾
numbers = [1, 2, 3] more_numbers = [4, 5, 6] numbers.extend(more_numbers) print(numbers)  # 输出: [1, 2, 3, 4, 5, 6]
43.insert():在指定索引处插入元素
numbers = [1, 2, 3] numbers.insert(1, 4) print(numbers)  # 输出: [1, 4, 2, 3]
44.remove():移除列表中第一个匹配的元素
numbers = [1, 2, 3, 2, 4] numbers.remove(2) print(numbers)  # 输出: [1, 3, 2, 4]
45.pop():移除并返回指定索引处的元素
numbers = [1, 2, 3] popped = numbers.pop(1) print(popped)  # 输出: 2 print(numbers)  # 输出: [1, 3]
46.index():返回第一个匹配元素的索引
numbers = [1, 2, 3, 2, 4] index = numbers.index(2) print(index)  # 输出: 1
47.count():返回元素在列表中的出现次数
numbers = [1, 2, 3, 2, 4] count = numbers.count(2) print(count)  # 输出: 2
48.sort():对列表进行排序
numbers = [5, 2, 4, 1, 3] numbers.sort() print(numbers)  # 输出: [1, 2, 3, 4, 5]
九、列表进阶操作
49.reverse():反转列表中的元素顺序
numbers = [1, 2, 3, 4, 5] numbers.reverse() print(numbers)  # 输出: [5, 4, 3, 2, 1]
十、工具类函数
50.random.random():生成 0 到 1 之间的随机浮点数
import random print(random.random())  # 输出: 0.2203627(示例值)
51.time.sleep():让程序暂停指定时长(单位:秒)
import time time.sleep(5) print('hello')  # 延迟5秒后输出
52.os.listdir():显示指定目录下的文件和子目录列表
import os path = r'D:/images' dirs = os.listdir(path) for file in dirs:     print(file)  # 输出目录下所有文件
十一、字符与编码
53.chr():返回指定 Unicode 代码对应的字符
char = chr(65) print(char)  # 输出: 'A'
54.ord():返回字符对应的 Unicode 代码
code = ord('A') print(code)  # 输出: 65
十二、进制转换
55.bin():将整数转换为二进制字符串
binary = bin(10) print(binary)  # 输出: '0b1010'
56.hex():将整数转换为十六进制字符串
hexadecimal = hex(16) print(hexadecimal)  # 输出: '0x10'
57.oct():将整数转换为八进制字符串
octal = oct(8) print(octal)  # 输出: '0o10'
十三、字节与集合
58.bytearray():创建一个可变的字节数组对象
my_array = bytearray([0, 1, 2, 3]) print(my_array)  # 输出: bytearray(b'\x00\x01\x02\x03')
59.bytes():创建一个不可变的字节数组对象
my_bytes = bytes([0, 1, 2, 3]) print(my_bytes)  # 输出: b'\x00\x01\x02\x03'
60.frozenset():创建一个不可变的集合对象
my_set = frozenset([1, 2, 3, 2]) print(my_set)  # 输出: frozenset({1, 2, 3})
十四、字符串与代码执行
61.ascii():返回对象的可打印字符串表示,非 ASCII 字符会转义
text = "Hello, 你好" ascii_text = ascii(text) print(ascii_text)  # 输出: 'Hello, \u4f60\u597d'
62.exec():执行动态生成的 Python 代码
code = ''' for i in range(5):     print(i) ''' exec(code)  # 输出: 0 1 2 3 4
63.format():根据指定格式进行字符串格式化
name = "Alice" age = 25 formatted = "Name: {}, Age: {}".format(name, age) print(formatted)  # 输出: "Name: Alice, Age: 25"

最新文章

随机文章