python基础学习(二):数据类型
来源:互联网 发布:python 内容管理系统 编辑:程序博客网 时间:2024/06/07 04:52
1.python常用的数据类型有整形(int),浮点型(float),布尔型(bool),字符串(str)
他们之间可以转化:
>>> a="520"
>>> b=int(a)>>> b
520
>>> b=float(a)
>>> b
520.0
>>> c=str(a)
>>> c
'520'
>>> a=5e19
>>> b=str(a)
>>> b
'5e+19'
转化时注意问题:
(1)像这种根本无法转换的类型进行转换时会发生错误:
>>> a='lucy'
>>> b=int(a);
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
b=int(a);
ValueError: invalid literal for int() with base 10: 'lucy'
(2)不可跳跃转化
a='5.12'
>>> b=int(a)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
b=int(a)
ValueError: invalid literal for int() with base 10: '5.12'
字符串‘5.12’只能先转换成float型,再转化为int,如下
>>> a='5.12'
>>> b=float(a)
>>> b
5.12
>>> c=int(b)
>>> c
5
另外注意科学计数法如 4e13也是属于float
2.获得数据类型type()
>>> a="asdfg"
>>> type(a)
<class 'str'>
>>> type(12.3)
<class 'float'>
3.验证某变量是不是某类型isinstance(variable,datatype)
>>> a='asdfg'
>>> isinstance(a,str)
True
>>> isinstance(12.3,float)
True
>>> isinstance(12.33.int)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
isinstance(12.33.int)
AttributeError: 'float' object has no attribute 'int'
- python基础学习(二):数据类型
- python基础学习二——数据类型
- python基础数据类型二
- python基础数据类型(二):列表
- Python学习二:数据类型
- python基础学习-数据类型
- Java基础学习笔记:(二)数据类型
- 0基础lua学习(二) 数据类型
- Python学习(五)------- 数据类型(二)
- Python学习之数据类型&(二)
- Python学习之基本数据类型(二)
- python学习(1)基础及数据类型
- python基础学习(4)—数据类型
- Python基础(二)之Python变量和数据类型
- Python基础学习(二)
- Python基础学习(二)
- python学习笔记二数据类型
- python学习之基础数据类型
- 【Unified Auditing】统一审计的存储(12.2)
- 欢迎使用CSDN-markdown编辑器
- LeetCode 98. Validate Binary Search Tree Add to List
- Android使用Retrofit请求WebService
- 二叉树遍历:已知前序中序输出后序/已知后序中序输出前序
- python基础学习(二):数据类型
- 哈希hash
- POJ 2485(建高速公路__最小生成树)
- domain、entity和model的package的不同用法
- 12个有趣的C语言面试题及答案
- wampsever无法进入phpadmin等
- AAPTerr: No Delegate set : lost message:\\?\C:\...\build-cache\...\*.png ERROR: Unable to open PNG f
- 算法设计与应用基础-第十一周
- MyBatis之SQL语句映射文件