1
2

More than 3 years have passed since last update.

[競技プログラミング] Python チートシート

Last updated at Posted at 2020-05-06

前書き

普段はC++で問題を解いているが,オーバーフローや気分転換時にPythonを使いたい場面があった為,
[競技プログラミング] C++ チートシート
のPythonバージョンとして記録

前知識

AtCoder に登録したら次にやること ~ これだけ解けば十分闘える!過去問精選 10 問 ~

基本知識

1 秒間で処理できる for 文ループの回数は、

10^8=100,000,000 回程度

計算量オーダーの求め方を総整理! 〜 どこから log が出て来るか 〜

テンプレート

program.py
import itertools
import collections
import math

def main():


if __name__ == "__main__":
    main()

基本

復習

program.py
x = 10

#割り算
print(x/3)

# 除算
print(x //3)

# 余り
print(x % 3)

#べき乗
print(3 ** 2)


数値操作

program.py

文字列操作

program.py

print("hello"+"world")
print("hello" * 3)

# result

helloworld
hellohellohello

配列

program.py

セット

重複と順番を自動に整理してくれる(重複なし)

program.py

関数

program.py

入出力

入力

program.py
"""
入力値 >>> ABC
"""
str = input() # >>> str = "ABC"
strs = list(input()) # >>> strs = ["A","B","C"]

"""
入力値 >>> 1
"""
int = int(input()) # >>> int = 1

"""
入力値 >>> 1 2
"""
x,y = map(int,input().split()) # >>> x = 1, y = 2

"""
入力値 >>> 1 2 3 4 5 ... n
"""
lists = input().split() # >>> lists = ['1','2','3',...,'n']
lists = list(map(int,input().split())) # >>> lists = [1,2,3,4,5,...,n]

"""
入力値 >>> FTFTTFTTTFTTTTF 
"""
lists = input().split('T') 
# >>> list = ['F', 'F', '', 'F', '', '', 'F', '', '', '', 'F']

"""
入力値 >>> 3
           hoge
           foo
           bar
"""
num = int(input())
lists = [input() for i in range(n)]
# >>> num = 3
# >>> lists = ['hoge','foo','bar']

"""
入力値 >>> 1 2 2 3 1
           4 5 3 4 1 2 3 4
           2 3 5 6 0 2
"""
while True:
    try:
        lists = list(map(int,input().split())) 
        # ループ
        # 1周目 list = [1,2,2,3,1]
        # 2周目 list = [4,5,3,4,1,2,3,4]
        # 3周目 list = [2,3,5,6,0,2]
        # 4周目 エラー(out of range) よってexceptに行く。
    except:
        break;

# OR

lists = []
while True:
    try:
        lists.append(list(map(int,input().split())))
    except:
        break;
#lists = [[1, 2, 2, 3, 1], [4, 5, 3, 4, 1, 2, 3, 4], [2, 3, 5, 6, 0, 2]]


"""
入力値 >>> A B
"""

"""
入力値 >>> d1 d2 ... dN
&
入力値 >>> d1 
          d2 
          ...
          dN
"""


"""
入力値 >>> 3 4
           99 99 99
           99 0 99
           99 99 99
           99 99 99
"""


"""
入力値 >>> 10 12
          W........WW.
          .WWW.....WWW
          ....WW...WW.
          .........WW.
          .........W..
          ..W......W..
          .W.W.....WW.
          W.W.W.....W.
          .W.W......W.
          ..W.......W.
"""


"""
入力値 >>> d1 d2 .. d? (入力数が不明)
"""


出力

program.py
"""
桁数指定
"""


"""
セット全出力
"""
1
2
3

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
1
2