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

本文详细介绍了Python中的数据类型,包括查看、判断、转换,以及特殊数据类型如None和复数,以及序列类型如列表、元组和字符串的使用。适合初学者学习Python数据类型的基础知识。
摘要由CSDN通过智能技术生成

🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972

   个人介绍: 研一|统计学|干货分享
         擅长Python、Matlab、R等主流编程软件
         累计十余项国家级比赛奖项,参与研究经费10w、40w级横向

【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'
  • 19
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小墨&晓末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值