文字列、ループ
「独学プログラマー」
文字列
'+'.join('abc') #a.b.c #順番に注意(英語文と同じ順番) ' aaa '.strip() #aaa 'aaa'.replace('a', 'b') #bbb 'abcde'.index('d') #3 try: 'abcde'.index('z') #存在しないとValue Errorとなるため、例外処理を行う except: print('Not found.') 'A' in 'A is large a.' #True 'B' not in 'A is large a.' #True 'I don\'t care.' 'Line1\nLine2' ['a', 'b', 'c', 'd', 'e'][0:2] #['a', 'b'] #['a', 'b', 'c', 'd', 'e'][:2]と同じ #終了インデックスは含まないことに注意 ['a', 'b', 'c', 'd', 'e'][2:4] #['c', 'd'] ['a', 'b', 'c', 'd', 'e'][:] #コピー
ループ
for s in 'Abc': print s #A #b #c abc = ['a', 'b', 'c'] i = 0 for s in abc: n = abc[i] n = n.upper() abc[i] = n i += 1 ##Pythonにはi++はない print(abc) #['A', 'B', 'C'] for i in range(0, 10): print(i) #0 - 9 x = 0 while x < 10: print(x) x += 1 # 0 - 9 break #ループを終了 continue #ループを続けるが、実行中の処理(次の行〜)はとばす # 無限ループを止めるのはcontrol + c # 外のループが1回まわる毎、内のループが全てまわる
入れ子のループは今まで頭の中でこんがらがっていたのが、説明がすごくしっくりきた。