入力処理

1行 / N列

N<4
```1 2 3

a, b, c = map(int, input().split())
print(a, b, c)
# 1 2 3
```
N=>4
```1 2 3 4 5

l = [int(_) for _ in input().split()]
print(l)
# [1, 2, 3, 4, 5]
```

M行 / 1列

M<4
```1
2
3

a = int(input())
b = int(input())
c = int(input())
print(a, b, c)
# 1 2 3
```
M=定数
```1
2
3
4
5

l = [int(input()) for _ in range(5)]
print(l)
# [1, 2, 3, 4, 5]
```
M=変数
```5 # 入力行数指定(M=5)
1
2
3
4
5

m = int(input())
l = [int(input()) for _ in range(m)]
print(l)
```
M=不定
```1
2
3
4
5
-1 # 終了フラグ

l = []
while True:
n = int(input())
if n == -1:
break
l.append(n)
print(l)
# [1, 2, 3, 4, 5]
```

M行 / N列

M=定数
```1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

l = [input().split() for _ in range(3)]
print(l)
# [['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], ['11', '12', '13', '14', '15']]
# 各要素がstr型なので取扱に注意
```
M=変数
```3 # 行数(M)を指定する
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

m = int(input())
l = []
for i in range(m):
l.append(list(map(int, input().split())))
print(l)
# [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
```

出力処理

int型

1次元リスト
```l = [1, 2, 3, 4, 5]
for i in l:
print(i, end=' ')
# 1 2 3 4 5
```
1次元リスト
```l = [1, 2, 3, 4, 5]
for i in range(len(l)):
print(l[i], end=' ')
# 1 2 3 4 5
```

```l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in l:
print(i, end=' ')
# [1, 2, 3] [4, 5, 6] [7, 8, 9]
```

```l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in l:
for j in i:
print(j, end=' ')
# 1 2 3 4 5 6 7 8 9
```

```l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in range(len(l)):
for j in range(len(l[i])):
print(l[i][j], end=' ')
# 1 2 3 4 5 6 7 8 9
```

str型

1次元リスト
```l = ['ab', 'cd', 'ef']
for i in l:
print(i, end=' ')
# ab cd ef
```
1次元リスト
```l = ['ab', 'cd', 'ef']
for i in l:
for j in i:
print(j, end=' ')
# a b c d e f
```
1次元リスト
```l = ['ab', 'cd', 'ef']
for i in range(len(l)):
print(l[i], end=' ')
# ab cd ef
```
1次元リスト
```l = ['ab', 'cd', 'ef']
for i in range(len(l)):
for j in range(len(l[i])):
print(l[i][j], end=' ')
# a b c d e f
```

```l = [['ab', 'cd', 'ef'], ['gh', 'ij', 'kl']]
for i in l:
for j in i:
print(j, end=' ')
# ab cd ef gh ij kl
```

```l = [['ab', 'cd', 'ef'], ['gh', 'ij', 'kl']]
for i in l:
for j in i:
for k in j:
print(k, end=' ')
# a b c d e f g h i j k l
```

```l = [['ab', 'cd', 'ef'], ['gh', 'ij', 'kl']]
for i in range(len(l)):
for j in range(len(l[i])):
print(l[i][j], end=' ')
# ab cd ef gh ij kl
```

```l = [['ab', 'cd', 'ef'], ['gh', 'ij', 'kl']]
for i in range(len(l)):
for j in range(len(l[i])):
for k in range(len(l[i][j])):
print(l[i][j][k], end=' ')
# a b c d e f g h i j k l
```

区切り文字指定

```a = 1
b = 2
c = 3
print(a, b, c)
# 1 2 3
```

```a = 1
b = 2
c = 3
print(a, b, c, sep='\n')
# 1
# 2
# 3
```
カンマ
```a = 1
b = 2
c = 3
print(a, b, c, sep=',')
# 1,2,3
```

```a = 1
b = 2
c = 3
print(a, b, c, sep='')
# 123
```

基本処理

繰り返し

```for i in range(2, 5):
print(i, end=' ')
# 2 3 4
```

```for i in range(5, 2, -1):
print(i, end=' ')
# 5 4 3
```
インクリメント数の指定
```for i in range(2, 10, 2):
print(i, end=' ')
# 2 4 6 8
```
デクリメント数の指定
```for i in range(10, 2, -2):
print(i, end=' ')
# 10 8 6 4
```

条件

if...in
```if 'abc' in input():
print('OK')
else:
print('NG')
```

```print("YES" if a <= x <= a + b else "NO")
```

文字列

• 配列、文字列の逆順

```l = "abc"
print(l[::-1])
# cba
```

リスト処理

• 要素数の取得
count
```l = ['a', 'b', 'b', 'c', 'c', 'c']
print(l.count('c'))
# 3

print(l.count('d'))
# 0
```
• 要素の探索
index
```l = ['a', 'b', 'c', 'd', 'e', 'f']
print(l.index('c', 0, 5))
# 2

l = ['a', 'b', 'c', 'd', 'c', 'c']
print(l.index('c', 0, 5))
# 2
```
• 特定要素の出力 / 削除
pop/remove
```l = ['a', 'b', 'c', 'd', 'e', 'f']
print(l.pop(1))
# b

print(l)
# ['a', 'c', 'd', 'e', 'f']

l.pop()
print(l)
# ['a', 'c', 'd', 'e']

l.remove('d')
print(l)
# ['a', 'c', 'e']
```
• 要素の追加
append/extend/insert
```l = ['a', 'b', 'c']
l.append('d')
print(l)
# ['a', 'b', 'c', 'd']

l.extend(['e', 'f'])
print(l)
# ['a', 'b', 'c', 'd', 'e', 'f']

l.insert(1, 'z')
print(l)
# ['a', 'z', 'b', 'c', 'd', 'e', 'f']

l.append(['g', 'h'])
print(l)
# ['a', 'z', 'b', 'c', 'd', 'e', 'f', ['g', 'h']]

l.insert(1, ['x', 'y'])
print(l)
# ['a', ['x', 'y'], 'z', 'b', 'c', 'd', 'e', 'f', ['g', 'h']]
```
• 要素のユニーク化

