d1_ndarray_operation_2.py

プログラム一覧に戻る

import numpy as np


arr1 = np.array([1, 2, 3, 4])  # リストからndarrayを生成し、arr1という名前を付ける
print("arr1 =", arr1)

arr2 = arr1 + 2
print("arr1 + 2 =", arr2)  # [3 4 5 6]

arr2 = arr1 - 2
print("arr1 - 2 =", arr2)  # [-1  0  1  2]

arr2 = arr1 * 2
print("arr1 * 2 =", arr2)  # [2 4 6 8]

arr2 = arr1 / 2
print("arr1 / 2 =", arr2)  # [0.5 1.  1.5 2. ]

arr2 = 2 / arr1
print("2 / arr1 =", arr2)  # [2.  1.  0.66666667 0.5 ]

arr2 = arr1 ** 2
print("arr1 ** 2 =", arr2)  # [ 1  4  9 16]

arr2 = 2 ** arr1
print("2 ** arr1 =", arr2)  # [ 2  4  8 16]