8 Logic Functions Solutions
title: "Logic functions" date: 2026-05-24T13:52:03Z
Logic functions
1import numpy as np
1np.__version__
'1.11.2'
Truth value testing
Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.
1x = np.array([1,2,3])
2print np.all(x)
3
4x = np.array([1,0,3])
5print np.all(x)
True
False
Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero.
1x = np.array([1,0,0])
2print np.any(x)
3
4x = np.array([0,0,0])
5print np.any(x)
True
False
Array contents
Q3. Predict the result of the following code.
1x = np.array([1, 0, np.nan, np.inf])
2#print np.isfinite(x)
Q4. Predict the result of the following code.
1x = np.array([1, 0, np.nan, np.inf])
2#print np.isinf(x)
Q5. Predict the result of the following code.
1x = np.array([1, 0, np.nan, np.inf])
2#print np.isnan(x)
Array type testing
Q6. Predict the result of the following code.
1x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])
2#print np.iscomplex(x)
Q7. Predict the result of the following code.
1x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])
2#print np.isreal(x)
Q8. Predict the result of the following code.
1#print np.isscalar(3)
2#print np.isscalar([3])
3#print np.isscalar(True)
Logical operations
Q9. Predict the result of the following code.
1#print np.logical_and([True, False], [False, False])
2#print np.logical_or([True, False, True], [True, False, False])
3#print np.logical_xor([True, False, True], [True, False, False])
4#print np.logical_not([True, False, 0, 1])
Comparison
Q10. Predict the result of the following code.
1#print np.allclose([3], [2.999999])
2#print np.array_equal([3], [2.999999])
Q11. Write numpy comparison functions such that they return the results as you see.
1x = np.array([4, 5])
2y = np.array([2, 5])
3print np.greater(x, y)
4print np.greater_equal(x, y)
5print np.less(x, y)
6print np.less_equal(x, y)
[ True False]
[ True True]
[False False]
[False True]
Q12. Predict the result of the following code.
1#print np.equal([1, 2], [1, 2.000001])
2#print np.isclose([1, 2], [1, 2.000001])