산술 연산자로 다음과 같은 것들이 있다.


연산자

기능

용례

+

덧셈


-

뺄셈


*

곱셈


/

나눗셈

결과는 실수형이다. (2.x에서는 int / int의 결과는 int)

//

자리내림 나눗셈

피연산자가 모두 정수인 경우 나눗셈의 결과 소수점 아래는 버리고 정수만 취한다.

둘 중 하나라도 실수라면 / 연산자와 동일함.

**

거듭제곱

2**10, (1+2j)**20

%

나머지

3%4, -10%3, 12.345%0.4


  나눗셈의 경우 ver 2.x에서는 정수형끼리의 나눗셈은 결과도 정수형이 된다. 다음 결과를 확인해 보라.


----------------------------------------------------------

>>> 4/5

>>> a, b=11,5 #a에 11, b에 5를 대입한다.

>>> b/a

----------------------------------------------------------


하지만 3.x버전에서는 나눗셈의 결과는 무조건 실수로 된다.


■ 2.x버전에서는 정수간 나눗셈의 결과는 정수였다. 즉 1/2는 0, 2/3은 1이다. 하지만 3.x버전에서는 정수간 나눗셈의 결과는 나누어 떨어지는 경우라도 무조건 실수형이 된다. 즉, 1/2는 0.2, 6/3은 2.0이 된다.


  연산자 //는 두 피연산자가 모두 정수일 경우 결과값이 실수이면 소수점 아래는 버린다.


······································································································································

>>> 9//2 # 결과는 4 (정수)

>>> 9//2.0 # 결과는 4.5(실수)

······································································································································


  연산자 %는 나눗셈 수행 후 정수몫을 구하고 난 나머지를 구하는 연산자이다. 다음을 확인해보라.


······································································································································

>>> 3%2

>>> 11.5 % 0.2

······································································································································


연산자 **는 거듭제곱 연산자이다. 다음 연산의 결과값들을 예상해 보자.


······································································································································

>>> 2**10

>>> a, c = 3, 4+5j #변수 a와 c를 차례로 초기화시킨다.

>>> c**a

······································································································································


  복소수의 거듭제곱도 (4+5j)*(4+5j)*(4+5j) 의 결과값을 보여준다. 파이썬에서는 복소수에 대한 산술 연산도 기본적으로 지원하므로 쉽게 수행할 수 있음을 알 수 있다.

#00003


Posted by 살레시오
,