fruit = "banana"
color = "yellow"
print("{0}は{1}です".format("banana","yellow"))
fruit = "banana"
color = "yellow"
print("{0}は{1}です".format("banana","yellow"))
1 2 3 4 5 |
<span class="cm-variable">city</span> = <span class="cm-string">"Tokyo"</span> <span class="cm-builtin">print</span>(<span class="cm-variable">city</span>.<span class="cm-property">count</span>(<span class="cm-string">"o"</span>)) <span class="cm-comment"># 2と出力されます。 リストの場合 (["山形","山梨","山口","山形","山梨","山口"],"山")</span> |
というのはできない、リストでは部分一致ではなく完全一致
1 2 3 4 |
<span class="cm-variable">alphabet</span> = [<span class="cm-string">"a"</span>,<span class="cm-string">"d"</span>,<span class="cm-string">"e"</span>,<span class="cm-string">"c"</span>,<span class="cm-string">"b"</span>] <span class="cm-builtin">sorted</span>(<span class="cm-variable">alphabet</span>) <span class="cm-builtin">print</span>(<span class="cm-variable">alphabet</span>) |
adecbの順番で出力
1 2 3 4 5 |
<span class="cm-variable">alphabet</span> = [<span class="cm-string">"a"</span>,<span class="cm-string">"d"</span>,<span class="cm-string">"e"</span>,<span class="cm-string">"c"</span>,<span class="cm-string">"b"</span>] <span class="cm-variable">alphabet</span>.<span class="cm-property">sort</span>() <span class="cm-builtin">print</span>(<span class="cm-variable">alphabet</span></code><code class="cm-s-ipython language-python">)</code><code class="cm-s-ipython language-python"> abcdeの順番で出力 |
1 2 3 4 |
<span class="cm-variable">ruits</span> = {<span class="cm-string">"strawberry"</span>: <span class="cm-string">"red"</span>, <span class="cm-string">"peach"</span>: <span class="cm-string">"pink"</span>, <span class="cm-string">"banana"</span>: <span class="cm-string">"yellow"</span>} <span class="cm-keyword">for</span> <span class="cm-variable">fruit</span>, <span class="cm-variable">color</span> <span class="cm-keyword">in</span> <span class="cm-variable">fruits</span>.<span class="cm-property">items</span>(): <span class="cm-builtin">print</span>(<span class="cm-variable">fruit</span><span class="cm-operator">+</span><span class="cm-string">" is "</span><span class="cm-operator">+</span><span class="cm-variable">color</span>) |
出力結果
1 2 3 |
<span class="cm-variable">strawberry</span> <span class="cm-keyword">is</span> <span class="cm-variable">red</span> <span class="cm-variable">peach</span> <span class="cm-keyword">is</span> <span class="cm-variable">pink</span> <span class="cm-variable">banana</span> <span class="cm-keyword">is</span> <span class="cm-variable">yellow</span> |
fruits = [["strawberry", "red"],
["peach", "pink"],
["banana", "yellow"]]
for a,b in fruits:
print(a + " is " + b)
1 2 3 4 |
<span class="cm-builtin">list</span> = [<span class="cm-string">"a"</span>, <span class="cm-string">"b"</span>] <span class="cm-keyword">for</span> <span class="cm-variable">index</span>, <span class="cm-variable">value</span> <span class="cm-keyword">in</span> <span class="cm-builtin">enumerate</span>(<span class="cm-builtin">list</span>): <span class="cm-builtin">print</span>(<span class="cm-variable">index</span>, <span class="cm-variable">value</span>) |
出力結果
1 2 |
<span class="cm-number">0</span> <span class="cm-variable">a</span> <span class="cm-number">1</span> <span class="cm-variable">b</span> |