17
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

競プロをPythonでやるときのメモ

17
Last updated at Posted at 2021-04-18

はじめに

AtcoderをPythonで戦って行く際、忘れまくるのでメモりまくります

標準入力

ただ受け取るだけ

入力admin
s = input()
print(s)

admin [str]

入力123
s = int(input())
print(s)

123 [int]文字列はエラーになる

リストにする

入力admin
s = list(input())
print(s)

['a','d','m','i','n'] [list]

mapを使う(一度に複数の変数を受け取る)

入力3 4 5
a, b, c = map(int, input().split())
print(a,b,c)

3 4 5 [int]

改行されている値の取得

入力
1
2
3
4

# if 行数がわかる場合
a = [int(input()) for i in range(4)]

# if 行数が不明の場合
import sys

a = []
for i in sys.stdin:
    a.append(int(i))

[1, 2, 3, 4]

標準出力

複数の変数を出力

入力[1, 2, 3, 4]
a = [1,2,3,4]
count = 1
print(a)
for i in a:
    print(count, i)
    print("{0}, {1}".format(count, i))
    count += 1

[1, 2, 3, 4]
(1, 1)
1, 1
(2, 2)
2, 2
(3, 3)
3, 3
(4, 4)
4, 4

文字列操作

逆順に出力

入力123
a = input()
print(a)
print(a[::-1])

123 [str]
321 [str]

繰り返し処理

引数を複数に

a = [1,2,3]
b = ["a", "b", "c"]
for (i,j) in zip(a,b):
    print(i,j)

1 a [int str]
2 b [int str]
3 c [int str]

引数を複数に その2

a = [11,12,13,14,15]
b = ["a", "b", "c", "d", "e"]


for (i,j) in zip(range(len(a)),b):
    print(a[i], b[i])

11 a [int str]
12 b [int str]
13 c [int str]
14 d [int str]
15 e [int str]

17
23
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?