1 2 3 4 5 6 |
<span class="cm-variable">animals</span> = [<span class="cm-string">"tiger"</span>, <span class="cm-string">"dog"</span>, <span class="cm-string">"elephant"</span>] <span class="cm-keyword">for</span> n <span class="cm-keyword">in</span> <span class="cm-variable">animals</span>: <span class="cm-builtin">print</span>(n) </code> in animalsで繰り返す回数を指定(リストの要素の数だけ繰り返す) for 直後の変数nにはリストの文字列が代入される |
辞書の要素の追加、変更、削除
town = {"Aichi": "aichi", "Kanagawa": "Yokohama"}
追加の例
town["Hokkaido"] = "sapporo"
変更の例
town["Aichi"] = "Nagoya"
辞書名["更新したい値のキー"] = 値
辞書名["追加したいキー"] = 値
削除の例
town = {"Aichi": "aichi", "Kanagawa": "Yokohama", "Hokkaido": "sapporo"}
del(town["Aichi"])
del(辞書名["削除したいキー"])
辞書の要素を取り出す
town = {"Aichi": "Nagoya", "Kanagawa": "Yokohama"}
print("愛知の県庁所在地は"+(town["Aichi"])+"です。")
print("神奈川の県庁所在地は"+(town["Kanagawa"]+"です。")
要素の指定は(変数名[“要素名”])で行う
リストの要素を削除
1 2 3 4 5 6 |
<span class="cm-variable">alphabet</span> = [<span class="cm-string">"a"</span>, <span class="cm-string">"b"</span>, <span class="cm-string">"c"</span>, <span class="cm-string">"d"</span>, <span class="cm-string">"e"</span>] <span class="cm-keyword">del</span>(<span class="cm-variable">alphabet</span>[<span class="cm-number">3</span>:]) <span class="cm-keyword">del</span>(<span class="cm-variable">alphabet</span>[<span class="cm-number">0</span></code><code class="cm-s-ipython language-python">])</code><code class="cm-s-ipython language-python"> <span class="cm-builtin">print</span>(<span class="cm-variable">alphabet</span>) <span class="cm-comment"># ["b", "c"]が出力される </span> |
del(リスト[インデックス番号])
例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
In [9]: sample_list = ['a','b','c','d','e'] In [10]: sample_list[0] Out[10]: 'a' In [11]: sample_list[0:] Out[11]: ['a', 'b', 'c', 'd', 'e'] In [12]: sample_list[0:-1] Out[12]: ['a', 'b', 'c', 'd'] In [13]: sample_list[0:4] Out[13]: ['a', 'b', 'c', 'd'] In [19]: sample_list[-3:] Out[19]: ['c', 'd', 'e'] In [22]: sample_list[-3:-1] Out[22]: ['c', 'd'] # リストを反転させるとき In [14]: sample_list[::-1] Out[14]: ['e', 'd', 'c', 'b', 'a'] |
1 |
リスト
1 2 3 4 5 6 7 8 9 |
<span class="cm-variable">alphabet</span> = [<span class="cm-string">"a"</span>, <span class="cm-string">"b"</span>, <span class="cm-string">"c"</span>, <span class="cm-string">"d"</span>, <span class="cm-string">"e"</span>] <span class="cm-variable">alphabet</span>[<span class="cm-number">0</span>] = <span class="cm-string">"A"</span> <span class="cm-variable">alphabet</span>[<span class="cm-number">1</span>:<span class="cm-number">3</span>] = [<span class="cm-string">"B"</span>, <span class="cm-string">"C"</span>] </code><code class="cm-s-ipython language-python"><span class="cm-builtin">print</span></code><code class="cm-s-ipython language-python">(<span class="cm-variable">alphabet</span>) </code><code class="cm-s-ipython language-python"><span class="cm-comment"># ["A", "B", "C", "d", "e"]が出力される</span></code><code class="cm-s-ipython language-python"> <span class="cm-variable">alphabet</span> = <span class="cm-variable">alphabet</span> <span class="cm-operator">+</span> [<span class="cm-string">"f"</span>] <span class="cm-variable">alphabet</span> += [<span class="cm-string">"g"</span>,<span class="cm-string">"h"</span>] <span class="cm-variable">alphabet</span>.<span class="cm-property">append</span>(<span class="cm-string">"i"</span>) <span class="cm-builtin">print</span>(<span class="cm-variable">alphabet</span>) <span class="cm-comment"># ["A", "B", "C", "d", "e", "f", "g", "h", "i"]が出力される</span> |
pass とcontinueの違い ifを抜ける
passだとifを抜ける
continueだとifの中のelifも実行される。
■continueの場合以下のelifは実行される
1 2 3 4 |
if len(ans) != 6: continue(elifは実行される) elif ans.isdecimal() == False: print "a" |
■passの場合以下のelifは実行されない
1 2 3 4 |
if len(ans) != 6: pass (elifは実行されない) elif ans != c[0]: print "a" |