Projects/array_comparisons.py

19 lines
899 B
Python

import numpy as np
# Creating an array of integers from 1 to 10 inclusive
array = np.arange(1, 21)
# Retrieving elements greater than or equal to 5 AND less than 9
print(array[(array >= 5) & (array < 9)])
# Retrieving elements less than or equal to 4 AND not equal to 2
print(array[(array != 2) & (array <= 4)])
# Retrieving elements less than 3 OR equal to 8
print(array[(array < 3) | (array == 8)])
# Retrieving elements between 2 inclusive AND 5 inclusive OR equal to 9
print(array[(array >= 2) & (array <= 5) | (array == 9)])
print(array[(array % 2 == 0)])
'''
If both conditions are true, | returns True, otherwise returns False.
If either condition is true, & returns True, otherwise returns False.
The & operator is used for element-wise logical AND, while | is used for element-wise logical OR.
The parentheses around conditions are necessary to ensure correct precedence of operations.
'''