【Python操作基础】——数据类型

简介: 【Python操作基础】——数据类型

【Python操作基础】系列——数据类型,建议收藏!



该篇文章帮助认识Python的各种数据类型,包括数据类型查看、数据类型判断、数据类型转换、一些特殊数据类型、以及序列数据类型。

1 查看数据类型

  运行程序:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"       ##执行多输出
type(1)                                               ##整型
type(1.2)                                             ##浮点型
type(True)                                           ##布尔型
type("djissj")                                       ##字符串
type([1,2,3,4])                                      ##列表
type((1,2,3,4,5))                                    ##元组
type({1,2,3})                                        ##集合
type({"a":0,"b":1,"c":2})                            #字典

  运行结果:

int
float
bool
str
list
tuple
set
dict

2 判断数据类型

  运行程序:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"       ##执行多输出
x=10
isinstance(x,int)
isinstance(True,int)            #python中,布尔型为int类型子类

  运行结果:

True
True

3 转换数据类型

  运行程序:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"       ##执行多输出
int(1.6)   #将浮点型转化为整型
float(1)   #将浮点型转化为整型
bool(0)    #将整型转化为布尔型
tuple([1,2,-3])#将列表转化为元组
list((1,2,3)) #将元组转化为列表

  运行结果:

1
1.0
False
(1, 2, -3)
[1, 2, 3]

4 特殊数据类型

  运行程序:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"       ##执行多输出
x=None
print(x)  ##python中None必须用print才能输出结果
Ellipsis  #省略号
x=2+3j
print('x=',x)#支持复数类型
y=complex(3,4) #复数
print('y=',y)
print("x+y=",x+y)#支持复数运算
##进制换算
int('100',base=2)   #二进制转化为10进制
int('100',base=10)  #十进制
9.8e2   #科学计数法import csv  
  
# 打开CSV文件  
with open('your_file.csv', 'r') as csvfile:  
    # 创建CSV读取器对象  
    reader = csv.reader(csvfile)  
      
    # 读取所有行  
    rows = list(reader)  
  
# 打印所有行  
for row in rows:  
    print(row)

  运行结果:

None
Ellipsis
x= (2+3j)
y= (3+4j)
x+y= (5+7j)
4
100
980.0

5 序列类型

  运行程序:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"       ##执行多输出
mySeq1="Data Sciendce"
mySeq2=[1,2,3,4,5]
mySeq3=(1,2,3,4,5)
mySeq1[1:3],mySeq2[1:3],mySeq3[1:3]      #提取每组第2个到第3个
mySeq1*3      #重复三次序列

  运行结果:

('at', [2, 3], (2, 3))
'Data SciendceData SciendceData Sciendce'


相关文章
|
3天前
|
数据挖掘 Python
Python文件读取操作详解
Python文件读取操作详解
8 1
|
3天前
|
存储 Python
Python字典的定义与操作详解
Python字典的定义与操作详解
4 1
|
3天前
|
索引 Python
Python集合的定义与操作详解
Python集合的定义与操作详解
4 1
|
3天前
|
存储 索引 Python
Python数据容器的切片操作详解
Python数据容器的切片操作详解
6 1
|
3天前
|
索引 Python
Python字符串的定义与操作详解
Python字符串的定义与操作详解
7 1
|
3天前
|
存储 索引 Python
Python元组的定义与操作详解
Python元组的定义与操作详解
4 1
|
5天前
|
SQL 关系型数据库 MySQL
Python 操作 MySQL 数据库
Python 操作 MySQL 数据库
18 0
|
5天前
|
算法 数据处理 开发者
Python Number 数据类型
Python Number 数据类型
14 1
|
5天前
|
Python
Python pass 数据类型
Python pass 数据类型
|
6天前
|
存储 Python
Python组合数据类型的使用与技巧
Python组合数据类型的使用与技巧