1
0

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 3 years have passed since last update.

Pythonで使えるメモ

Last updated at Posted at 2020-08-17

#Python 早引きメモ
プログラミングするときのコピペメモ

入力

1行の中に複数の値が入っているのを、分けて変数に入れる
1 2 -> a=1, b=2

a,b = (int(x) for x in input().split())

多重リスト

table = [[0] * W for i in range(H)]

複数行の場合
最初に、行数を指定

num = int(input())
a=list(range(num))
b=list(a)

for i in range(num):
    a[i],b[i]=(int(x) for x in input().split())

出力

num = "Yeah"
print("Hello",num) # HelloYeah
print(num,end='')  # Yeah (改行しない)

if文

インデントに注意!

if a == 0:
  ~~
elif a < 0:
  ~~
else:
  ~~

for文

値で指定 i =0,1,2,3,4

for i in range(5):

リストで指定

list1 = ["Suzuki","Noguchi","Takayama"]
for i in list1:

while文

while n<1000:

リストのソート

list1.sort() #値の小さい順 or 文字コード順にソート
list1.reverse() #値の大きい順 or 文字コードの逆順にソート

リストの最大値・最小値

最大値 max(リスト) 最小値 min(リスト)

print(max(list1),min(list2))
#最大値のインデックス
list3.index(max(list3))
#最大値のインデックス(複数あるとき=リスト内包表記)
[i for i, j in enumerate(list4) if j == max(list4)]

時間計算

時間だけでも、日付をふくめたdatetime型で扱うと楽

import datetime

h,m = (int(x) for x in input().split())
time[i] = datetime(2020, 1, 1, h, m, 00)       # datetime(年,月,日,時,分,秒)で入力

if time[i] < datetime(2020, 1, 1, 9, 00 ,00):  # 比較もできる
  hometime = time[i] - timedelta(minutes=30)   # 差し引きは、timedeltaで
  print(hometime.strftime("%H:%M"))            # 出力は、.strftime("%H:%M:%S")

文字列処理

文字列完全一致 ==・不一致 !=

print('abc' == 'abc') # True
print('abc' == 'ABC') # False
print('abc' != 'ABC') # True

文字列一部一致

print('xyz' in 'aaa-xyz-zzz') # True

文字列前方一致・後方一致

s = 'aaa-xyz-zzz'
print(s.startswith('aaa')) # True
print(s.startswith('zzz')) # False
print(s.endswith('zzz')) # True
print(s.endswith('xyz')) # False
print(s.endswith(('xxx', 'yyy', 'zzz')))# True  タプルでor検索

大文字小文字変換

s = 'Abcあいうえお'
print(s.lower()) # abcあいうえお
print(s.upper()) # ABCあいうえお
1
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?