클라우드 낚시꾼

파이썬 타입 판별 방법 (정수 판별, 실수 판별 등등) 본문

Programming Language/Python 활용

파이썬 타입 판별 방법 (정수 판별, 실수 판별 등등)

KanuBang 2024. 1. 24. 17:29
728x90

파이썬 주어진 데이터의 타입을 판별하는데는 다양한 방법이 있다. 가장 일반적인 방법 3가지를 알아보자.

type() 함수를 이용해 타입 확인하기

num = 42
print(type(num))  # <class 'int'>

num_float = 3.14
print(type(num_float))  # <class 'float'>

isinstance(확인하려는 데이터,  타입 or 타입 튜플)를 활용

y = 3
if isinstance(y, int):
    print("integer")
else:
    print("float") # here

string = "hello!"
if isinstance(string, (int,str,list)):
    print("match") # here
else:
    print("no-match")

int()를 이용해 정수 판별하기

x = 3
if x == int(x):
    print("integer") #here
else:
    print("float")
728x90