파이썬의 지원 자료형을 알아보겠습니다.

자료형 설명
int 정수
float 실수
complex 복소수
bool 불린
srt 문자열
list 리스트
tuple 튜플
set 집합
dict 사전

 

# 데이터 타입 출력

int_v = 7
float_v = 10.0 
bool = True
str1 = "python"
str2 = "Anaconda"
list = [str1, str2]
tuple = (7, 8, 9)
set = {5, 8, 9}
dict = {
    "name": "Machine Learning",
    "version": 2.0
}

print(type(int_v))
print(type(float_v))
print(type(bool))
print(type(str1))
print(type(str2))
print(type(list))
print(type(tuple))
print(type(set))
print(type(dict))


=== 실행 결과 ===
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>
<class 'str'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>

 

# 형 변환

먼저 변수의 타입을 확인한다.

a = 4.0
b = 6
c = True
d = False
print(type(a), type(b), type(c), type(d))

=== 실행 결과 ===
<class 'float'> <class 'int'> <class 'bool'> <class 'bool'>

위에서 입력한 a, b, c, d의 값을 형 변환 한후 타입을 확인한다.

print(float(a))
print(int(b))
print(int(c))
print(float(d))

=== 실행 결과 ===
4.0
6
1
0.0

'Language > Python' 카테고리의 다른 글

06. python 자료형 - 숫자형  (0) 2019.12.07
05. python - 비교 연산자 & 논리 연산자  (0) 2019.11.16
03. python - print()  (0) 2019.09.19
02. Python 환경변수 설정하기  (0) 2019.09.19
01. Windows에서 Python 설치하기  (0) 2019.09.19

+ Recent posts