입출력(I/O)

입력

적은값 → 변수 갯수 직접 입력해서 받기

많은 값 → 리스트로 입력

기본(한개)

input() 함수로 받기

**한개**
# 기본(문자열)
a = input()
# 숫자형(int 형변환)
a = int(input())

적은 값 - 문제에서 몇개 인지 지정 됬을때

split(), map() 함수 활용하기

# 기본(문자열)
a, b, c = input().split()

# 숫자형(map 이용 형변환)
a, b, c = map(int, input().split())

map(int, input().split()) → 해당 형태에 주의!

많은 값(N개 입력)

리스트로 입력 받기
# 기본(문자열)
data_str = list(input().split())
# 숫자형
data_num = list(map(int, input().split()))
**# 오타 안나게 형태에 주의!**

# 리스트 컴프리헨션(List Comprension)
numbers = [int(x) for x in input().split()]

빠른 입력

import sys
# input을 덮어써서 사용 가능
input = sys.stdin.readline

n = int(input().rstrip())
data = list(map(int, input().split()))

출력

기본

print(변수 or 값)

형식 지정

# f-string
print(f"{변수1} {변수2}")
print("{} {}".format(변수1, 변수2))  # format() 메서드

빠른 출력

import sys

# 한 줄씩 출력
sys.stdout.write(str(변수) + '\\n')

# 여러 값을 한 줄에 출력(언패킹)
print(*리스트)

실제 적용

첫줄에 N개 → 아래로 한개씩

# 담을 빈 리스트 만들기
items = []
# 들어올 갯수 입력받기
n = int(input())
# N개만큼 입력받아서 리스트에 저장
for _ in range(n):
	item = int(input())
	items.append(item)

첫줄에 N개 → 아랫줄 가로로 N개

# 들어올 갯수 입력받기
n = int(input())
# N개만큼 입력받아서 리스트에 저장
nums = list(map(int, input().split())

Last updated