```a = [2, 3, 1, 2]

# tuple化
b = set(a)
print(b)
# {1, 2, 3}

# list化
c = list(b)
print(c)
# [1, 2, 3]
```

```l = [2, 3, 1, 2]
print(list(set(l)))
# [1, 2, 3]
```

```a = [[1, 0], [0, 0], [1, 1], [1, 0], [0, 1], [0, 0]]

b = map(tuple, a)
print(b)
# <map object at 0x6ffffd530b8>

c = set(b)
print(c)
# {(0, 1), (1, 0), (0, 0), (1, 1)}

d = map(list, c)
print(d)
# <map object at 0x6ffffd530f0>

e = list(d)
print(e)
# [[0, 1], [1, 0], [0, 0], [1, 1]]
```

```a = [[1, 0], [0, 0], [1, 1], [1, 0], [0, 1], [0, 0]]
print(list(map(list, set(map(tuple, a)))))
# [[0, 1], [1, 0], [0, 0], [1, 1]]
```

ソート

• 1次元リスト

```l = [2, 1, 3]
l.sort()
print(l)
# [1, 2, 3]
```

```l = [2, 1, 3]
l.sort(reverse=True)
print(l)
# [3, 2, 1]
```

```l = [2, 1, 3]
l.reverse()
print(l)
# [3, 1, 2]
```
• 多次元リスト

```l = [[0, 1], [1, 1], [1, 0], [0, 0]]
l.sort(key = lambda l:(l[0], l[1]))
print(l)
# [[0, 0], [0, 1], [1, 0], [1, 1]]
```

```from operator import itemgetter
l = [[0, 1], [1, 1], [1, 0], [0, 0]]
l.sort(key = itemgetter(0, 1))
print(l)
# [[0, 0], [0, 1], [1, 0], [1, 1]]
```

```l = [[0, 1], [1, 1], [1, 0], [0, 0]]
l.sort(key = lambda l:(l[0], l[1]), reverse=True)
print(l)
# [[1, 1], [1, 0], [0, 1], [0, 0]]
```

```from operator import itemgetter
l = [[0, 1], [1, 1], [1, 0], [0, 0]]
l.sort(key = itemgetter(0, 1), reverse=True)
print(l)
# [[1, 1], [1, 0], [0, 1], [0, 0]]
```

```l = [[0, 1], [1, 1], [1, 0], [0, 0]]
l.reverse()
print(l)
# [[0, 0], [1, 0], [1, 1], [0, 1]]
```

リスト内包表記

• 繰り返し

```l = [1*i for i in range(5)]
print(l)
# [0, 1, 2, 3, 4]
```
ネスト
```l = [1*i + 10*j + 100*k for k in range(2) for j in range(3) for i in range(4)]
print(l)
#[0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 100, 101, 102, 103, 110, 111, 112, 113, 120, 121, 122, 123]
```

三項演算子

```print("1" if a == 1 else "other")

print("1") if a == 1 else print("other")

if a == 1:
print("1")
else:
print("other")
```

```print("1" if a == 1 else "2" if a == 2 else "3" if a == 3 else "other")

if a == 1:
print("1")
elif a == 2:
print("2")
elif a == 3:
print("3")
else:
print("other")
```

最大値

```print(max(1, 2))
# 2
```

最小値

```print(min(1, 2))
# 1
```

平方根

```y = x^{1/2}
```

```x = 4
y = x ** (1/2)

print(y)
# 2.0

print(int(y))
# 2
```

応用処理

奇数/偶数判定

• 文字列配列

```1
2

a = int(input())
b = int(input())
print(["Even", "Odd"][a * b % 2])
# Even
```
• Bool演算

```1
2

a = int(input())
b = int(input())
print((a * b % 2) * "Odd" or "Even")
# Even
```
• Bool演算

```1
2

a = int(input())
b = int(input())
print("Odd" if a * b & 1 else "Even")
# Even
```

N値判定

2値判定
```x = int(input())
print(["Even", "Odd"][x % 2])
```
3値判定
```x = int(input())
print(["0", "Positive", "Negative"][x > 0 or -(x < 0)])
```

平方根の整数部と小数部の算出

```y = x^{1/2}　（a：整数部, b：小数部）
```

```x = 5
y = x ** .5

# 平方値
print(y)
# 2.23606797749979

# 整数部
a = int(y)
print(a)
# 2

# 小数部
b = a - int(y)
print(b)
# 0.2360679774997898
```

連立方程式の解の個数

```2x+3y+4z=10　（0≦x<20, 0≦y<30, 0≦z<40）
```
• リスト内包表記 + for文ネスト + countメソッド
```print([2*x + 3*y + 4*z for z in range(40) for y in range(30) for x in range(20)].count(10))
# 5
```
• リスト内包表記 + for文ネスト + if文 + len関数
```_ = 0 # dummy
print(len([ _ for x in range(20) for y in range(30) for z in range(40) if 2*x + 3*y + 4*z == 10 ]))
# 5
```