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?

numpyの勉強記録2

Posted at

連投すみません。
もう1つ自分の学習用メモを。

【Python】()[]の使い分けについて

Exersicesにおいて次のようなNumPyの5x5行列から下記の行列を抽出せよという問題がありました。

import numpy as np
mat = np.arange(1, 26).reshape(5, 5)

array([[12, 13, 14, 15],
       [17, 18, 19, 20],
       [22, 23, 24, 25]])

最初
mat(2:, 1:) と書いてしまってエラーが出たのですが
いま( )と[ ]の使い分けが分からない。

これまたチャッピー先生への質問と回答。

Q:pythonにおいて()と[]の使い分けは何ですか?

A:
Pythonでは()と[]は用途が異なります。

()の用途
関数の呼び出し: 例: mat.reshape(5, 5)
タプルの作成: 例: (2, 1)
[]の用途
インデックス指定やスライス: 例: mat[2:, 1:]
リストや配列の作成: 例: [1, 2, 3]

0
0
0

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?