티스토리 뷰

0. 기초 수학 연산

기초 수학 연산인 덧셈, 뺄셈, 곱셈, 나눗셈의 몫과 나머지를 구하는  방법을 설명하고자 한다.

 

1. 덧셈연산

+ 연산자는 이항 연산자로 두 변수의 값을 더한다. 

+= 연산자는 이항 연산자로 두 변수를 더하여 좌측에 있는 변수에 값을 넣는다. 

operator.add 함수는 + 연산자와 동일한 기능을 수행한다. 

operator.iadd 함수는 += 연산자와 동일한 기능을 함수내에서 수행하나 좌측변수를 반환하지는 않는다. 

# 07_00_PythonAddition

# 기본 할당 방법
FirstValue = 3
SecondValue = 2


# 연산자를 사용하는 방법
print(FirstValue + SecondValue)                     # 5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

FirstValue += SecondValue 
print(FirstValue)                                   # 5
print(SecondValue)                                  # 2

# 연산자 함수를 사용하는 방법
import operator
FirstValue = 3
SecondValue = 2

# + 연산자와 동일함
addValue = operator.add(FirstValue, SecondValue)
print(addValue)                                     # 5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# += 연산자와 동일하다고 하였으나 실제 작업 결과 다름
# 함수의 내에서의 처리 방법은 += 로직과 동일하나 값을 리턴하지는 않음
addValue = operator.iadd(FirstValue, SecondValue)
print(addValue)                                     # 5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

2. 뺄셈연산

- 연산자는 이항 연산자로 두 변수의 차이를 계산한다. 

-= 연산자는 이항 연산자로 두 변수의 차이를 계산하여 좌측 변수에 값을 넣는다. 

operator.sub 함수는 - 연산자와 동일한 기능을 수행한다. 

operator.isub  함수는 -= 연산자와 동일한 기능을 함수내에서 수행하나 좌측변수를 반환하지는 않는다.

# 07_01_PythonMethematicalSubtraction

FirstValue = 3
SecondValue = 2

# 연산자를 사용하는 방법
print(FirstValue - SecondValue)                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

FirstValue -= SecondValue 
print(FirstValue)                                   # 1
print(SecondValue)                                  # 2

# 연산자 함수를 사용하는 방법
import operator
FirstValue = 3
SecondValue = 2

subValue = operator.sub(FirstValue, SecondValue)
print(subValue)                                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# -= 연산자와 동일하다고 하였으나 실제 작업 결과 다름
# 함수의 내에서의 처리 방법은 -= 로직과 동일하나 값을 리턴하지는 않음
subValue = operator.isub(FirstValue, SecondValue)
print(subValue)                                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

3. 곱셈연산

* 연산자는 이항 연산자로 두 변수의 곱을 계산한다.

*= 연산자는 이항 연산자로 두 변수의 곱을 계산하여 좌측 변수에 값을 넣는다. 

operator.mul 함수는 * 연산자와 동일한 기능을 수행한다. 

operator.imul  함수는 *= 연산자와 동일한 기능을 함수내에서 수행하나 좌측변수를 반환하지는 않는다.

# 07_02_PythonMethematicalMultiplication

FirstValue = 3
SecondValue = 2

# 연산자를 사용하는 방법
print(FirstValue * SecondValue)                     # 6
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

FirstValue *= SecondValue 
print(FirstValue)                                   # 6
print(SecondValue)                                  # 2

# 연산자 함수를 사용하는 방법
import operator

FirstValue = 3
SecondValue = 2

multiValue = operator.mul(FirstValue, SecondValue)
print(multiValue)                                   # 6
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# *= 연산자와 동일하다고 하였으나 실제 작업 결과 다름
# 함수의 내에서의 처리 방법은 *= 로직과 동일하나 값을 리턴하지는 않음
multiValue = operator.imul(FirstValue, SecondValue)
print(multiValue)                                   # 6
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

4. 나눗셈연산

/ 연산자는 이항 연산자로 좌측 변수를 우측 변수로 나눗셈을 한다.

// 연산자는 이항 연산자로 좌측 변수를 우측 변수로 나누고 정수 부분만 반환한다. 

/= 연산자는 이항 연산자로 좌측 변수를 우측 변수로 나누어 좌측 변수에 값을 넣는다.

//= 연산자는 이항 연산자로 좌측 변수를 우측 변수로 나누어 좌측 변수에 정수 값을 넣는다.

operator.truediv 함수는 / 연산자와 동일한 기능을 수행한다. 

