Skip to content
PythonExamples

LESSON 5 / 5 · 8 MIN

Basic Operators

LET'S MAKE IT CLICK

Combine your values

Operators combine or compare values. An expression such as 7 + 2 evaluates to a result you can assign or print.

WATCH THE IDEA
Left value7

The first operand.

Operator+

Choose the operation.

Result7 + 2 → 9

Combine the two operands.

An operator connects values to produce a result.

Move through one idea at a time. Watch, experiment, and check your understanding along the way.

Start with arithmetic

WATCH THE IDEA
Add7 + 2 → 9

Find the sum.

Multiply7 * 2 → 14

Find the product.

Divide7 / 2 → 3.5

True division produces a float here.

Different operators answer different questions about the same two values.

Use + for addition, - for subtraction, and * for multiplication. The / operator performs true division: with integers, its result is a float, so 8 / 2 produces 4.0.

See the Python code
print(7 + 2)  # 9
print(7 * 2)  # 14
print(7 / 2)  # 3.5

Whole quotients and remainders

WATCH THE IDEA
True quotient-7 / 2 → -3.5

Start with the exact quotient here.

Round down-3.5 → -4

Move toward negative infinity.

Remainder-7 % 2 → 1

Because -7 = (-4 × 2) + 1.

Floor division rounds down. The remainder keeps the integer division relationship true.

With integer operands, // gives the quotient rounded down, and % gives the remainder. For example, 7 // 2 is 3 and 7 % 2 is 1.

Rounded down means toward negative infinity: -7 // 2 is -4, not -3. Python keeps the relationship a = (a // b) * b + (a % b) for integer operands with a nonzero divisor.

YOUR TURN TO EXPLORE

Change an operator. Inspect the result.

Choose two integers and an operation. Try division by zero or a negative input and watch what Python would display.

example.pyPython
    CURRENT VALUESReady

    No assignments yet.

    OUTPUT

    No output yet.

    Predict the result, then take the first step.

    QUICK CHECK 1 / 2

    What does 7 // 2 evaluate to?
    You can try again.

    Compare values

    WATCH THE IDEA
    Compare3 == 3

    Ask whether the values are equal.

    EvaluateEqual values

    The comparison is satisfied.

    ResultTrue · bool

    A comparison returns a boolean.

    Use == to compare. A single = assigns a value to a name.

    == checks equality; > checks whether the left value is greater. Comparisons produce a boolean: True or False. Remember that = assigns and == compares.

    See the Python code
    print(3 == 3)  # True
    print(3 > 5)   # False

    Make the order clear

    WATCH THE IDEA
    Expression2 + 3 * 4

    Multiplication has higher precedence.

    Multiply first2 + 12

    Evaluate 3 * 4.

    Then add14

    Finish with 2 + 12.

    Parentheses can change the order: (2 + 3) * 4 is 20.

    Multiplication and division normally happen before addition and subtraction. Parentheses change the order: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20.

    Division, floor division, and remainder with a zero divisor raise ZeroDivisionError. Check or handle invalid inputs when building real programs.

    See the Python code
    print(2 + 3 * 4)    # 14
    print((2 + 3) * 4)  # 20

    QUICK CHECK 2 / 2

    What does 2 + 3 * 4 evaluate to?
    You can try again.

    TAKE IT WITH YOU

    What you have learned

    • / and // perform different kinds of division.
    • Comparisons return True or False.
    • Parentheses make evaluation order explicit.

    Answer both quick checks correctly to mark this lesson complete. You can continue at any time.