제로베이스 2일차(연산자)

Eunwoo Lee·2022년 7월 5일
0

연산자

산술연산자 + - * / % // **

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

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

논리연산자 and, or, not

실수를 이용한 덧셈

fnum1 = 3.14
fnum2 = 0.12
result = fnum2 + fnum1

print(f'result = {result}')
print('result = %0.2f' %result)

정수를 이용한 덧셈

num1 = 10
num2 = 20
result = num1 + num2

print(f'result = {result}')
'

문자를 이용한 덧셈

str1 = 'good'
str2 = ' '
str3 ='morning'
result = str1 + str2 + str3

print(f'result = {result}')

문자열덧셈은 가능하나 뺼셈은 오류발생한다

실습
점수를 입력받고 합계를 출력하자
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))

국어 점수 : 85
영어 점수 : 90
수학 점수 : 95
국어 점수 85
영어 점수 90
수학 점수 95
합계 270

나눗셈 연산자

num1 = 10
num2 = 3
print(f'num1 % num2 = {num1 % num2}')
print(f'num1 / num2 = {num1 / num2}')
print(f'num1 // num2 = {num1 // num2}')

num1 % num2 = 1
num1 / num2 = 3.3333333333333335
num1 // num2 = 3

%연산자는 나머지
/연산자는 나머지와 몫
//연산자는 몫

0을 나눗셈하면 무조건0

num1 = 0
num2 = 3
result = num1 / num2
print(f'result = {result}')

result = 0.0

0으로 나누는 경우

num1 = 0
num2 = 3
result = num2 / num1
print(f'result = {result}')

ZeroDivisionError: division by zero
오류발생

나눗셈을 하면 데이터 타입은 항상 float이다

divmod() 함수를 사용하면 나머지와 몫을 한번에 구할수있다.

num1 = 10
num2 = 3
result = divmod(num1, num2)

print('result = {}'.format(result))
print('몫 = {}'.format(result[0]))
print('나머지 = {}'.format(result[1]))

result = (3, 1)
몫 = 3
나머지 = 1

제곱근 구하는 공식

n ** (1/m)

math 모듈의 sqrt()와 pow() 함수사용

sqrt()함수는 제곱근을 구할떄 사용하며(주의할점은 2제곱근만 구할수잇음)
pow()함수는 제곱을 구할때 사용한다

import math

firstmoney = 200
result = (firstmoney) * math.pow(2, 11)
print(f'result = {result}')

result = int(result)
str_result = format(result, ',') #float형에서 string형으로 변경 3자리마다 ,찍힘
print(str_result, '원')

result = 409600.0
409,600 원

문자를 비교할때는 아스키 코드를 이용해야한다.(구글검색참고)

문자와 아스키 코드 변환 할떄는 ord()함수나 chr()함수를 사용한다
print("'A' -> {}".format(ord('A')) )
print('65 -> {}'.format(chr(65)))
'A' -> 65
65 -> A

실습
#국어, 영어, 수학 점수를 입력하고 평균이 70점 이상이면 True를
출력하는 코드를 작성해 보자.(단 과목별 점수가 최소 60이상인 경우만 True를 출력한다)
kor_socore = int(input('국어 점수 입력 : '))
eng_socore = int(input('영어 점수 입력 : '))
math_socore = int(input('수학 점수 입력 : '))
aver = (kor_socore + eng_socore + math_socore) / 3
print('국어점수 : {}'.format(kor_socore))
print('영어점수 : {}'.format(eng_socore))
print('수학점수 : {}'.format(math_socore))
print('평균점수 : {}, 결과 : {}'.format(aver, aver >= 70))
print('국어점수 : {}, 결과 : {}'.format(kor_socore, kor_socore >= 60))
print('영어점수 : {}, 결과 : {}'.format(eng_socore, eng_socore >= 60))
print('수학점수 : {}, 결과 : {}'.format(math_socore, math_socore >= 60))
result = (kor_socore >= 60) and (eng_socore >= 60) and (math_socore >= 60)
print('과락 결과 : {}'.format(result))
print('최종결과 : {}'.format((aver >= 70) and result))

국어 점수 입력 : 85
영어 점수 입력 : 90
수학 점수 입력 : 55
국어점수 : 85
영어점수 : 90
수학점수 : 55
평균점수 : 76.66666666666667, 결과 : True
국어점수 : 85, 결과 : True
영어점수 : 90, 결과 : True
수학점수 : 55, 결과 : False
과락 결과 : False
최종결과 : False

0개의 댓글