[Python] Baekjun 1000 times - A+B
We have summarized the basic input/output solution for adding two numbers by converting the input string to an integer in Python.
한국어 원문은 여기에서 볼 수 있습니다.
[Python] Baekjun 1000 times - A+B
Solution
1
2
3
A, B = input().split()
print(int(A) + int(B))
input()
- When input as
input(), it is saved as a string.
Another solution
1
a, b = map(int, input().split())
You can receive it directly as int.
1
a, b = int(input().split()) # 잘못된 풀이
-> Since the result of input().split() is in the form of a list, an error occurs if the list is wrapped with int().
map() function
- A tool that applies the “same function” to multiple values in a list at once
1
map(함수, 반복 가능한 데이터)
``` python
예시
nums = [1, 2, 3]
result = map(int, nums) # nums 안에 있는 값들 하나씩 꺼내서 int() 적용 print(list(result)) # [1, 2, 3] ```
- Things to note
- If I print it right away, it doesn’t come out.
- If you immediately output the object to which
map()is applied, a strange object appears because the corresponding value outputs a map object (iterator). - Therefore, if you want to output the value to whichmapis applied, type conversion is required, such as wrapping it once withlist()and outputting it in list form. - Use it once and you’re done (iterator feature) ``` python a = map(int, [1,2,3])
print(list(a)) # [1,2,3] print(list(a)) # [] (이미 다 써버림) ```