Python Unit 2 - Logic

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

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:27 AM on 7/26/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

15 Terms

1
New cards

Comparison Operators

Operators that compare two values and return a Boolean (True or False):

  1. Equal to (==)

  2. Not equal to (!=)

  3. Greater than (>), Less than (<)

  4. Greater than or equal (>=), Less than or equal (<=)

Ex. 
x = 5

print(x == 5) # True

print(x != 5) # False

2
New cards

Logical Operators (and, or, not)

Used to combine or negate Boolean expressions:

and: Returns True if both statements are true.

or: Returns True if at least one statement is true.

not: Inverts the Boolean truth value.

Ex.

age = 20
has_id = True  

can_enter = (age >= 18) and has_id # True is_blocked = not has_id # False

3
New cards

Operator Precedence in Logic

The order in which logical evaluations are performed when multiple operators exist in one expression.
Ex.
1. not
2. and
3. or

4
New cards

Conditional Statement

A programming structure that controls program flow based on whether a condition evaluates to True or False.

Ex.

if temperature <= 80
     print("It's a hot day!")

5
New cards

Head-Body Structure

Python conditional statements use a head (containing the condition/keyword) and a body (containing the code to execute).

Ex.

if score >= 50:
	print("You Passed!")
	print("Congratulations!")
print("Game Over")

6
New cards

if statement

Evaluates a single Boolean expression or variable. Executes the code inside its body only if the condition is True. If False, the body is skipped entirely.

Ex.

day = 2  # Tuesday (0=Mon, 1=Tue, 2=Wed, etc.)
if 0 <= day <= 4:
    print("Weekday")  # Executes because condition is True

7
New cards

else statement

Attached to an if statement to define a backup code block that executes only when the preceding if condition evaluates to False .

Ex.

day = 5  # Saturday
if 0 <= day <= 4:
    print("Weekday")
else:
    print("Weekend")  # Executes because dow is 5 (False condition)

8
New cards

elif Statement

Short for "else if". Acts as a bridge between an initial if and a final else to check multiple conditions sequentially.

Ex.

num = 15
if num % 2 == 0:
    print("Divisible by 2")
elif num % 3 == 0:
    print("Divisible by 3")  # Executes, and the rest of the chain is skipped
elif num % 5 == 0:
    print("Divisible by 5")

9
New cards

Conditional Order

In an if/elif/else chain, order matters because the first True condition catches execution. Programmer best practice requires placing the most specific conditions first.

Ex.

num = 20

# INCORRECT ORDER: num % 2 catches 20 first, so Div5 never runs
# CORRECT ORDER: Specific condition (% 5) before broader condition (% 2)
if num % 5 == 0:
    print("Divisible by 5")  # Runs first!
elif num % 2 == 0:
    print("Even")

10
New cards

match Statement

Introduced for structural pattern matching, it compares a single variable or expression against multiple specific case blocks.

Ex.

x = 2
match x:
    case 1:
        print("One")
    case 2:
        print("Two")  # Matches and prints "Two"
    case 3:
        print("Three")

11
New cards

Match vs. If/Elif/Else

match statements are optimized for comparing exact, discrete values (like specific integers or strings). if/elif/else chains are more flexible and better suited for evaluating value ranges, inequalities (>, <)

12
New cards

Default Case

Written using a single underscore (case _:), this acts as a universal catch-all default case within a match statement.

Ex.

x = 99
match x:
    case 1:
        print("One")
    case 2:
        print("Two")
    case _:
        print("Not 1 or 2")  # Catches 99

13
New cards

Linked Cases

Groups multiple discrete values into a single case using the vertical bar (|). If the expression matches any of the grouped values, that case block executes.

Ex.

digit = 3
match digit:
    case 0 | 1 | 2 | 3 | 4:
        print("Round down")  # Matches because 3 is in the group
    case 5 | 6 | 7 | 8 | 9:
        print("Round up")

14
New cards

Nested Conditional Statement

Placing one conditional structure (such as an if/elif/else or match) completely inside another. The inner (nested) conditional is only evaluated if the outer conditional's branch executes.

Ex.

is_weekday = True
is_holiday = False

if is_weekday:
    # Inner conditional only runs if outer is True
    if is_holiday:
        print("No school (Holiday)")
    else:
        print("Go to school")
else:
    print("No school (Weekend)")

15
New cards

Cases for Nesting

Nesting is primarily used for multi-layered decision-making used for even cases and match statements.