코딩, 개발에 대한 기록 저장소

파이썬(Python) 데이터 타입(type) 확인 및 비교

파이썬(Python) 데이터 타입(type) 확인 및 비교

데이터 타입 확인

a = ['a', 'b']
print(type(a))

b = 123
print(type(b))

c = 3.14
print(type(c))

d = 'love'
print(type(d))

결과:

<class 'list'>
<class 'int'>
<class 'float'>
<class 'str'>

type()으로 비교

a = ['a', 'b']
b = 123
c = 3.14
d = 'love'

if type(a) is list:
    print("a is list")

if type(b) is int:
    print("b is int")

if type(c) is float:
    print("c is float")

if type(d) is str:
    print("d is str")

결과:

a is list
b is int
c is float
d is str

isinstance()으로 비교

a = ['a', 'b']
b = 123
c = 3.14
d = 'love'

if isinstance(a, list):
    print("a is list")

if isinstance(b, int):
    print("b is int")

if isinstance(c, float):
    print("c is float")

if isinstance(d, str):
    print("d is str")

결과:

a is list
b is int
c is float
d is str