Python
연산자 1 - 사칙연산자
연산자 1 - 사칙연산자
안녕하세요.
이번시간에는 연산자에 대해서 알아보겠습니다.
연산자에는 여러가지 연산자가 있습니다.
사칙 연산자, 대입 연산자, 비교 연산자, 논리 연산자, 비트 연산자
이 중 대입 연산자는 우리가 앞에서 이미 사용을 했습니다.
변수에 값을 대입할 때 사용했던 = 가 대입연산자 중 하나입니다.
이번 시간에는 사칙 연산자에 대해서 알아보겠습니다.
사칙 연산자는 우리가 알고 있는 덧셈, 곱셈, 나눗셈, 곱셈이 있습니다.
거기에 더해 프로그래밍에서는 나눗셈의 나머지를 구하는 연산자도 있으며 파이썬에서는 거듭제곱도 있습니다.
사칙 연산자 + 사용하기
먼저 더하기를 해볼게요.
기호 +를 사용합니다.
print(5+8)
위와 같이 사용하여 5와 8을 덧셈합니다.
print(5+8)

결과는 바로 아래에서 확인할 수 있습니다.

이렇게 숫자와 숫자의 덧셈을 답을 구할 수 있습니다.
+를 연결연산자로 사용하기
앞에서도 했지만 문자와 문자간에 + 를 사용하면 이는 연결연산자로 사용됩니다.
print("Hello" + "World")

결과는 바로 아래에서 확인할 수 있습니다.

사칙 연산자 - 사용하기
이번엔 뺄셈을 해볼게요.
10에서 5를 뺀다면 다음과 같이 표시합니다.
print(10-5)

결과는 바로 아래에서 확인할 수 있습니다.

사칙 연산자 / 사용하기
나눗셈은 기호 /를 사용합니다.
5를 2로 나누면..
print(5/2)

결과는 바로 아래에서 확인할 수 있습니다.

5를 2로 나눈값인 2.5가 나타납니다.
여기에 나눗셈의 나머지를 구하는 %를 사용하면 나머지값만 나타납니다.
5를 2로 나누면 2.5까지 계산을 하지만 %를 사용하면 2까지만 계산하고 나머지 값 1을 표시합니다.
print(5%2)

결과는 바로 아래에서 확인할 수 있습니다.

나머지 값인 1을 출력합니다.
또, 몫만 구할 수도 있습니다.5를 2로 나누면 2.5인데 여기서 2만 반환합니다.
기호는 //를 사용합니다.
print(5//2)

결과는 바로 아래에서 확인할 수 있습니다.

사칙 연산자 * 사용하기
이번엔 곱셈을 해봅시다.
곱셈을 하려면 * 를 사용합니다. 프로그래밍에서 *는 곱셈 기호입니다.
5와 7을 곱하면
print(5*7)

결과는 바로 아래에서 확인할 수 있습니다.

5와 7을 곱한 값 35를 출력합니다.
이제 마지막 지수승을 볼까요.
사칙 연산자 ** 사용하기
이번엔 지수승입니다. 2에 5승을 계산한다면 (2**5)를 사용합니다.
그럼 결과는 2를 5번 곱한 값을 출력합니다. 즉 2*2*2*2*2의 결과를 표시합니다.
2**5를의 결과를 보면
print(2**5)

결과는 바로 아래에서 확인할 수 있습니다.

2를 5회 곱한 값인 32가 출력됩니다.
다음시간에는 대입연산자에 대해서 알아봅시다. =도 있지만 그외에도 여러가지가 있습니다.
//=$langList['bottomThankyou'][$langMode]?>