Python_산술연산자(+, -)

장해수·2023년 6월 5일
  1. 연산자
  • 피연산자를 통해 계산을 함
  • result = data1 + data2
  1. 연산자 종류
    1) 산술 연산자: +, -, , /, %(나눗셈-나머지), //(나눗셈-몫), **(제곱)
    2) 할당 연산자: =, +=, -=,
    =, /=, %=, //=
    3) 비교 연산자: >, >=, <=, ==, !=
    4) 논리 연산자: and, or, not

  2. 덧셈 연산자
    1) 정수 + 정수

num1 = 10
num2 = 20

print (num1 + num2)

2) 실수 + 실수

num1 = 3.14
num2 = 1.592

print (num1 + num2)
print ('num1 + num2 = %.2f' % (num1 + num2))

3) 정수 + 실수

num3 = 3
num4 = 0.14

print(num3 + num4)

4) 문자 + 문자

str1 = 'Jeno '
str2 = 'is '
str3 = 'my son.'

print(str1 + str2 + str3)

5) 숫자 + 문자 >> 불가능

str1 = 'Jeno '
str2 = 'is '
num3 = 24

print(str1 + str2 + num3)
TypeError: can only concatenate str (not "int") to str
  1. 뺄셈 연산자
    1) 정수 - 정수
num1 = 10
num2 = 20
result = num1 - num2

print(f'num1: {num1}')
print(f'num2: {num2}')
print(f'result: {result}')

2) 실수 - 실수

fnum1 = 3.14
fnum2 = 0.14
result = fnum1 - fnum2

print(f'fnum1: {fnum1}')
print(f'fnum2: {fnum2}')
print(f'result: {result}')
print(f'type of result: {type(result)}')

3) 정수 - 실수

fnum1 = 3.14
num2 = 3
result = fnum1 - num2

print(f'fnum1: {fnum1}')
print(f'num2: {num2}')
print('result: %.2f' % result)
print(f'type of result: {type(result)}')

4) 문자 - 문자 >> 불가능

<실습>
1. 덧셈 연산자

  • 코드
kor = int(input('국어 점수: '))
eng= int(input('영어 점수: '))
mat = int(input('수학 점수: '))
total = kor + eng + mat

print('국어 점수 {}'.format(kor))
print('영어 점수 {}'.format(eng))
print('수학 점수 {}'.format(mat))
print('합계 {}'.format(total))
  • 결과
  1. 뺄셈 연산자
  • 코드
partTimeMoney = int(input('알바비: '))
cardMoney = int(input('카드값: '))
result = partTimeMoney - cardMoney

print('partTimeMoney: {}원'.format(partTimeMoney))
print('cardMoney: {}원'.format(cardMoney))
print('남은 돈: {}원'.format(result))

profile
데이터 진행시켜

0개의 댓글