operator.floordiv 함수는 // 연산자와 동일한 기능을 수행한다.

operator.itruediv 함수는 /= 연산자와 동일한 기능을 함수내에서 수행하나 좌측변수를 반환하지는 않는다.

operator.ifloordiv  함수는 //= 연산자와 동일한 기능을 함수내에서 수행하나 좌측변수를 반환하지는 않는다.

# 07_03_PythonMethematicalDivision

FirstValue = 3
SecondValue = 2

# 연산자를 사용하는 방법
print(FirstValue / SecondValue)                     # 1.5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

floorValue = FirstValue // SecondValue      
print(floorValue)                                   # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

FirstValue /= SecondValue 
print(FirstValue)                                   # 1.5
print(SecondValue)                                  # 2

FirstValue = 3
SecondValue = 2
FirstValue //= SecondValue
print(FirstValue)                                   # 1
print(SecondValue)                                  # 2


# 연산자 함수를 사용하는 방법
import operator
FirstValue = 3
SecondValue = 2

trueValue = operator.truediv(FirstValue, SecondValue)
print(trueValue)                                    # 1.5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

floorValue = operator.floordiv(FirstValue, SecondValue)
print(floorValue)                                   # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# /= 연산자와 동일하다고 하였으나 실제 작업 결과 다름
# 함수의 내에서의 처리 방법은 /= 로직과 동일하나 값을 리턴하지는 않음
iTrueValue = operator.itruediv(FirstValue, SecondValue)
print(iTrueValue)                                   # 1.5
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# //= 연산자와 동일하다고 하였으나 실제 작업 결과 다름 
# 함수 내에서 처리방법은 /= 로직과 동일하나 값을 리턴하지는 않음 
iFloorValue = operator.ifloordiv(FirstValue, SecondValue)
print(iFloorValue)                                  # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

5. 나머지연산

% 연산자는 이항 연산자로서 좌변 변수의 값을 우변 변수의 값으로 나누어 나머지 값을 반환한다. 

%= 연산자는 이항 연산자로서 좌변 변수의 값을 우변 변수의 값으로 나누어 나머지 값을 좌변변수에 넣는다.

operator.mod  함수는 % 연산자와 동일한 기능을 수행한다. 

operator.imod 함수는 %= 연산자와 동일한 기능을 함수 내에서 수행하나 좌측변수를 반환하지는 않는다. 

# 07_04_PythonMethematicalMod

FirstValue = 3
SecondValue = 2

modValue = FirstValue % SecondValue
print(modValue)                                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

FirstValue %= SecondValue
print(FirstValue)                                   # 1
print(SecondValue)                                  # 2

# 연산자 함수를 사용하는 방법
import operator

FirstValue = 3
SecondValue = 2

modValue = operator.mod(FirstValue, SecondValue)
print(modValue)                                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

# %= 연산자와 동일하다고 하였으나 실제 작업 결과 다름
# 함수의 내에서의 처리 방법은 %= 로직과 동일하나 값을 리턴하지는 않음
modValue = operator.imod(FirstValue, SecondValue)
print(modValue)                                     # 1
print(FirstValue)                                   # 3
print(SecondValue)                                  # 2

6. 지수연산

** 연산자는 이항 연산자로서 좌변 변수의 값을 우변 변수의 값만큼 곱한 값을 반환한다.

**= 연산자는 이항 연산자로서 좌변 변수의 값을 우변 변수의 값만큼 곱한 값을 좌변변수에 넣는다.

operator.pow  함수는 ** 연산자와 동일한 기능을 수행한다. 

operator.ipow 함수는 **= 연산자와 동일한 기능을 함수 내에서 수행하나 좌측변수를 반환하지는 않는다. 

# 07_05_PythonMehtematicalExponentaiation

FirstValue = 3
SecondValue = 2

expValue = FirstValue ** SecondValue
print(expValue)             # 9
print(FirstValue)           # 3
print(SecondValue)          # 2

FirstValue **= SecondValue
print(FirstValue)           # 9
print(SecondValue)          # 2

# 연산자 함수를 사용하는 방법
import operator

FirstValue = 3
SecondValue = 2
expValue = operator.pow(FirstValue, SecondValue)
print(expValue)             # 9
print(FirstValue)           # 3
print(SecondValue)          # 2

expValue = operator.ipow(FirstValue, SecondValue)
print(expValue)             # 9
print(FirstValue)           # 3
print(SecondValue)          # 2

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함