본문 바로가기

컴퓨터/Python

수치

int 정수를 쓰면 10진수 정수로 인식

>>> year = 2008

>>> month = 12

>>> print (year, month)

2008 12


정수 앞에 '0o'는 8진수 '0b'는 2진수 '0x'는 16진수로 인식

>>> 0o10

8

>>> 0b10

2

>>> 0x10

16


10진수를 입력으로 받아서 원하는 진수로 변환

>>> oct(38)

'0o46'

>>> hex(38)

'0x26'

>>> bin(38)

'0b100110'


type()함수를 자료형 변환

>>> type(1)

<class 'int'>

>>> type(2**31)

<class 'int'>

>>> type(3.14)

<class 'float'>

>>> type(3.14e-2)

<class 'float'>


복소수 표현법 

>>> x = 3 - 4j

>>> type(x)

<class 'complex'>

>>> x.imag            허수부분

-4.0

>>> x

(3-4j)

>>> x.real               실수부분
3.0
>>> x.conjugate()
(3+4j)

연산
>>> r =2
>>> circle_area = 3.14*(r**2)
>>> x = 3
>>> y = 4
>>> triangle_area = x*y/2
>>> print(circle_area, triangle_area)
12.56 6.0

정수나누기(//)
>>> 2 // 3
0
>>> 5 // 2
2

'컴퓨터 > Python' 카테고리의 다른 글

튜플  (0) 2013.07.02
세트  (0) 2013.07.02
리스트  (0) 2013.07.02
유니코드  (0) 2013.07.02
문자  (0) 2013.07.02