Python内置函数(22)——float
英文文档:
class float([x])
Return a floating point number constructed from a number or string x.
If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be '+' or '-'; a '+' sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or a positive or negative infinity. More precisely, the input must conform to the following grammar after leading and trailing whitespace characters are removed:
sign ::= "+" | "-"
infinity ::= "Infinity" | "inf"
nan ::= "nan"
numeric_value ::=floatnumber|infinity|nan
numeric_string ::= [sign]numeric_value
Here floatnumber is the form of a Python floating-point literal, described in Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY” and “iNfINity” are all acceptable spellings for positive infinity.
Otherwise, if the argument is an integer or a floating point number, a floating point number with the same value (within Python’s floating point precision) is returned. If the argument is outside the range of a Python float, an OverflowError will be raised.
For a general Python object x, float(x) delegates to x.__float__().
If no argument is given, 0.0 is returned.
说明:
1. 函数功能将一个数值或者字符转换成浮点型数值。
>>> float(3)
3.0
>>> float('')
3.0
2. 不提供参数的时候,返回0.0。
>>> float()
0.0
3. 字符串必须能正确转换成浮点型数值的,否则报错。
>>> float('3.14.15926')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
float('3.14.15926')
ValueError: could not convert string to float: '3.14.15926'
4. 字符串中允许出现“+”、“-”两个符号,两个符号和数字之间不能出现空格,但是符号前面和数字后面允许出现空格。
>>> float('+3.14') #带正号
3.14
>>> float('-3.14') #带负号
-3.14
>>> float(' -3.14 ') #正负号前、数字后可以有空格
-3.14
>>> float('- 3.14') #正负号与数字间不可以有空格
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
float('- 3.14')
ValueError: could not convert string to float: '- 3.14'
5. 有几个特殊的字符串能正确转换,"Infinity"或者“inf”(不区分大小写),能正确转换,表示无穷大,可以和“+”、“-”一起使用;“nan”也能正确转换,表示没有值。
>>> float('Infinity')
inf
>>> float('inf')
inf
>>> float('inFinIty') #不区分大小写
inf
>>> float('+inFinIty') #正无穷
inf
>>> float('-inFinIty') #负无穷
-inf
>>> float('nan') #没有值
nan
6. 定义的对象如果要被float函数正确转换成浮点数,需要定义__float__函数。
>>> class X:
def __init__(self,score):
self.score = score >>> x = X(9.7)
>>> float(x) #不能转换
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
float(x)
TypeError: float() argument must be a string or a number, not 'X' >>> class X: #重新定义类,加入__float__方法
def __init__(self,score):
self.score = score
def __float__(self):
return self.score >>> x = X(9.7)
>>> float(x) #可以转换
9.7
Python内置函数(22)——float的更多相关文章
- Python内置函数(10)——float
英文文档: class float([x]) Return a floating point number constructed from a number or string x. If the ...
- Python内置函数(22)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- python 内置函数input/eval(22)
python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...
- 学习过程中遇到的python内置函数,后续遇到会继续补充进去
1.python内置函数isinstance(数字,数字类型),判断一个数字的数字类型(int,float,comple).是,返回True,否,返回False2.python内置函数id()可以查看 ...
- Python入门之 Python内置函数
Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
随机推荐
- redis对list进行的相关操作
redis对list类型进行的相关操作以及如何在python使用 redis对list类型操作的命令: 命令 语法 概述 返回值 Redis Blpop 命令 BLPOP key1 [key2 ] t ...
- 20165220《网络对抗技术》week1 Exp0 Kali安装
下载地址: 地址:https://www.kali.org/downloads/ 安装: 登录 配置网络: 共享文件夹设置: 安装软件: 输入apt-get install ibus ibus-pin ...
- 手机QQ公众号亿级消息实时群发架构
编者按:高可用架构分享及传播在架构领域具有典型意义的文章,本文由孙子荀分享.转载请注明来自高可用架构公众号 ArchNotes. 孙子荀,2009 年在华为从事内核和分布式系统的开发工作:2011 ...
- 【C语言编程练习】5.10寻找水仙数
1. 题目要求 如果一个3位数等于各位数字的立方和,则称这个数为水仙数,例如407=4^3+0^3+7^3.编写一个程序,找出全部的水仙数 2. 题目分析 感觉又和之前的题目大同小异了,先找出解空间, ...
- easyUI 创建有复选框的table.datagrid
table : function(data){ pt.v.table.datagrid({ // singleSelect:true, height:295, columns:[[ {field:'x ...
- CefSharp Cookie独立 GetGlobalCookieManager
可以实现: 登陆多个京东站点,而京东各个账号互不影响. 可以完全实现Cookie独立,Cache独立. Demo用VS2017开发,C# ,NET4.5 没错,稍加改造就可以用来刷单.有Demo,有 ...
- vue简单指令笔记
v-once 执行一次性插值,数据改变插值处内容不会更新 <span v-once>这个将不会改变: {{ msg }}</span> v-text 插入文本 <!--两 ...
- [LeetCode] Fibonacci Number 斐波那契数字
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- R语言读入数据库的中英名词互译测试并计分脚本(考试用)
1. 分子生物学中英文.csv,输入文件,两列,以tab键分隔的txt文本,没有列名 2. 错误的名解.csv, 如果在测试中拼写错误,会写出到这个文件,可用这个容易犯错的名词进行新的测试 3. 注意 ...
- Widows 和Linux 查看和操作端口方法
Windows 打开cmd1.netstat -n查看本机的使用的所有端口①.proto表示协议 有tcp和udp两种②.Local Address 表示本机的IP,后面跟的是我们使用的端口号③.Fo ...