python判断变量是否为int、字符串、列表、元组、字典的方法详解

作者:猪笨是念来过倒 时间:2022-09-28 05:11:57 

在实际写程序中,经常要对变量类型进行判断,除了用type(变量)这种方法外,还可以用isinstance方法判断:


a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {'a':1, 'b':2, 'c':3}
e = "abc"
if isinstance(a,int):
 print ("a is int")
else:
 print ("a is not int")
if isinstance(b,list):
 print ("b is list")
else:
 print ("b is not list")
if isinstance(c,tuple):
 print ("c is tuple")
else:
 print ("c is not tuple")
if isinstance(d,dict):
 print ("d is dict")
else:
 print ("d is not dict")
if isinstance(e,str):
 print ("d is str")
else:
 print ("d is not str")

来源:https://blog.csdn.net/liao392781/article/details/90296229

标签:python,判断变量,isinstance
0
投稿

猜你喜欢

  • nodejs对项目下所有空文件夹创建gitkeep的方法

    2024-05-08 09:37:40
  • python绘图之坐标轴的超详细讲解

    2021-12-13 06:57:17
  • sql server实现在多个数据库间快速查询某个表信息的方法

    2024-01-25 05:23:10
  • Python+OpenCV实现信用卡数字识别的方法详解

    2022-01-15 14:25:41
  • vue中自定义指令(directive)的基本使用方法

    2024-05-28 15:46:32
  • python logging模块的使用总结

    2021-11-15 06:46:53
  • 详解Tensorflow数据读取有三种方式(next_batch)

    2023-08-10 07:30:42
  • 梅尔倒谱系数(MFCC)实现

    2022-08-08 18:28:08
  • 原生js+ajax分页组件

    2024-05-21 10:12:06
  • Python字典简介以及用法详解

    2023-12-05 04:23:52
  • MySQL中使用FREDATED引擎实现跨数据库服务器、跨实例访问

    2024-01-25 12:55:52
  • javascript replace()用法详解附实例代码

    2024-04-17 09:42:52
  • vue组件之间进行传值的方法

    2024-04-27 15:48:04
  • Python输出各行命令详解

    2021-12-03 05:29:18
  • Python实现修改Excel文件的元数据

    2023-05-01 01:01:47
  • 浅谈javascript中的作用域

    2024-05-11 09:31:22
  • 利用LyScript实现应用层钩子扫描器

    2023-01-10 16:28:39
  • Go-ethereum 解析ethersjs中产生的签名信息思路详解

    2023-08-05 21:34:49
  • Go实现文件上传和下载

    2023-06-19 07:42:32
  • 详解Python+Matplotlib绘制面积图&热力图

    2021-10-10 10:16:29
  • asp之家 网络编程 m.aspxhome.com