2022年 11月 6日

python怎么获取数据类型?

python怎么获取数据类型?

  • 1. type()
  • 2. isinstance()

1. type()

type() : 获取变量类型

a = 1 # 
print(type(a))

b = 3.14
print(type(b))

c = True 
print(type(c))

d = 3+3j
print(type(d))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

2. isinstance()

isinstance(a,b)函数有两个参数
a: 要进行判断的数据
b: 自己指定的数据类型
结果会返回一个bool类型,true:a和b这两个参数类型一致,false:a和b这两个参数类型不一致。

a = 1
print(isinstance(a,int))

b = 'abc'
print(isinstance(b,int))
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述