print(f ’ ')与 print(f " ")一样,表示格式化字符,是对于f后 ’ ’ 或 " " 中 { } 内的变量和表达式输出为值。
如下样例:
from matplotlib_inline import backend_inline
from mxnet import np, npx
from d2l import mxnet as d2l
npx.set_np()
def f(x):
return 3 * x ** 2 - 4 * x
def numerical_lim(f, x, h):
return (f(x + h) - f(x)) / h
h = 0.1
for i in range(5):
print(f'h={h:.5f}, numerical limit={numerical_lim(f, 1, h):.5f}')
h *= 0.1
加f的输出结果如图所示:
不加f时的输出结果如图所示:
注: 其中 { : } :及:后的内容代表结果保留位数可省略,:前的内容为变量或表达式
这篇博客探讨了Python中利用函数`numerical_lim`进行数值极限计算的方法,通过示例展示了如何逐步减小步长`h`来逼近函数的导数。文章强调了`f'()`和`f()在格式化字符串中的作用,以及它们如何影响输出结果的精度。
1万+

被折叠的 条评论
为什么被折叠?



