Step-by-Step Execution:
- import numpy as np
- This imports the NumPy library, which provides efficient array operations.
- numbers = np.array([1, 2, 3])
- This creates a NumPy array:
- This creates a NumPy array:
- new_numbers = numbers + 1
- NumPy allows element-wise operations, so adding 1 increases each element by 1:
- NumPy allows element-wise operations, so adding 1 increases each element by 1:
- print(new_numbers.tolist())
- new_numbers.tolist() converts the NumPy array back into a Python list:
- Finally, it prints the result.
- new_numbers.tolist() converts the NumPy array back into a Python list:
Final Output:
Why use NumPy?
- Faster operations than standard Python lists.
- Supports vectorized computations without loops.
- Memory efficient for large datasets.
0 Comments:
Post a Comment