CSCI 1301: Chapter 4 and 5

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

1/76

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:57 AM on 10/8/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

77 Terms

1
New cards

Branch

A sequence of statements that runs only when its condition is True.

2
New cards

Condition (Boolean expression)

An expression evaluating to True or False; it controls if and while statements.

3
New cards

Relational operators

== != <

4
New cards

Logical operators

and, or, not — combine or invert Booleans. Must be lowercase in Python.

5
New cards

Boolean variable (flag)

A variable holding True/False that tracks a state, e.g. found = False.

6
New cards

Nested branch

An if/else inside another branch's body. Indentation decides what belongs where.

7
New cards

Multi-branch (if-elif-else)

A chain testing conditions in order; at most ONE branch runs.

8
New cards

Dangling else

An else with no braces — it pairs with the NEAREST unmatched if above it.

9
New cards

Short-circuit evaluation

and stops at the first False; or stops at the first True. Later operands never evaluate.

10
New cards

Chained comparison

0 <= x < 100 means (0 <= x) and (x < 100), evaluated left to right.

11
New cards

Conditional expression (ternary)

value_if_true if condition else value_if_false — e.g. y = 0 if x < 100 else x.

12
New cards

Truthiness

Values usable directly as conditions: 0, '', [], None are falsy; almost everything else is truthy.

13
New cards

if statement syntax

if condition: then an indented block. The colon is required.

14
New cards

First-true-branch rule

In if-elif-else, conditions test top-down; the FIRST True branch runs and the rest of the chain is skipped.

15
New cards

else pairing rule

else always pairs with the nearest if above it that doesn't already have an else.

16
New cards

Separate ifs are independent

Two separate if statements can BOTH run. An elif chain runs at most one branch.

17
New cards

No-true-branch rule

If no condition is True and there is no else, nothing runs — this is legal, not an error.

18
New cards

Operator precedence

Parentheses → not → and → or. Comparisons bind tighter than not.

19
New cards

Ternary best use

Simple assignments: y = 0 if x < 100 else x.

20
New cards

Indentation = structure

Python groups statements into blocks purely by indentation. 4 spaces conventional; never mix tabs and spaces.

21
New cards

== vs =

== compares; = assigns. Writing if x = 5: is a SyntaxError.

22
New cards

not a == b trap

Parses as not (a == b). Write a != b or add parentheses.

23
New cards

Never == on floats

0.1 + 0.2 == 0.3 is False — floating-point imprecision. Compare with a tolerance instead.

24
New cards

String comparison

Character by character using Unicode values; case-sensitive. 'Z' (90) < 'a' (97).

25
New cards

True == 1

In Python bool is a subclass of int: True == 1 and False == 0.

26
New cards

and / or return operands

They return the deciding operand, not True/False: 0 or 5 gives 5; 5 and 0 gives 0.

27
New cards

Short-circuit prevents crashes

x = 0 → (x != 0) and (10/x > 1) is False with NO ZeroDivisionError.

28
New cards

is vs ==

== compares values; is compares identity (same object in memory). Use == for values.

29
New cards

in on a dict

Checks KEYS only, not values.

30
New cards

De Morgan's laws

not (a and b) == (not a) or (not b); not (a or b) == (not a) and (not b).

31
New cards

in on a string

Substring test: 'abc' in '123abcd' is True.

32
New cards

Loop

Repeats a body of statements; each pass is one iteration.

33
New cards

while loop

Repeats while its condition is True. Condition is checked BEFORE each iteration.

34
New cards

for loop

Iterates over a sequence or container, visiting each element once.

35
New cards

Loop variable

The variable updated each pass (while) or assigned each element (for).

36
New cards

Infinite loop

The condition never becomes False — e.g. the loop variable is never updated.

37
New cards

Sentinel value

A special input value like -1 or 'quit' that ends an input loop; it is not part of the data.

38
New cards

break

Exits the loop immediately.

39
New cards

continue

Skips the rest of the body and jumps to the next iteration.

40
New cards

Nested loop

A loop inside another loop's body. The inner loop runs fully for EACH outer iteration.

41
New cards

Off-by-one error

Loop runs one time too many or too few — usually a < vs <= or range() boundary mistake.

42
New cards

Accumulator

A variable building a result across iterations, e.g. total += n.

43
New cards

while is pre-test

Condition checked before each pass — zero iterations are possible if it starts False.

