Python函数:f是啥意思?

Posted

技术标签:

【中文标题】Python函数:f是啥意思?【英文标题】:Python function what does :f mean?Python函数:f是什么意思? 【发布时间】:2018-10-10 23:43:25 【问题描述】:
def fahrenheit_to_centigrade(deg_fahrenheit):
deg_c = (deg_fahrenheit - 32.)*5./9.
return deg_c

for deg_F in [-40, -30, -20, -10, 0, 10, 50, 100]:
    conv_temp = fahrenheit_to_centigrade(deg_F)
    print(f"deg_F:f degrees F is conv_temp:f degrees C")

上面是我得到的一个函数(python 的介绍,所以如果这是一个非常愚蠢的问题,我提前道歉)。没看懂

:f

最终打印语句中的位(在 deg_F 和 conv_temp 之后)?这是什么意思?它是否会调节结果以使值具有相同的 sig.f / 类?这个有名字让我自己查吗?

【问题讨论】:

"格式为 float" 一个similar question/answer. “这个有名字让我自己查吗?”见6.1.3.1. Format Specification Mini-Language 非常感谢! 【参考方案1】:

这是浮点和十进制值的表示类型说明符。 'f' 表示固定精度,而不是例如科学指数记数法:

Type Meaning
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.
'E' Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.
'f' Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6.
'F' Fixed-point notation. Same as 'f', but converts nan to NAN and inf to INF.
'g' General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude. The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it. Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision. A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.
'G' General format. Same as 'g' except switches to 'E' if the number gets too large. The representations of infinity and NaN are uppercased, too.
'n' Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.
'%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.
None Similar to 'g', except that fixed-point notation, when used, has at least one digit past the decimal point. The default precision is as high as needed to represent the particular value. The overall effect is to match the output of str() as altered by the other format modifiers.
https://docs.python.org/3.6/library/string.html#formatspec

【讨论】:

以上是关于Python函数:f是啥意思?的主要内容,如果未能解决你的问题,请参考以下文章

python的format函数,'0:4.2f'.format(3.14159)表达式大括号里边的0具体是啥意思,由啥作用?

python中def是啥意思?

Python3 函数中没有标识符的单个 * 是啥意思? [复制]

(Python)这个函数中的语法“slider.var”是啥意思

python里的end是啥意思

python中yield是啥意思