NOETRION

Choose a topic

DAY 04 OF 30 · RUN AND REASON

Operators and expressions

Calculate totals, compare values, and distinguish equality, identity, and membership.

90–120 minutes suggested study timePython 3.12Not yet completed

Today's outcomes

  • Trace precedence and use parentheses.
  • Apply arithmetic, comparisons, and Boolean operators.
  • Distinguish ==, is, and in.

Before you start: Complete Day 03 and its practical challenge.

Understand the concept

Arithmetic operators express different questions

Addition, subtraction, multiplication, and division answer different calculation questions. / performs true division; for two ordinary integers it returns a float. // performs floor division. % produces a remainder connected to that floor division. ** raises a value to a power. Choose the operator from the question, not from the type you would like to see on screen.

For positive minute counts, 135 // 60 gives two complete hours and 135 % 60 gives 15 remaining minutes. With negative values, floor division goes toward negative infinity rather than simply deleting the fractional part. Thus -7 // 3 is -3 and -7 % 3 is 2. The identity a == (a // b) * b + (a % b) explains how the pair fits together for nonzero integer b.

Python follows precedence rules, so multiplication binds more tightly than addition. 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. Parentheses are useful documentation when a formula combines several operations. Write them to express the intended grouping rather than relying on a reader remembering every precedence rule.

Assignment updates state; comparison asks a question

completed = 4 assigns a value, whereas completed == 4 compares values. completed += 1 performs an augmented assignment. For the integer counter in this lesson, it calculates a new integer and rebinds the name. Mutable objects can have different in-place behavior, which you will inspect when lists arrive.

Comparisons include <, <=, >, >=, ==, and !=. A chained comparison such as 0 <= minutes <= 120 expresses two ordered checks. The boundary symbols matter: “at least 60” includes 60, while “more than 60” does not. A program can perfectly execute the wrong interpretation of a written rule.

Boolean logic combines conditions

and requires both conditions to be satisfied; or requires at least one; not reverses a truth value. In general Python, and/or return one of their operands rather than always producing a bool. In today's comparisons, the operands already are Booleans, so the combined results are Booleans too.

These operators also short-circuit. In denominator != 0 and total / denominator > 2, the division is not evaluated when the first condition is false. That can protect a particular expression from division by zero, but it should not conceal a missing-data decision. You still need to decide what an absent denominator means to the caller.

Equality, identity, and membership are separate relationships

== asks whether values compare equal. is asks whether two references identify the same object. in asks whether a value is a member of a container, or a substring when the right operand is a string. Use is None to test the singleton missing marker; do not use is to compare ordinary numeric or string values.

Worked example

minutes = 135
full_hours = minutes // 60
extra_minutes = minutes % 60
print("Duration:", full_hours, "hours", extra_minutes, "minutes")

target = 90
completed = 3
completed += 1
eligible = minutes >= target and completed >= 4
print("Eligible:", eligible)
print("In expected range:", 0 <= minutes <= 180)
print("Contains word:", "Python" in "30 Days Python Challenge")

score = None
print("Missing score:", score is None)
print("Negative quotient/remainder:", -7 // 3, -7 % 3)
Expression Result Reason
135 // 60 2 Two full positive groups of 60
135 % 60 15 Remaining positive minutes
completed after += 1 4 The previous count was 3
minutes >= target and completed >= 4 True Both comparisons are true
score is None True The score uses the chosen missing marker
-7 // 3, -7 % 3 -3, 2 -3 × 3 + 2 equals -7

These outputs describe a fictional eligibility rule. No statistical or educational claim follows from a learner meeting that made-up threshold.

Guided practice

  1. Predict: calculate 8 / 3, 8 // 3, and 8 % 3 by hand. Explain which result answers “how many complete groups?” and which answers “what is the ratio?”
  2. Modify: change minutes to 89 while leaving target at 90. Eligibility must become False. Then restore minutes and change completed to 3; the result must also become False.
  3. Reconstruct: write an expression that accepts a score from 0 through 100 inclusive. Test -1, 0, 100, and 101. Name the result is_valid_score so its meaning is visible to the next lesson's branching code.

When a Boolean result surprises you, print the individual comparisons before their combination. That identifies which part of the written rule differs from your assumptions.

Daily challenge

Create an eligibility calculator. A fictional certificate requires at least 10 completed days and at least 600 total practice minutes. Compute both conditions, then the combined result. Also decompose the total minutes into full hours and remaining minutes. No if statement is needed yet: print the calculated Boolean.

Completed days Total minutes Eligible? Duration
10 600 True 10 hours, 0 minutes
9 600 False 10 hours, 0 minutes
10 599 False 9 hours, 59 minutes
12 735 True 12 hours, 15 minutes

Assume nonnegative integer counts today. State that contract next to the inputs. Day 5 will turn the Boolean into a readable outcome and reject values outside the valid range.

Reference solution

Compare after attempting the challenge
completed_days = 12
practice_minutes = 735

enough_days = completed_days >= 10
enough_minutes = practice_minutes >= 600
eligible = enough_days and enough_minutes
hours = practice_minutes // 60
remaining = practice_minutes % 60

print("Enough days:", enough_days)
print("Enough minutes:", enough_minutes)
print("Eligible:", eligible)
print(f"Duration: {hours} hours, {remaining} minutes")

The named component conditions make a failure easier to explain. If eligible is False, you can identify which requirement is unmet without recreating the entire expression during debugging.

Debug and explain

Do not write completed_days is 10; use equality for a value comparison. Do not replace >= with > merely to make a single example pass. Test the exact threshold. Do not assume int(-7 / 3) behaves like floor division: truncation toward zero and flooring differ for negative values.

Explain why 600 minutes passes, why changing and to or changes the certificate rule, and why membership is different from identity. Rewrite the eligibility requirement in ordinary English without reading the variable names. If the English rule changes, your acceptance table must change too.

Review and next step

Keep all four acceptance cases, not just the successful case. During the next review, reconstruct the inclusive boundaries and negative quotient identity. Day 5 adds decisions that transform conditions into clear, mutually understandable messages.

Sources and verification

Original teaching examples checked with Python 3.12.14. Library behavior reviewed against these primary references on October 3, 2026. Day 26's examples also use Beautiful Soup 4.14.3.

DAILY CHECKPOINT

Check your understanding

Five questions, 20 points each. Pass at 85% or above, which requires all five correct. Feedback explains each answer; revise the lesson and retry when needed.

01. What is 7 // 2?

02. What is 7 % 2?

03. Which operator checks equal values?

04. What does x in values check?

05. What is 2 + 3 * 4?

Review your practical work

Check these statements after running your own challenge. This is your self-review; the website does not execute or assess your Python code.

Pass the knowledge check and finish the practical self-review to save completion.