The provided code stub reads two integers from STDIN, a and b. Add code to print three lines where:
- The first line contains the sum of the two numbers.
- The second line contains the difference of the two numbers (first - second).
- 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__'아래에 있는 경우 실행할 수 있다.
쉽게 말하자면 프로그램의 '시작점'을 알려주는 코드이다.
'Develop > hackerrank.com' 카테고리의 다른 글
HackerRank Python Solution / Write a function (0) | 2021.12.31 |
---|---|
HackerRank Python Solution / Loops (0) | 2021.12.30 |
HackerRank Python Solution / Division (0) | 2021.11.12 |
HackerRank Python Solution / If-Else (0) | 2021.11.11 |
HackerRank Python Solution / "Hello World!" (0) | 2021.11.11 |