1/28
Looks like no tags are added yet.
What does == mean?
Compare values for equality; it does not assign.
What syntax begins a conditional block?
if condition: then an indented block.
When does elif execute?
Only if earlier branches were false and its condition is true.
When does else execute?
When no previous if/elif condition was true.
What is True and False?
False: both must be True for AND.
What is True or False?
True: at least one must be True for OR.
What is wrong with if x > 0: when x is a multi-element NumPy array?
The truth value is ambiguous; use a mask or .any()/.all().
Why can np.sin(np.pi) == 0 be False?
Floating-point rounding makes the result extremely close to zero, not exactly zero.
How do you check whether an array has ANY positive element?
(x > 0).any()
How do you check whether ALL array elements are positive?
(x > 0).all()
What does x < 3 check?
Whether x is strictly smaller than 3.
When do two if statements both run?
If each of their separate conditions is True.
How is if/elif different from two independent ifs?
An if/elif chain executes only the first matching branch.
For x=0, what does if x>0: print('P'); else: print('N') output?
N
For x=2, if x<0/elif x<5/else, which branch runs?
The elif branch because x is not negative but is less than 5.
What does not True evaluate to?
False
What does not (3>2) evaluate to?
False
What is False or False?
False
What is True and True?
True
Write a condition for x between 1 and 5 inclusive (scalar)
1 <= x <= 5
Write a scalar condition for x outside [1,5]
x < 1 or x > 5
Write code to print 'positive' only when x>0
if x > 0:
print('positive')
Write if/else setting y to abs(x), without np.abs
if x >= 0:
y = x
else:
y = -x
Why does an elif branch not run after an earlier if is True?
The chain stops at its first satisfied condition.
Where must the colon appear in if x>0: ?
At the end of the if header, before the indented body.
What does break do when an if condition inside a loop is met?
Immediately exits the loop; execution continues after the loop.
How do you test whether any entry of x exceeds 10?
(x > 10).any()
How do you test whether all entries of x are nonnegative?
(x >= 0).all()
What is the safer numeric test for f(x) near zero?
abs(fx) < tol, for a chosen small tolerance.