判断变量是否为某种类型
使用isinstance进行判断python基础类型
import numpy as np
a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {'a':1,'b':2,'c':3}
e = "abc"
f = np.array(b)
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")
if isinstance(f,np.ndarray):
print("f is np.ndarray")
else:
print("f is not np.ndarray")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
![]()
使用is进行判断基础类型
import numpy as np
a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {'a':1,'b':2,'c':3}
e = "abc"
f = np.array(b)
if type(a) is int:
print("a is int")
else:
print("a is not int")
if type(b) is list:
print("b is list")
else:
print("b is not list")
if type(c) is tuple:
print("c is tuple")
else:
print("c is not tuple")
if type(d) is dict:
print("d is dict")
else:
print("d is not dict")
if type(e) is str:
print("d is str")
else:
print("d is not str")
if type(f) is np.ndarray:
print("f is np.ndarray")
else:
print("f is not np.ndarray")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
![]()
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树基础语法缩进规则416632 人正在系统学习中
评论记录:
回复评论: