0
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?

勉強日誌、データ型、for, while文

Posted at

UdemyのPython for Data Science and Machine Learning Bootcampについて
セクション4を受講、今日はデータの型、for, while文の書き方について。

データ型は配列がリスト、タプル、辞書、セットの4つある事を学んだ。
それぞれ特徴は
リスト:Cでいう配列。複数のデータを格納する
タプル:リストと同様に複数のデータを格納するが書き換え不可
辞書:pythonオリジナルの様で詳しいイメージが湧きにくかったが、データにキーワードを関連付けられる??
セット:重複を許さない配列

タプルと辞書についてはあれば便利なんだろうなとは思うけど、ぜひ使いたいというシーンがいまのところは想像できない。経験を積んで使いこなせるようになっていきたい。

for文は記述方法が直感的にピンとこない。
例題で書かれたコードが

x = [1,2,3,4]

out = []
for item in x:
out.append(item**2)
print(out)

実行結果は
[1, 4, 9, 16]

なのですが、xの値を2乗しようとしてitemを2乗しているのがなぜだろう。
しかもitemって変数宣言してなくて突然現れたのも違和感。
これってpython界では一般的??それとも特殊な記述例??

これをCで記述しようとすると
int x[] = {1, 2, 3, 4};
int out[4];
int i;
for (i = 0; i < 4; i++) {
out[i] = x[i] * x[i];
}
なので
下のように書く方が違和感なく入ってくる。

x = [1, 2, 3, 4]
out = []
for i in range(len(x)):
out.append(x[i] ** 2)

もちろんどっちじゃないとダメってことはないんだろうけど
なるべく一般的な見方に慣れていきたい。

あとでこの日誌を見返したときは講義の記述に慣れていて
あぁ、この時はこんなこと思ってたんだなあってなるんだろうか。

0
0
1

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
0
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?