If Statements and Boolean Logic - Exam 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/28

encourage image

There's no tags or description

Looks like no tags are added yet.

29 Terms

1
New cards

What does == mean?

Compare values for equality; it does not assign.

2
New cards

What syntax begins a conditional block?

if condition: then an indented block.

3
New cards

When does elif execute?

Only if earlier branches were false and its condition is true.

4
New cards

When does else execute?

When no previous if/elif condition was true.

5
New cards

What is True and False?

False: both must be True for AND.

6
New cards

What is True or False?

True: at least one must be True for OR.

7
New cards

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().

8
New cards

Why can np.sin(np.pi) == 0 be False?

Floating-point rounding makes the result extremely close to zero, not exactly zero.

9
New cards

How do you check whether an array has ANY positive element?

(x > 0).any()

10
New cards

How do you check whether ALL array elements are positive?

(x > 0).all()

11
New cards

What does x < 3 check?

Whether x is strictly smaller than 3.

12
New cards

When do two if statements both run?

If each of their separate conditions is True.

13
New cards

How is if/elif different from two independent ifs?

An if/elif chain executes only the first matching branch.

14
New cards

For x=0, what does if x>0: print('P'); else: print('N') output?

N

15
New cards

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.

16
New cards

What does not True evaluate to?

False

17
New cards

What does not (3>2) evaluate to?

False

18
New cards

What is False or False?

False

19
New cards

What is True and True?

True

20
New cards

Write a condition for x between 1 and 5 inclusive (scalar)

1 <= x <= 5

21
New cards

Write a scalar condition for x outside [1,5]

x < 1 or x > 5

22
New cards

Write code to print 'positive' only when x>0

if x > 0:

print('positive')

23
New cards

Write if/else setting y to abs(x), without np.abs

if x >= 0:

y = x

else:

y = -x

24
New cards

Why does an elif branch not run after an earlier if is True?

The chain stops at its first satisfied condition.

25
New cards

Where must the colon appear in if x>0: ?

At the end of the if header, before the indented body.

26
New cards

What does break do when an if condition inside a loop is met?

Immediately exits the loop; execution continues after the loop.

27
New cards

How do you test whether any entry of x exceeds 10?

(x > 10).any()

28
New cards

How do you test whether all entries of x are nonnegative?

(x >= 0).all()

29
New cards

What is the safer numeric test for f(x) near zero?

abs(fx) < tol, for a chosen small tolerance.