자료형

CODEDRAGON Development/Python

반응형

   

자료형

  • 데이터 형(data type)이라고도 합니다.
  • 데이터의 유형을 정의합니다.
  • 자료형에는 문자와 숫자등이 있습니다.

   

   

   

   

데이터 타입 확인하기

>>> 10
10
>>> type(1)
<class 'int'>

  

   

>>> type(3.14)
<class 'float'>

   

>>> abcdefg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abcdefg' is not defined
>>> print('abcdefg')
abcdefg

   

>>> print('python')
python
>>> type('python')
<class 'str'>

   

>>> print('안녕하세요')
안녕하세요
>>> type('안녕하세요')
<class 'str'>
>>> type(u'안녕하세요')
<class 'str'>

   

   

   

타입확인이 중요한 이유

정수형과 정수형의 연산시 결과도 정수형으로 나와

1/2의 결과인 0.5가 0으로 출력될 수 있습니다 (타 언어, python2.7)

>>> 1/2
0.5
>>> 1/2.0
0.5
>>>

   

>>> 3.14
3.14
>>>

  

   

   

반응형

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

수치 - complex  (0) 2015.06.26
수치 - float  (0) 2015.06.20
ex01-주석  (0) 2015.06.03
ex01-주석  (0) 2015.05.28
주석(comment)  (0) 2015.05.23