tag:crieit.net,2005:https://crieit.net/magazines/aocory/%E8%84%B3%E7%AD%8BPython/feed
脳筋Pythonの投稿 - Crieit
Crieitで連載「脳筋Python」の最近の投稿
2019-02-27T09:33:21+09:00
https://crieit.net/magazines/aocory/%E8%84%B3%E7%AD%8BPython/feed
tag:crieit.net,2005:PublicArticle/14838
2019-02-27T09:33:21+09:00
2019-02-27T09:33:21+09:00
https://crieit.net/posts/Python-while
Pythonのwhile文を使い方
<h2 id="Pythonの比較演算子まとめ"><a href="#Python%E3%81%AE%E6%AF%94%E8%BC%83%E6%BC%94%E7%AE%97%E5%AD%90%E3%81%BE%E3%81%A8%E3%82%81">Pythonの比較演算子まとめ</a></h2>
<p>Pythonのループ処理には「for文」と「while文」の2つの種類があります。今回はwhile文についてご紹介します。<br />
while文は主に、「無限ループ」や「ある条件の間繰り返す」といった処理によく使われます。</p>
<h2 id="Pythonのwhile文の書き方"><a href="#Python%E3%81%AEwhile%E6%96%87%E3%81%AE%E6%9B%B8%E3%81%8D%E6%96%B9">Pythonのwhile文の書き方</a></h2>
<p>Pythonでwhile文は</p>
<pre><code>while 条件式:
繰り返したい処理
</code></pre>
<p>と書きます。<br />
実際はループを抜けたら処理を入れ込むことが多く以下の様に書きます。</p>
<pre><code>while 条件式:
処理A
処理B
</code></pre>
<p>while文はif文と合わせるので</p>
<pre><code>while 条件式:
処理A
if 条件
break
処理B
</code></pre>
<h2 id="実際のコード"><a href="#%E5%AE%9F%E9%9A%9B%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">実際のコード</a></h2>
<pre><code>index = 1
while index <= 5:
print(index)
index += 1
print("終了")
index = 1
while index <= 5:
print(index)
index += 1
break
print("Break終了")
index = 1
while index <= 5:
print(index)
index += 1
if index ==4:
break
print("Break終了")
</code></pre>
<h2 id="出力結果"><a href="#%E5%87%BA%E5%8A%9B%E7%B5%90%E6%9E%9C">出力結果</a></h2>
<pre><code>1
2
3
4
5
終了
1
Break終了
1
2
3
Break終了
</code></pre>
<p>これで簡単な条件を無事扱うことが出来そうです。</p>
<h2 id="動くサンプル"><a href="#%E5%8B%95%E3%81%8F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB">動くサンプル</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://colab.research.google.com/drive/191wx5C3_48NBPY62dSrhYtUMertqF2sY">Python3</a></p>
aocory
tag:crieit.net,2005:PublicArticle/14836
2019-02-25T10:14:49+09:00
2019-02-25T10:14:49+09:00
https://crieit.net/posts/Python-5c73418999ea0
Pythonの比較演算子まとめ
<h2 id="Pythonの比較演算子まとめ"><a href="#Python%E3%81%AE%E6%AF%94%E8%BC%83%E6%BC%94%E7%AE%97%E5%AD%90%E3%81%BE%E3%81%A8%E3%82%81">Pythonの比較演算子まとめ</a></h2>
<p>Pythonのif文を使いたいので比較演算子について勉強したのでまとめておきます。</p>
<div class="table-responsive"><table>
<thead>
<tr>
<th></th>
<th>演算子</th>
<th>結果</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>x < y</td>
<td>xがyより小さければTrue</td>
</tr>
<tr>
<td></td>
<td>x</td>
<td></td>
</tr>
<tr>
<td></td>
<td>x > y</td>
<td>xがyより大きければTrue</td>
</tr>
<tr>
<td></td>
<td>x >= y</td>
<td>xがyより大きいか等しければTrue</td>
</tr>
<tr>
<td></td>
<td>x == y</td>
<td>xとyの値が等しければTrue</td>
</tr>
<tr>
<td></td>
<td>x != y</td>
<td>xとyの値が等しくなければTrue</td>
</tr>
<tr>
<td></td>
<td>x is y</td>
<td>xとyが同じオブジェクトであればTrue</td>
</tr>
<tr>
<td></td>
<td>x is not y</td>
<td>xとyが同じオブジェクトでなければTrue</td>
</tr>
<tr>
<td></td>
<td>x in y</td>
<td>xがyに含まれていればTrue</td>
</tr>
<tr>
<td></td>
<td>x not in y</td>
<td>xがyに含まれていなければTrue</td>
</tr>
</tbody>
</table></div>
<h2 id="実際のコード"><a href="#%E5%AE%9F%E9%9A%9B%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">実際のコード</a></h2>
<pre><code>def sample(num):
if num > 100:
print('100 < num')
elif num > 60:
print('60 < num <= 100')
elif num > 30:
print('30 < num <= 60')
elif num > 0:
print('0 < num <= 30')
elif num == 0:
print('num == 0')
else:
print('num < 0')
sample(1000)
sample(70)
sample(60)
sample(0)
sample(-100)
</code></pre>
<h2 id="出力結果"><a href="#%E5%87%BA%E5%8A%9B%E7%B5%90%E6%9E%9C">出力結果</a></h2>
<pre><code>100 < num
60 < num <= 100
30 < num <= 60
num == 0
num < 0
</code></pre>
<p>これで簡単な条件を無事扱うことが出来そうです。</p>
<h2 id="動くサンプル"><a href="#%E5%8B%95%E3%81%8F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB">動くサンプル</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://colab.research.google.com/drive/191wx5C3_48NBPY62dSrhYtUMertqF2sY">Python3</a></p>
aocory
tag:crieit.net,2005:PublicArticle/14835
2019-02-25T09:25:48+09:00
2019-02-25T09:25:48+09:00
https://crieit.net/posts/Python-if
Pythonのif文の基本
<h2 id="Pythonのif文の基本"><a href="#Python%E3%81%AEif%E6%96%87%E3%81%AE%E5%9F%BA%E6%9C%AC">Pythonのif文の基本</a></h2>
<p>Pythonで制御文を書きたいのでif文について勉強したのでまとめておきます。</p>
<h2 id="Pythonのif文の基本"><a href="#Python%E3%81%AEif%E6%96%87%E3%81%AE%E5%9F%BA%E6%9C%AC">Pythonのif文の基本</a></h2>
<p>Pythonのif文の基本は以下の様なコードです。</p>
<pre><code>if 条件式1:
`条件式1がTrueのときに行う処理`
elif 条件式2:
`条件式1がFalseで条件式2がTrueのときに行う処理`
elif 条件式3:
`条件式1, 2がFalseで条件式3がTrueのときに行う処理`
...
else:
`すべての条件式がFalseのときに行う処理`
</code></pre>
<p>となります。<br />
他の言語との違いは<br />
if()ではなくif そのまま条件式<br />
else ifではなくてelif:という記述になります。</p>
<h2 id="実際のコード"><a href="#%E5%AE%9F%E9%9A%9B%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">実際のコード</a></h2>
<pre><code class="Python">num = 2
if num == 1:
print('numの値は1です')
elif num == 2:
print('numの値は2です')
elif num == 3:
print('numの値は3です')
else:
print('該当する値はありません')
color = 'red'
if color == "blue":
print('colorの値はblueです')
elif color == "black":
print('colorの値はblackです')
elif color == "red":
print('colorの値はredです')
else:
print('該当する値はありません')
</code></pre>
<h2 id="出力結果"><a href="#%E5%87%BA%E5%8A%9B%E7%B5%90%E6%9E%9C">出力結果</a></h2>
<pre><code>2
colorの値はredです
</code></pre>
<h2 id="動くサンプル"><a href="#%E5%8B%95%E3%81%8F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB">動くサンプル</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://colab.research.google.com/drive/191wx5C3_48NBPY62dSrhYtUMertqF2sY">Python3</a></p>
aocory
tag:crieit.net,2005:PublicArticle/14830
2019-02-22T08:50:24+09:00
2019-02-22T08:50:24+09:00
https://crieit.net/posts/Python-enumerate-for
Python enumerateを使ってfor文にリストを追加する
<h2 id="Python enumerateを使ってfor文にインデックスを追加する"><a href="#Python%E3%80%80enumerate%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6for%E6%96%87%E3%81%AB%E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B">Python enumerateを使ってfor文にインデックスを追加する</a></h2>
<p>forで指定回数処理を行うとき、1 blue,2 red...のようにインデックスをつけたくなるときがあります<br />
。そういうときに活躍するのが「enumerate()」です。</p>
<h2 id="Python for文にインデックスを追加する方法"><a href="#Python+for%E6%96%87%E3%81%AB%E3%82%A4%E3%83%B3%E3%83%87%E3%83%83%E3%82%AF%E3%82%B9%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95">Python for文にインデックスを追加する方法</a></h2>
<p>enumerate()関数の第二引数には開始値を指定できます。0始まりではなく任意の整数値から始めることが可能です。</p>
<h2 id="実際のコード"><a href="#%E5%AE%9F%E9%9A%9B%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">実際のコード</a></h2>
<pre><code class="Python">colors = ['blue', 'black', 'red']
for name in colors:
print(name)
for i, name in enumerate(colors):
print(i, name)
for i, name in enumerate(colors,8):
print(i, name)
</code></pre>
<h2 id="出力結果"><a href="#%E5%87%BA%E5%8A%9B%E7%B5%90%E6%9E%9C">出力結果</a></h2>
<pre><code>blue
black
red
0 blue
1 black
2 red
8 blue
9 black
10 red
</code></pre>
<p>これで簡単な配列を無事扱うことが出来そうです。</p>
<h2 id="動くサンプル"><a href="#%E5%8B%95%E3%81%8F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB">動くサンプル</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://colab.research.google.com/drive/191wx5C3_48NBPY62dSrhYtUMertqF2sY">Python3</a></p>
aocory
tag:crieit.net,2005:PublicArticle/14827
2019-02-20T10:29:55+09:00
2019-02-20T10:29:55+09:00
https://crieit.net/posts/Python-for-5c6cad938b0e8
Python for処理を指定回数行う
<h2 id="Pythonfor処理を指定回数行う"><a href="#Pythonfor%E5%87%A6%E7%90%86%E3%82%92%E6%8C%87%E5%AE%9A%E5%9B%9E%E6%95%B0%E8%A1%8C%E3%81%86">Pythonfor処理を指定回数行う</a></h2>
<p>スクレイピングで複数の要素からなる配列を取得し、それを表示させるということが多々あります。<br />
そのあとに少し加工したい!という思いに応えてくれるのが「range()」です。</p>
<h2 id="Python for処理を指定回数行う方法"><a href="#Python+for%E5%87%A6%E7%90%86%E3%82%92%E6%8C%87%E5%AE%9A%E5%9B%9E%E6%95%B0%E8%A1%8C%E3%81%86%E6%96%B9%E6%B3%95">Python for処理を指定回数行う方法</a></h2>
<p>Pythonで「range()」のサンプルコードは以下のようになります。<br />
注意点は、URLの生成などで使う場合「range()」は数字で渡されるため文字列に変換しなければなりません。</p>
<h2 id="実際のコード"><a href="#%E5%AE%9F%E9%9A%9B%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89">実際のコード</a></h2>
<pre><code class="Python">for i in range(3):
print(i)
#URLを作るサンプル
url="https://www.corylog.com/python/python00"
for i in range(5):
print(url + str(i))
</code></pre>
<h2 id="出力結果"><a href="#%E5%87%BA%E5%8A%9B%E7%B5%90%E6%9E%9C">出力結果</a></h2>
<pre><code>0
1
2
[https://www.corylog.com/python/python000](https://www.corylog.com/python/python000)
[https://www.corylog.com/python/python001](https://www.corylog.com/python/python001)
[https://www.corylog.com/python/python002](https://www.corylog.com/python/python002)
[https://www.corylog.com/python/python003](https://www.corylog.com/python/python003)
[https://www.corylog.com/python/python004](https://www.corylog.com/python/python004)
</code></pre>
<p>これで簡単な配列を無事扱うことが出来そうです。</p>
<h2 id="動くサンプル"><a href="#%E5%8B%95%E3%81%8F%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB">動くサンプル</a></h2>
<p><a target="_blank" rel="nofollow noopener" href="https://colab.research.google.com/drive/191wx5C3_48NBPY62dSrhYtUMertqF2sY">Python3</a></p>
aocory