프로그래밍 언어/Python

산술 연산자(덧셈)

ruby-jieun 2022. 12. 30. 15:19

 

연산자

 

 

 

* 연산자의 종류

 

  • 산술 연산자 : +, -, *, / , %, //, **

  • 할당 연산자 : =, +=, -=, *=, /=, %=, //=

  • 비교 연산자 : >, >=, <, <=, ==, !=

  • 논리 연산자 : and, or, not

 

 

 

num1 = 3
num2 = 4

fNum1 = 1.233
fNum2 = 0.12

str1 = 'Kim'
str2 = ' '
str3 = 'jieun'

 

 

 

 

 

덧셈 연산자

 

 

 

 

* 덧셈 연산자

    • 정수를 이용한 덧셈

result = num1 + num2
print(f'result : {result}')
result : 7

 

 

    • 실수를 이용한 덧셈

result = fNum1 + fNum2
print(f'result : {result}')
print('result : %.2f' % result)
result : 1.3530000000000002
result : 1.35

 

 

    • 정수와 실수를 이용한 덧셈

result = num1 + fNum2
print(f'result : {result}')
result : 3.12

 

 

    • 문자를 이용한 덧셈

result = str1 + str2 + str3
print(f'result : {result}')
result : Kim jieun

 

 

    • 숫자와 문자를 이용한 덧셈

불가능하다.

TypeError : unsupported operand type(s) for +: 'int' and 'str'