Python
[Python] Type Cast (타입 변환)
newly0513
2021. 6. 6. 16:26
728x90
반응형
- int
- float
- str
- bool
type()
해당 데이터타입을 모르거나 확인이 필요한 경우 사용
# data type을 확인
type(123)
type(12.3)
type('123')
type([])
type({})
type(())
# type 확인 결과
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
int()
해당 데이터를 int type으로 변환. 문자형의 경우 변환할 수 없음.
int(1.23)
int('123')
int('a')
# 실행결과
1
123
value error
float()
해당 데이터를 float type으로 변환. 문자형의 경우 변환할 수 없음.
float(12)
float('12')
float('-123')
float('1e-003')
float('a')
# 실행결과
12.0
12.0
-123.0
0.001
error
str()
해당 데이터를 str type으로 변환.
str(1)
str(1.2)
str("a")
# 실행결과
'1'
'1.2'
'a'
bool()
해당 데이터를 boolean type으로 변환. 숫자형의 경우 0이면 False, 아니면 True. 문자형의 경우 빈 값이면 False, 아니면 True
bool(0)
bool(1)
bool(1.2)
bool('')
bool('a')
# 실행결과
False
True
True
False
True
728x90
반응형