Python

[Python] - 산술연산자 7가지 ( +, -, *, /, **, //, %)

경토리 2023. 2. 11. 23:23
728x90

1. 덧셈 ( + )

문자열도 덧셈가능. 

>>> 2+3

5

>>> "hi"+"hello"

'hihello'

 

2. 뺄셈 ( - )

문자열은 뺄셈 불가. 

>>> 2-3

-1

>>> "hi"-"hello"

Traceback (most recent call last):

 

  File "<ipython-input-6-42d8c0eec2fe>", line 1, in <module>

    "hi"-"hello"

 

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

 

3. 곱셈 ( * )

 

- 문자열도 정수와 곱셈 가능. 실수와는 곱셈 불가. 

>>> 3*4

12

>>> "hi"*3

'hihihi'

>>> "hi"*3.0

Traceback (most recent call last):

 

  File "<ipython-input-11-80e9c3f36b0c>", line 1, in <module>

    "hi"*3.0

 

TypeError: can't multiply sequence by non-int of type 'float'

 

4. 나눗셈 ( / )

>>> 3/5

0.6

 

5. 거듭제곱 ( ** )

>>> 3**3

27

>>> 2**5

32

 

6. 몫 ( // )

>>> 10//3

3

>>> 5//2

2

 

7. 나머지 ( % )

>>> 10%3

1

>>> 5%3

2

 

728x90