python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set)

一般通过以下方法进行判断:

1、isinstance(参数1,参数2)

描述:该函数用来判断一个变量(参数1)是否是已知的变量类型(参数2) 类似于type()

参数1:变量

参数2:可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值: 如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False

例子:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#判断变量类型的函数
def typeof(variate):type=Noneif isinstance(variate,int):type = "int"elif isinstance(variate,str):type = "str"elif isinstance(variate,float):type = "float"elif isinstance(variate,list):type = "list"elif isinstance(variate,tuple):type = "tuple"elif isinstance(variate,dict):type = "dict"elif isinstance(variate,set):type = "set"return type
# 返回变量类型
def getType(variate):arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}vartype = typeof(variate)if not (vartype in arr):return "未知类型"return arr[vartype]#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

2、通过与已知类型的常量进行比较

例子:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#判断变量类型的函数
def typeof(variate):type1 = ""if type(variate) == type(1):type1 = "int"elif type(variate) == type("str"):type1 = "str"elif type(variate) == type(12.3):type1 = "float"elif type(variate) == type([1]):type1 = "list"elif type(variate) == type(()):type1 = "tuple"elif type(variate) == type({"key1":"123"}):type1 = "dict"elif type(variate) == type({"key1"}):type1 = "set"return type1
# 返回变量类型
def getType(variate):arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}vartype = typeof(variate)if not (vartype in arr):return "未知类型"return arr[vartype]#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

补充:

isinstance() 与 type() 区别:

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

  • isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

python中判断变量的类型相关推荐

  1. Python中判断输入数据的类型

    初学者一枚记录一下 一般在实际操作的过程中可能限定输入数据的类型 根据函数来判断的 在接收input()输入的数据后,判断接收到的字符串是否为数字 例如: str = input("plea ...

  2. python中的变量与对象

    一. 什么是变量 变量就是以前学习的数学中常见的等式x = 3(x是变量,3是变量值),在编程中,变量不仅可以是数学,还可以是任意数据类型 二. 变量的命名规则 变量名必须是英文大小写.数字和_的组合 ...

  3. python中 是什么类型_浅谈python中的变量默认是什么类型

    浅谈python中的变量默认是什么类型 1.type(变量名),输出的结果就是变量的类型: 例如 >>> type(6) 2.在Python里面变量在声明时,不需要指定变量的类型,变 ...

  4. python中none算变量吗_在python中对变量判断是否为None的三种方法总结

    三种主要的写法有: 第一种:if X is None; 第二种:if not X: 当X为None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()这 ...

  5. python字符串类型判断(python中datatype()测试字符串类型)

    python如何判断变量是否是字符串 Python中的数据类型有数字.字符串,列表.元组.字典.集合等. 相关推荐:<Python教程> python中,判断某变量的数据类型是否为字符串, ...

  6. python中给变量赋值时、既确定了变量的值_Python 中的变量赋值不需要类型声明,可以直接赋值,通过值来决定变量的类型。_学小易找答案...

    [单选题]"阴阳离决.精气乃绝"所反映的阴阳关系是 [单选题]在Photoshop通道种类中不包括( ). [单选题]国家垄断资本主义的主要形式是(). [判断题]Python 中 ...

  7. python中的变量是动态类型的什么意思_Python零基础入门(一):对Python的简单认识...

    点击蓝字 关注浅韵 一起划水 写在前面 期末考试告一段落之后,这个公众号又活了. 漫长的假期,想玩也是肯定的,但是发现自己想学的东西也很多,想写的东西也很多,所以给自己来了一个假期整活计划. 在这个假 ...

  8. Python判断变量的类型

    Python判断变量的类型有两种方法:type() 和 isinstance() type() a = ['xxx', 123] if type(a) is list:print('list数组') ...

  9. python空类型-在Python中__________表示空类型。

    [填空题]任意长度的 Python 列表.元组和字符串中最后一个元素的下标为 ________ . [单选题]下面代码的输出结果是 ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬ ...

最新文章

  1. python在中国的发展-Python未来的发展前景到底如何?
  2. android commit apply,关于SharedPreference的commit和apply和ANR
  3. 前端学习(2618):vue插槽--默认插槽
  4. Oracle view 小结片段
  5. mysql 主主忽略错误_MySQL 主主报错: Fatal error: The slave I/O thread stops because master and slave have...
  6. 深度学习(七十)darknet 实现编写mobilenet源码
  7. vnx 服务器映射,EMC VNX5200/5400存储 新增LUN与Hosts映射操作(示例代码)
  8. 获取aplicationContext对象,从而获取任何注入的对象
  9. 【报告分享】迈向更好的教育:未来教育的技术空间研究报告.pdf(附下载链接)
  10. msql查询姓名不带r的员工姓名_java基础之MySQL多表查询2
  11. C++ 0x/11学习笔记
  12. SQL Server无法安装问题
  13. GitHub十大热门Python项目,过程很有趣
  14. 使用Echarts在前端页面绘制地图
  15. css单线边框_css中的border-collapse属性如何设置表格边框线?(代码示例)
  16. 怎么测试网站访问速度?
  17. vue+element实现一个excel表格下载的功能
  18. 听歌识曲C++程序说明
  19. 设f(x)=∑x^n/n^2,证明f(x)+f(1-x)+lnxln(1-x)=∑1/n^2
  20. 3ds_max入门教程

热门文章

  1. MinGW 与MSVC的区别
  2. Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置
  3. python语法笔记(六)
  4. Sqlite使用技巧集锦
  5. 浏览器窗口的高度和宽度
  6. Openvswitch手册(5): VLAN and Bonding
  7. 要速度更要方便!20款实用Chrome插件推荐
  8. php获取显示图书数据,php基于dom实现读取图书xml格式数据的方法
  9. 【学习笔记】7、标准数据类型—字符串
  10. 【MM模块】 Cash Discounts 现金折扣