LESSON 04 · BEGINNER PATH9 min

Basic Operators

Use Python operators to calculate values and compare results.

Video coming to YouTube

Work through the full lesson and run its code while the video is being prepared for publication.

Understand the concept

Arithmetic operators combine numbers. The slash performs division, double slash gives floor division, percent gives the remainder, and double asterisks raise a value to a power.

Comparison operators such as ==, !=, >, and <= produce booleans. Parentheses make the intended order of evaluation clear when an expression has several operators.

  • Arithmetic operators
  • Comparison returns bool
  • Parentheses control grouping

See it step by step

Read the code, predict the output, then compare it with the result.

01. Divide into full boxes

PYTHON
items = 18
box_size = 4
print(items // box_size)
print(items % box_size)
EXPECTED OUTPUT
4
2

// counts full boxes and % gives the two items left over.

02. Group and compare

PYTHON
print((2 + 3) * 4)
print(7 > 5)
EXPECTED OUTPUT
20
True

Parentheses make addition happen first. The comparison produces a Boolean value.

Try it in Python

Edit the example and run it. Python starts in your browser the first time you click Run.

Python console

Ready to run
Need input()? Add one value per line
Ctrl / ⌘ + Enter to run
OUTPUT
Your output appears here.
Your turnCalculate the number of full boxes and leftover items when 23 items are packed in boxes of 5. Print both results.
Need a hint?

Use 23 // 5 for the full boxes and 23 % 5 for leftovers. Print each result, with or without a descriptive label.

Complete the task and select Check task to verify your code.

    Quick quiz

    Three questions. You can change your answers and try again.

    01.What is 7 // 2?

    02.What is 7 % 2?

    03.What is 2 + 3 * 4?

    Typical mistakes

    Everyone meets these errors. See what causes them and how to fix them.

    Using ^ for powers

    COMMON MISTAKE
    print(2 ^ 3)
    FIX
    print(2 ** 3)

    What happens: It prints 1 because ^ is bitwise XOR.
    Python's exponentiation operator is **.

    Dividing by zero

    COMMON MISTAKE
    print(10 / 0)
    FIX
    print(10 / 2)

    What happens: ZeroDivisionError.
    The divisor must be nonzero.