44
New cards

range(stop)

0 up to stop-1. range(3) gives 0, 1, 2 — that is 3 iterations.

45
New cards

range(start, stop)

start up to stop-1. range(2, 7) gives 2, 3, 4, 5, 6.

46
New cards

range(start, stop, step)

range(2, 8, 3) gives 2, 5. Negative step counts down: range(7, 0, -2) gives 7, 5, 3, 1.

47
New cards

break scope

break exits only the INNERMOST loop it sits in.

48
New cards

Loop variable after the loop

Keeps its last value once the loop ends.

49
New cards

for over a dict

Iterates over KEYS. Use d[key] to get values.

50
New cards

for over a string

Assigns each CHARACTER in order.

51
New cards

Loop else

for/while can have an else clause — it runs only if the loop ended WITHOUT break.

52
New cards

while vs for

while: iteration count unknown beforehand (input-driven). for: count known or iterating a container.

53
New cards

Reassigning the for-loop variable

Does not change iteration: for n in range(3): n = 99 still visits 0, 1, 2.

54
New cards

while True

Needs a break inside, or it never ends.

55
New cards

!= in a while condition

The counter can skip the target: n=1; while n != 10: n += 2 runs forever (1,3,5,7,9,11…).

56
New cards

Range end is exclusive

range(1, 10) runs 9 times (1 through 9), NOT 10. Classic off-by-one source.

57
New cards

Loop-else runs on NO break

The else runs when the loop finishes normally — the name is misleading.

58
New cards

Variable defined only inside a branch

If the if/loop body never runs, the variable never exists → NameError. Initialize it before.

59
New cards

enumerate()

for i, v in enumerate(lst): gives (index, value) pairs together.

60
New cards

Trace — what prints? x = 7 | if x > 5: print('A') | elif x > 3: print('B') | else: print('C')

A — the FIRST true branch runs; the rest of the chain is skipped.

61
New cards

Trace — what prints? x = 5 | if x > 10: | ··if x > 20: print('big') | else: print('small')

small — the else pairs with the OUTER if (dangling else rule).

62
New cards

Trace — what prints? x = 10 | if x > 5: print('a') | if x > 8: print('b')

a then b — separate ifs are independent, so both run.

63
New cards

Trace — what prints? x = 0 | if x != 0 and 10/x > 1: print('y') | else: print('n')

n — and short-circuits on the False, so no ZeroDivisionError.

64
New cards

Trace — what prints? print(0 or 5) | print(5 and 0)

5 then 0 — and/or return the deciding operand, not True/False.

65
New cards

Trace — what prints? x = 150 | print(0 <= x < 100)

False — chained comparison: (0<=150) and (150<100).

66
New cards

Trace — what prints? x = True | y = False | print(not x or y)

False — not binds tighter: (not True) or False.

67
New cards

Trace — what prints? v = 1 | while v < 6: print(v) | v *= 2

1, 2, 4 — then v becomes 8 and fails the check.

68
New cards

Trace — what prints? for n in range(3): print(n)

0, 1, 2 — range starts at 0 and stops before 3.

69
New cards

Trace — what prints? total = 0 | for n in range(1, 5): total += n | print(total)

10 — sums 1+2+3+4.

70
New cards

Trace — what prints? for n in range(5): | ··if n == 3: break | ··print(n)

0, 1, 2 — break exits the loop immediately.

71
New cards

Trace — what prints? for n in range(5): | ··if n % 2 == 0: continue | ··print(n)

1, 3 — continue skips the even iterations.

72
New cards

Trace — how many lines? for a in range(3): | ··for b in range(3): print(a, b)

9 — the inner loop runs 3 times per outer iteration (3×3).

73
New cards

Trace — what prints? for n in range(3): | ··if n == 9: break | else: print('done')

done — no break ever happened, so the loop-else runs.

74
New cards

Trace — infinite? n = 1 | while n != 10: n += 2

Yes, infinite — n hits 1,3,5,7,9,11… and never exactly 10.

75
New cards

Leap year: 1900? 2000?

1900: no (century year, not divisible by 400). 2000: yes (divisible by 400).

76
New cards

Trace — what prints? print('apple' < 'Banana')

False — 'a' (97) > 'B' (66); string comparison is case-sensitive.

77
New cards

Trace — what happens? if x = 5: print('y')

SyntaxError — = assigns; == compares.