본문으로 바로가기

 

The provided code stub reads two integers from STDIN, a and b. Add code to print three lines where:

  1. The first line contains the sum of the two numbers.
  2. The second line contains the difference of the two numbers (first - second).
  3. The third line contains the product of the two numbers.

Example

 

Arithmetic Operators

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(a+b)
    print(a-b)
    print(a*b)

 

 

해설

if__name__ == '__main__'의 사용법과 연산자 기능을 활용하는 코드이다.

 

if__name__ == '__main__'

>>> def plus(a, b):
...     return a + b

>>> def minus(a, b):
...     return a - b


>>> if __name__ == '__main__':
...     print(plus(1, 2))
...     print(minus(1, 2))
3
-1

if__name__ == '__main__'은 변수 __name__의 값이 '__main__'이 맞는지 여부를 확인한다.
def 함수를 통해 프로그래밍한 후, 그 코드가 if __name__ == '__main__'아래에 있는 경우 실행할 수 있다.

쉽게 말하자면 프로그램의 '시작점'을 알려주는 코드이다.