---
title: Python AtCoder入門 第1講 Pythonの基礎と入出力
tags: 
author: [佐藤](https://image.docswell.com/user/aomori_sato)
site: [Docswell](https://www.docswell.com/)
thumbnail: https://bcdn.docswell.com/page/4EZLZ3KG73.jpg?width=480
description: Python AtCoder入門 第1講 Pythonの基礎と入出力 by 佐藤
published: September 19, 26
canonical: https://image.docswell.com/s/aomori_sato/ZPRQQX-2026-09-19-180019
---
# Page. 1

![Page Image](https://bcdn.docswell.com/page/4EZLZ3KG73.jpg)

Python AtCoder入門
第1講 Pythonの基礎と入出力
第0講では、AtCoderがどういう場所で、なぜPythonで挑むのかをお話ししました。
ここからは実際に手を動かしていきます。
1


# Page. 2

![Page Image](https://bcdn.docswell.com/page/Y76WZXQQ7V.jpg)

今回のテーマ
最初のテーマは、
入出力
です。
地味に聞こえるかもしれませんが、ここは本書で最も重要な講です。
2


# Page. 3

![Page Image](https://bcdn.docswell.com/page/G75MWXR274.jpg)

なぜ入出力が重要なのか
AtCoderのすべての問題は、
入力を受け取ることから始まり、出力することで終わる
からです。
入力で詰まれば、どれだけ良い解き方を思いついても1点にもなりません。
3


# Page. 4

![Page Image](https://bcdn.docswell.com/page/9J29QXD1ER.jpg)

この講のゴール
問題文の入力形式を見た瞬間に、
受け取りのコードが指から出てくる状態
を目指します。
入力で迷わなくなれば、考えるべきことは「解き方」だけになります。
4


# Page. 5

![Page Image](https://bcdn.docswell.com/page/DEY4W824JM.jpg)

コードファイル名の方針
本書では、コード例ごとにファイル名を付けます。
例：
hello.py
print_values.py
read_integer.py
for.py
手元で試すときは、同じ名前で保存して実行してください。
5


# Page. 6

![Page Image](https://bcdn.docswell.com/page/VJNY9Q8678.jpg)

1-1 AtCoderとPython
AtCoderの問題を解くプログラムは、次の3つを行います。
1. 標準入力からデータを読む
2. そのデータを使って計算する
3. 標準出力に答えを書く
6


# Page. 7

![Page Image](https://bcdn.docswell.com/page/YE9P2DKRJ3.jpg)

標準入力とは
標準入力とは、
プログラムに流し込まれてくるデータ
のことです。
Pythonでは、
input()
で1行ずつ読み取ります。
7


# Page. 8

![Page Image](https://bcdn.docswell.com/page/GE8D53RGED.jpg)

標準出力とは
標準出力とは、
プログラムが外に出す文字
のことです。
Pythonでは、
print()
で出力します。
8


# Page. 9

![Page Image](https://bcdn.docswell.com/page/LELMYVKX7R.jpg)

AtCoderで使う基本関数
AtCoderの基本は、とてもシンプルです。
input()
print()
読んで、書く。
この2つを自在に使えるようにします。
9


# Page. 10

![Page Image](https://bcdn.docswell.com/page/4JMYNG23JW.jpg)

問題文の「入力形式」を読む
AtCoderの問題文には、必ず入力形式があります。
例：
N
A B
これは、
1行目に整数 N
2行目に整数 A と B
が与えられる、という意味です。
10


# Page. 11

![Page Image](https://bcdn.docswell.com/page/PJR9DZMR79.jpg)

実際の入力例
入力形式が、
N
A B
なら、実際の入力はたとえばこうなります。
5
3 7
入力形式とコードの対応を覚えることが、この講の中心です。
11


# Page. 12

![Page Image](https://bcdn.docswell.com/page/PEXQ19VYJX.jpg)

提出から判定までの流れ
コードが書けたら、提出フォームに貼り付けます。
1. 言語を選ぶ
2. コードを貼る
3. 提出する
4. 判定を待つ
5. AC なら正解
本書では基本的に PyPy3 を使います。
12


# Page. 13

![Page Image](https://bcdn.docswell.com/page/3EK92PQ4ED.jpg)

提出前にコードテスト
提出する前に、
コードテスト
で入力例を試しましょう。
入力例を入れる
実行する
出力例と一致するか確認する
一致したら提出する
この習慣で、無駄なWAを減らせます。
13


# Page. 14

![Page Image](https://bcdn.docswell.com/page/L73W4XPD75.jpg)

1-2 出力の基本
まずは出力から始めます。
答えを出力できなければ、合っているかどうかを確認できません。
使う関数は、
print()
です。
14


# Page. 15

![Page Image](https://bcdn.docswell.com/page/87DKQ2L2JG.jpg)

printの基本
は、渡されたものを標準出力に書き出します。
最後には自動で改行が付きます。
print()
print(&quot;Hello&quot;)
print(42)
print(1 + 2)
15


# Page. 16

![Page Image](https://bcdn.docswell.com/page/VJPKL12LE8.jpg)

hello.py
# Print a fixed message to standard output
print(&quot;Hello, AtCoder!&quot;)
出力：
Hello, AtCoder!
文字列は &quot; &quot; で囲みます。
16


# Page. 17

![Page Image](https://bcdn.docswell.com/page/2EVVQYD6EQ.jpg)

数値と式の出力
数値はそのまま出力できます。
print(42)
式を渡すと、計算結果が出力されます。
print(1 + 2)
出力：
3
17


# Page. 18

![Page Image](https://bcdn.docswell.com/page/57GLW9Y2EL.jpg)

複数の値を出力する
にカンマ区切りで複数の値を渡すと、
空白区切りで1行に出力
されます。
print()
print(3, 7)
print(&quot;A&quot;, 1, &quot;B&quot;)
18


# Page. 19

![Page Image](https://bcdn.docswell.com/page/4EQY3WM9JP.jpg)

出力例
print(3, 7)
print(&quot;A&quot;, 1, &quot;B&quot;)
出力：
3 7
A 1 B
AtCoderでは、この形をよく使います。
19


# Page. 20

![Page Image](https://bcdn.docswell.com/page/KJ4W1K4R71.jpg)

sepとend
の区切り文字や行末文字は変更できます。
sep ：値と値の間に入る文字
end ：出力の最後に付く文字
既定値は、
print()
sep=&quot; &quot;
end=&quot;\n&quot;
です。
20


# Page. 21

![Page Image](https://bcdn.docswell.com/page/LE1YGX4V7G.jpg)

print_options.py
print(3, 7, sep=&quot;,&quot;)
print(3, 7, sep=&quot;&quot;)
print(&quot;A&quot;, end=&quot;&quot;)
print(&quot;B&quot;)
出力：
3,7
37
AB
21


# Page. 22

![Page Image](https://bcdn.docswell.com/page/GEWGKNX3J2.jpg)

sep=&quot;\n&quot;
sep=&quot;\n&quot;
を使うと、値ごとに改行できます。
print(3, 7, 10, sep=&quot;\n&quot;)
出力：
3
7
10
「1行ずつ出力せよ」という問題で使えます。
22


# Page. 23

![Page Image](https://bcdn.docswell.com/page/47ZLZ36GJ3.jpg)

print_values.py
A = 3
B = 7
C = 10
# Print with spaces
print(A, B, C)
# Print with commas
print(A, B, C, sep=&quot;,&quot;)
# Print one value per line
print(A, B, C, sep=&quot;\n&quot;)
23


# Page. 24

![Page Image](https://bcdn.docswell.com/page/YJ6WZX2QJV.jpg)

print_values.py の出力
3 7 10
3,7,10
3
7
10
同じ値でも、出力形式によって書き方が変わります。
24


# Page. 25

![Page Image](https://bcdn.docswell.com/page/GJ5MWX22J4.jpg)

出力形式は問題文の通りに
AtCoderでは、出力形式が厳密です。
たとえば、
Yes と YES を間違える
空白区切りを改行区切りにする
デバッグ用の print を消し忘れる
これらはすべてWAになります。
25


# Page. 26

![Page Image](https://bcdn.docswell.com/page/LE3W4XK1E5.jpg)

1-3 入力の基本
ここからが本番です。
入力では、
input()
を使います。
ただし、最初にとても大切な注意点があります。
26


# Page. 27

![Page Image](https://bcdn.docswell.com/page/8EDKQ23K7G.jpg)

inputは文字列を返す
input()
が返すのは、必ず
文字列
です。
数字が書かれた行を読んでも、返ってくるのは数値ではありません。
27


# Page. 28

![Page Image](https://bcdn.docswell.com/page/V7PKL143J8.jpg)

input_string.py
入力：
5
コード：
N = input()
print(N + N)
出力：
55
N
は文字列 &quot;5&quot; なので、足し算ではなく連結になります。
28


# Page. 29

![Page Image](https://bcdn.docswell.com/page/2JVVQYXNJQ.jpg)

intで数値に変換する
数として計算したいなら、
int()
で整数に変換します。
N = int(input())
print(N + N)
入力が 5 なら、出力は 10 になります。
29


# Page. 30

![Page Image](https://bcdn.docswell.com/page/5EGLW9V5JL.jpg)

read_integer.py
# Read one integer from standard input
N = int(input())
# Calculate and print the answer
print(N * N)
入力例：
6
出力例：
36
30


# Page. 31

![Page Image](https://bcdn.docswell.com/page/4JQY3W6L7P.jpg)

覚える形
整数を1つ受け取るときは、
N = int(input())
です。
これは今後ずっと使います。
整数1つ = int(input())
と丸ごと覚えましょう。
31


# Page. 32

![Page Image](https://bcdn.docswell.com/page/K74W1K45E1.jpg)

1行に複数の値
次は、1行に2つの整数がある場合です。
入力例：
3 7
このような入力では、
split()
map()
を使います。
32


# Page. 33

![Page Image](https://bcdn.docswell.com/page/LJ1YGX42EG.jpg)

split
split()
は、文字列を空白で区切ります。
line = input()
parts = line.split()
入力が、
3 7
なら、 parts は次のようになります。
[&quot;3&quot;, &quot;7&quot;]
33


# Page. 34

![Page Image](https://bcdn.docswell.com/page/GJWGKNX272.jpg)

map
split()
そこで、
しただけでは、中身は文字列のままです。
map(int, input().split())
と書きます。
これは、各要素に int を適用する、という意味です。
34


# Page. 35

![Page Image](https://bcdn.docswell.com/page/4EZLZ36473.jpg)

read_two_ints.py
# Read two integers separated by a space
A, B = map(int, input().split())
# Print the sum and the product
print(A + B, A * B)
入力例：
3 7
出力例：
10 21
35


# Page. 36

![Page Image](https://bcdn.docswell.com/page/Y76WZX2G7V.jpg)

AtCoder最重要テンプレート
1行に2つの整数なら、
A, B = map(int, input().split())
です。
3つなら、
A, B, C = map(int, input().split())
です。
左辺の変数の数を増やすだけです。
36


# Page. 37

![Page Image](https://bcdn.docswell.com/page/G75MWX2X74.jpg)

個数が変わる入力
次のような入力では、個数が N によって変わります。
N
A_1 A_2 ... A_N
この場合、変数を並べて受け取ることはできません。
37


# Page. 38

![Page Image](https://bcdn.docswell.com/page/9J29QX4QER.jpg)

listで受け取る
個数が変わる場合は、リストにします。
A = list(map(int, input().split()))
これで、たとえば次のようなリストになります。
[3, 1, 4, 1, 5]
38


# Page. 39

![Page Image](https://bcdn.docswell.com/page/DEY4W8MYJM.jpg)

read_list.py
# Read the number of integers
N = int(input())
# Read N integers on one line
A = list(map(int, input().split()))
# Print the sum
print(sum(A))
入力例：
5
3 1 4 1 5
出力例：
14
39


# Page. 40

![Page Image](https://bcdn.docswell.com/page/VJNY9QWR78.jpg)

N行の入力
次のような形式もよく出ます。
N
A_1
A_2
...
A_N
1行に1つずつ、N行にわたって与えられる形式です。
40


# Page. 41

![Page Image](https://bcdn.docswell.com/page/YE9P2DXZJ3.jpg)

forでN回読む
この場合は、 for 文でN回読みます。
N = int(input())
total = 0
for i in range(N):
A = int(input())
total = total + A
print(total)
41


# Page. 42

![Page Image](https://bcdn.docswell.com/page/GE8D539YED.jpg)

for.py
# Read the number of lines
N = int(input())
# Read N integers, one per line, and sum them up
total = 0
for i in range(N):
A = int(input())
total = total + A
print(total)
入力例：
4
10
20
30
40
42


# Page. 43

![Page Image](https://bcdn.docswell.com/page/LELMYVW97R.jpg)

for.py の出力
100
for i in range(N):
は、
N回繰り返す
という意味です。
詳しくは第4講で扱います。
43


# Page. 44

![Page Image](https://bcdn.docswell.com/page/4JMYNG9VJW.jpg)

1-4 AtCoder頻出の入力テンプレート
ここまでの内容を、入力形式ごとに整理します。
この節が第1講の心臓部です。
まずは、よく出る形を丸ごと覚えましょう。
44


# Page. 45

![Page Image](https://bcdn.docswell.com/page/PJR9DZGW79.jpg)

テンプレート1：整数1つ
入力形式：
N
コード：
N = int(input())
ファイル名例：
input_one_int.py
45


# Page. 46

![Page Image](https://bcdn.docswell.com/page/PEXQ19XVJX.jpg)

テンプレート2：整数2つ
入力形式：
A B
コード：
A, B = map(int, input().split())
ファイル名例：
input_two_ints.py
46


# Page. 47

![Page Image](https://bcdn.docswell.com/page/3EK92PW5ED.jpg)

テンプレート3：整数3つ
入力形式：
A B C
コード：
A, B, C = map(int, input().split())
ファイル名例：
input_three_ints.py
47


# Page. 48

![Page Image](https://bcdn.docswell.com/page/L73W4X1175.jpg)

テンプレート4：文字列1つ
入力形式：
S
コード：
S = input()
ファイル名例：
input_string.py
文字列は int() で変換しません。
48


# Page. 49

![Page Image](https://bcdn.docswell.com/page/87DKQ2XKJG.jpg)

テンプレート5：N個の整数が1行
入力形式：
N
A_1 A_2 ... A_N
コード：
N = int(input())
A = list(map(int, input().split()))
ファイル名例：
input_list.py
49


# Page. 50

![Page Image](https://bcdn.docswell.com/page/VJPKL1P3E8.jpg)

テンプレート6：N行の整数
入力形式：
N
A_1
...
A_N
コード：
N = int(input())
A = [int(input()) for i in range(N)]
ファイル名例：
input_lines.py
50


# Page. 51

![Page Image](https://bcdn.docswell.com/page/2EVVQY2NEQ.jpg)

テンプレート7：整数と文字列
入力形式：
N S
コード：
N, S = input().split()
N = int(N)
ファイル名例：
input_int_string.py
map(int, ...)
は使えません。
51


# Page. 52

![Page Image](https://bcdn.docswell.com/page/57GLW9R5EL.jpg)

対応表
入力形式
コード
N
N = int(input())
A B
A, B = map(int, input().split())
A B C
A, B, C = map(int, input().split())
S
S = input()
N
→ A_1 ... A_N
N = int(input())
/ A = list(map(int, input().split()))
52


# Page. 53

![Page Image](https://bcdn.docswell.com/page/4EQY3WVLJP.jpg)

文字列は変換しない
文字列を受け取るときは、
S = input()
です。
int()
を付けると、数字でない文字列のときに ValueError になります。
53


# Page. 54

![Page Image](https://bcdn.docswell.com/page/KJ4W1KM571.jpg)

N行を1行で受け取る
A = [int(input()) for i in range(N)]
これは、
N行読み取り、それぞれ整数に変換してリストにする
という意味です。
リスト内包表記と呼ばれます。
54


# Page. 55

![Page Image](https://bcdn.docswell.com/page/LE1YGX827G.jpg)

int_string.py
# Read an integer and a string
N, S = input().split()
# Convert only N to an integer
N = int(N)
print(N * 2, S)
入力例：
3 abc
出力例：
6 abc
55


# Page. 56

![Page Image](https://bcdn.docswell.com/page/GEWGKNZ2J2.jpg)

template_single_int.py
# Pattern 1: a single integer
N = int(input())
print(N)
対応する入力：
5
56


# Page. 57

![Page Image](https://bcdn.docswell.com/page/47ZLZ314J3.jpg)

template_two_ints.py
# Pattern 2: two integers on one line
A, B = map(int, input().split())
print(A, B)
対応する入力：
3 7
57


# Page. 58

![Page Image](https://bcdn.docswell.com/page/YJ6WZXLGJV.jpg)

template_string.py
# Pattern 3: a single string
S = input()
print(S)
対応する入力：
AtCoder
58


# Page. 59

![Page Image](https://bcdn.docswell.com/page/GJ5MWX1XJ4.jpg)

template_list.py
# Pattern 4: N followed by N integers on one line
N = int(input())
A = list(map(int, input().split()))
print(N, sum(A))
対応する入力：
5
1 2 3 4 5
59


# Page. 60

![Page Image](https://bcdn.docswell.com/page/9E29QX1Q7R.jpg)

template_lines.py
# Pattern 5: N followed by N lines
N = int(input())
A = [int(input()) for i in range(N)]
print(N, sum(A))
対応する入力：
3
10
20
30
60


# Page. 61

![Page Image](https://bcdn.docswell.com/page/D7Y4W8ZYEM.jpg)

template_int_string.py
# Pattern 6: an integer and a string on the same line
N, S = input().split()
N = int(N)
print(N * 2, S)
対応する入力：
4 apple
61


# Page. 62

![Page Image](https://bcdn.docswell.com/page/VENY9Q3RJ8.jpg)

6パターンでかなり戦える
この6パターンを覚えるだけで、
A問題・B問題の入力形式はかなり処理できます。
入力で悩む時間を減らし、
問題を解くことに集中しましょう。
62


# Page. 63

![Page Image](https://bcdn.docswell.com/page/Y79P2D9ZE3.jpg)

章末まとめ
AtCoderのプログラムは、
標準入力から読み、計算し、標準出力に書く
だけです。
使う基本関数は、
input()
print()
です。
63


# Page. 64

![Page Image](https://bcdn.docswell.com/page/G78D53DY7D.jpg)

章末まとめ：print
print()
は、複数の値を渡すと空白区切りで出力します。
print(A, B)
sep
で区切り文字を変更できます。
print(A, B, sep=&quot;,&quot;)
64


# Page. 65

![Page Image](https://bcdn.docswell.com/page/L7LMYVM9JR.jpg)

章末まとめ：input
が返すのは、必ず文字列です。
整数として使うなら、
input()
N = int(input())
1行に複数の整数なら、
A, B = map(int, input().split())
です。
65


# Page. 66

![Page Image](https://bcdn.docswell.com/page/4EMYNGYVEW.jpg)

章末まとめ：リスト入力
個数が変わる場合は、リストで受け取ります。
A = list(map(int, input().split()))
N行にわたる入力は、
A = [int(input()) for i in range(N)]
で受け取れます。
66


# Page. 67

![Page Image](https://bcdn.docswell.com/page/PER9DZ9WJ9.jpg)

章末まとめ：文字列
文字列はそのまま受け取ります。
S = input()
整数と文字列が混ざる場合は、必要なものだけ個別に変換します。
N, S = input().split()
N = int(N)
67


# Page. 68

![Page Image](https://bcdn.docswell.com/page/P7XQ19QVEX.jpg)

練習問題 1-1 和と積
2つの整数 A , B が与えられます。
A + B と A × B を、この順に空白区切りで1行に出力してください。
68


# Page. 69

![Page Image](https://bcdn.docswell.com/page/37K92P957D.jpg)

練習問題 1-1：入力と出力
入力：
A B
出力：
A+B A*B
入力例：
4 9
出力例：
13 36
69


# Page. 70

![Page Image](https://bcdn.docswell.com/page/LJ3W4XW1J5.jpg)

answer_1_1.py
# Read two integers separated by a space
A, B = map(int, input().split())
# Print the sum and the product separated by a space
print(A + B, A * B)
1行に2つの整数なので、 map を使います。
70


# Page. 71

![Page Image](https://bcdn.docswell.com/page/8JDKQ2KKEG.jpg)

練習問題 1-2 合計点
人の生徒がテストを受けました。
i 番目の生徒の点数は A_i です。
全員の点数の合計を出力してください。
N
71


# Page. 72

![Page Image](https://bcdn.docswell.com/page/VEPKL1K378.jpg)

練習問題 1-2：入力と出力
入力：
N
A_1 A_2 ... A_N
入力例：
5
10 20 30 40 50
出力例：
150
72


# Page. 73

![Page Image](https://bcdn.docswell.com/page/27VVQYVN7Q.jpg)

answer_1_2.py
# Read the number of students
N = int(input())
# Read N scores on one line
A = list(map(int, input().split()))
# Print the total score
print(sum(A))
N
は使わなくても、入力として必ず読み取ります。
73


# Page. 74

![Page Image](https://bcdn.docswell.com/page/5JGLW9L57L.jpg)

練習問題 1-3 最高気温
ある町で N 日間の気温を記録しました。
i 日目の気温は T_i です。
1行ずつ与えられる気温の最大値を出力してください。
74


# Page. 75

![Page Image](https://bcdn.docswell.com/page/47QY3WYLEP.jpg)

練習問題 1-3：入力と出力
入力：
N
T_1
T_2
...
T_N
入力例：
4
12
25
18
7
出力例：
25
75


# Page. 76

![Page Image](https://bcdn.docswell.com/page/KE4W1KW5J1.jpg)

answer_1_3.py
# Read the number of days
N = int(input())
# Read N temperatures, one per line
T = [int(input()) for i in range(N)]
# Print the maximum temperature
print(max(T))
N行入力は、リスト内包表記で受け取れます。
76


# Page. 77

![Page Image](https://bcdn.docswell.com/page/L71YGXY2JG.jpg)

answer_1_3_for.py
# Read the number of days
N = int(input())
# Read N temperatures, one per line
T = []
for i in range(N):
T.append(int(input()))
# Print the maximum temperature
print(max(T))
for
文で書いても同じ結果になります。
77


# Page. 78

![Page Image](https://bcdn.docswell.com/page/G7WGKNG2E2.jpg)

次回予告
次の第2講では、
受け取った数値を使って計算する方法
を学びます。
特に、Pythonの整数の割り算には注意点があります。
78


