# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
import numpy as np
import sys
print("โ
1. Vectorized Operations")
arr = np.array([1, 2, 3, 4])
print("Original:", arr)
print("Add 5:", arr + 5)
print("Multiply by 2:", arr * 2)
print("Square root:", np.sqrt(arr))
print("\nโ 2. Fixed Size of NumPy Arrays")
arr = np.array([1, 2, 3])
print("Original:", arr)
# arr.append(4) # This would raise an error
arr = np.append(arr, 4)
print("After append (via np.append):", arr)
print("\n๐ 3. Uniform Dtype")
arr = np.array([1, 2, 3.5])
print("Array with int and float:", arr)
print("Dtype:", arr.dtype)
arr = np.array([1, "two", 3])
print("Array with mixed types:", arr)
print("Dtype:", arr.dtype)
print("\n๐ 4. Memory Comparison: List vs NumPy Array")
list_2d = [[1, 2], [3, 4], [5, 6]]
arr_2d = np.array(list_2d)
list_size = sum(sys.getsizeof(row) for row in list_2d)
numpy_size = arr_2d.nbytes
print("List of lists size:", list_size)
print("NumPy array size:", numpy_size)
print("\n๐ฏ 5. Boolean Indexing")
arr = np.array([10, 20, 30, 40, 50])
condition = arr > 25
print("Original array:", arr)
print("Condition (arr > 25):", condition)
print("Filtered result:", arr[condition])
print("\n๐งช 6. Exercises")
# a) Create NumPy array from 1 to 10 and multiply by 3
nums = np.arange(1, 11)
print("Numbers 1โ10:", nums)
print("Multiplied by 3:", nums * 3)
# b) Create 2D array and apply boolean indexing
matrix = np.array([[5, 15, 25], [10, 20, 30]])
print("2D Array:\n", matrix)
print("Elements > 15:", matrix[matrix > 15])
# c) Mix int and float and check dtype
mixed_arr = np.array([2, 7.1, 5])
print("Mixed array:", mixed_arr)
print("Dtype:", mixed_arr.dtype)
# d) Compare memory for a large list vs array
large_list = list(range(1000))
large_array = np.array(large_list)
print("Memory used by list:", sys.getsizeof(large_list) + sum(sys.getsizeof(x) for x in large_list))
print("Memory used by array:", large_array.nbytes)
# e) Append to NumPy array using np.append()
arr = np.array([1, 2, 3])
arr = np.append(arr, [4, 5])
print("Appended array:", arr)