# 基本用法int('123') # 123int(3.14) # 3# 指定进制转换int('1010', 2) # 10 (二进制转十进制)int('FF', 16) # 255 (十六进制转十进制)# 临界值处理int('') # ValueError: invalid literal for int() with base 10: ''int(None) # TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'int('3.14') # ValueError: invalid literal for int() with base 10: '3.14'int(float('inf')) # OverflowError: cannot convert float infinity to integer
# 基本用法len('abc') # 3len([1, 2, 3]) # 3len({'a': 1}) # 1# 临界值处理len('') # 0len(None) # TypeError: object of type 'NoneType' has no len()len(123) # TypeError: object of type 'int' has no len()
# 基本用法
lst = [1, 2, 3, 2]
lst.remove(2) # lst 变为 [1, 3, 2]# 临界值处理
lst.remove(4) # ValueError: list.remove(x): x not in list
lst.remove(None) # 如果列表中有 None 可以移除
5.32 list.pop()
移除并返回指定位置的元素。
# 基本用法
lst = [1, 2, 3]
lst.pop() # 返回 3, lst 变为 [1, 2]
lst.pop(0) # 返回 1, lst 变为 [2]# 临界值处理
lst.pop(100) # IndexError: pop index out of range
[].pop() # IndexError: pop from empty list
5.33 list.index()
返回指定元素的索引。
# 基本用法
lst = [1, 2, 3, 2]
lst.index(2) # 1# 指定搜索范围
lst.index(2, 2) # 3# 临界值处理
lst.index(4) # ValueError: 4 is not in list
[].index(1) # ValueError: 1 is not in list
from functools import reduce
# 基本用法
reduce(lambda x, y: x + y, [1, 2, 3, 4]) # 10 (((1+2)+3)+4)# 指定初始值
reduce(lambda x, y: x + y, [1, 2, 3], 10) # 16# 临界值处理
reduce(lambda x, y: x + y, []) # TypeError: reduce() of empty sequence with no initial value
reduce(lambda x, y: x + y, [1]) # 1 (单元素直接返回)
10. 模块与包函数
10.56 import
导入模块。
# 基本用法# import math # math.sqrt(4)# 别名# import numpy as np# 临界值处理# import nonexistent_module # ModuleNotFoundError
10.57 from…import
从模块导入特定对象。
# 基本用法# from math import sqrt# sqrt(4)# 导入多个# from math import sqrt, pi# 临界值处理# from math import nonexistent # ImportError: cannot import name 'nonexistent'
11. 日期时间函数
11.58 datetime.date.today()
获取当前日期。
from datetime import date
# 基本用法# today = date.today() # 返回 date 对象# 临界值处理# 无参数,总是返回当前日期
11.59 datetime.datetime.now()
获取当前日期和时间。
from datetime import datetime
# 基本用法# now = datetime.now() # 返回 datetime 对象# 指定时区# from datetime import timezone# datetime.now(timezone.utc)# 临界值处理# 无参数,总是返回当前时间
11.60 datetime.datetime.strptime()
将字符串解析为日期时间对象。
from datetime import datetime
# 基本用法# dt = datetime.strptime('2023-01-01', '%Y-%m-%d')# 临界值处理# datetime.strptime('invalid', '%Y') # ValueError: time data 'invalid' does not match format '%Y'# datetime.strptime(None, '%Y') # TypeError: strptime() argument 1 must be str, not None
11.61 datetime.datetime.strftime()
将日期时间对象格式化为字符串。
from datetime import datetime
# 基本用法# now = datetime.now()# now.strftime('%Y-%m-%d %H:%M:%S')# 临界值处理# datetime.strftime(None, '%Y') # AttributeError: 'NoneType' object has no attribute 'strftime'
# 基本用法
it = iter([1, 2, 3])
next(it) # 1# 临界值处理iter(1) # TypeError: 'int' object is not iterableiter(None) # TypeError: 'NoneType' object is not iterable
# 基本用法
mv = memoryview(b'abc')
mv[0] # 97# 临界值处理memoryview('abc') # TypeError: memoryview: a bytes-like object is required
13.86 ord()
返回字符的 Unicode 码点。
# 基本用法ord('a') # 97# 临界值处理ord('') # TypeError: ord() expected a character, but string of length 0 foundord('ab') # TypeError: ord() expected a character, but string of length 2 found
13.87 chr()
根据 Unicode 码点返回字符。
# 基本用法chr(97) # 'a'# 临界值处理chr(-1) # ValueError: chr() arg not in range(0x110000)chr(0x110000) # ValueError
13.88 bin()
将整数转换为二进制字符串。
# 基本用法bin(10) # '0b1010'# 临界值处理bin(3.14) # TypeError: 'float' object cannot be interpreted as an integerbin(-10) # '-0b1010'