Basic Operators
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.
Use Python operators to calculate values and compare results.
Example check: Worked and guided code samples are checked against CPython 3.13. Practice uses Pyodide 0.29.5 (Python 3.13) in your browser. How we check lessons
Video lesson: Basic Operators
The preview is stored on this site. YouTube loads only when you press Play. Watch on YouTube
Jump to a chapter
Read the video transcript
Select a timestamp to watch that moment on YouTube.
- 00:00
Operators let Python calculate and compare values. Imagine sharing seven items between two teams. You can ask how many items each team receives if halves are allowed, how many complete pairs you can form, or how many items are left over. Those are three different questions, even though each starts with seven and two. On the lesson page, the starter code asks all three and then tests a power and a comparison. Before running it, predict which lines produce a whole number, which produce a decimal number, and which produce a Boolean answer. Do not worry if you cannot name every symbol yet. We will connect each operator to the question it answers.
- 00:41
By the end, you will use floor division and remainder to solve a packing task, and you will know how parentheses change an expression's order of evaluation. The useful habit is to choose an operator because of the meaning of the problem, not because a symbol looks vaguely familiar from another language. Run the starter and match each printed result to its operator. One slash performs ordinary division: seven divided by two is three point five. Two slashes perform floor division: with these positive numbers the whole-group count is three. Percent gives the remainder, one. Three groups of two use six items, leaving one.
- 01:22
The two results fit the check three times two plus one equals seven. That relationship is a useful way to verify a packing calculation. There is a detail in the name floor division: it rounds down toward negative infinity, not simply toward zero. In our packing examples all counts are positive, so the result is the intuitive number of full boxes. If you later use negative values, do not assume the result is just the decimal part removed. Try negative seven double-slash two in a separate experiment; Python gives negative four. For physical items, a negative count probably means the input should be checked before packing anything.
- 02:05
Two asterisks mean exponentiation, so two raised to the third power gives eight. The greater-than comparison asks whether seven is greater than two and produces the Boolean value True. A comparison produces a truth value, not the numeric difference between the inputs. Now inspect the deeper packing example on the page. Twenty-seven items in groups of four make six complete groups and leave three. Six times four plus three reconstructs twenty-seven, so the printed equality check is True. Use that check when you solve a similar real-world problem; it can catch a swapped divisor or a mistaken leftover count.
- 02:45
The next guide example prints two plus three times four. Multiplication happens first, giving fourteen. Parentheses around two plus three make that addition happen first, giving twenty. Read the expression as a series of operations, not just a left-to-right row of symbols. If you want a particular grouping, parentheses make the intent explicit to Python and to another reader. Finally, the guide combines comparisons. A score of seventy-four is at least sixty and at most one hundred, so the range check is True. Seventy-four has remainder zero when divided by two, so an even-number test is also True.
- 03:26
The word and combines two Boolean results and is True only when both conditions are true. We will use this style again in conditions. For now, notice that arithmetic can produce a number, and a comparison of that number can produce True or False. Here is a symbol trap. Many people expect the caret to mean a power. In Python, caret is bitwise exclusive or, so two caret three produces one. The code runs, which can make the answer especially confusing. Use two asterisks for the power you intended, and the result becomes eight. Read the operator itself instead of assuming a familiar symbol has the same meaning everywhere.
- 04:08
Another mistake is dividing by zero. Python raises ZeroDivisionError because the operation has no valid numeric result. Look at the value on the right side of the division and trace where that divisor came from. If it was calculated or entered earlier, validate it before dividing. A good debugging method is to print the operands immediately before the failing expression, then fix the source of the unexpected value. These mistakes illustrate two kinds of problem: code that runs but answers the wrong question, and code that stops with a clear exception. Your practical task is to pack twenty-three items into boxes that hold five each.
- 04:50
Use floor division to find the number of full boxes and percent to find the leftover items. Pause and calculate it yourself first: four full boxes use twenty items and leave three. Set up names for the item count and box size, then print both answers clearly. The task checker looks for the calculation as well as its output, so typing the numbers four and three by hand is not the point. If the checker does not pass, confirm that you used double slash for full boxes and percent for leftovers, with twenty-three on the left and five on the right. Reconstruct the original count in your head: four times five plus three equals twenty-three.
- 05:31
The quiz asks what seven double-slash two returns, what seven percent two returns, and which operation happens first in two plus three times four. Before choosing an option, explain the relevant rule in a sentence. As a final experiment, change the box size to six and predict both outputs before you run again. The goal is not to memorize one packing answer. It is to connect the operators to questions you can solve repeatedly as the inputs change. Later, conditions and loops will use these results to make decisions and repeat work.
Understand the concept
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
items = 18
box_size = 4
print(items // box_size)
print(items % box_size)4
2// counts full boxes and % gives the two items left over.
02. Group and compare
print((2 + 3) * 4)
print(7 > 5)20
TrueParentheses make addition happen first. The comparison produces a Boolean value.
A closer look
Follow the reasoning, inspect each result, then try the suggested changes in the console below.
Translate a packing problem into two answers
Many practical calculations ask for both complete groups and leftovers. Floor division counts the complete groups; remainder tells you what does not fit. The two results belong together: groups × size + remainder reconstructs the original total. For a positive group size, the remainder is always smaller than that size.
Use this identity to check your own answer instead of trusting a printed number blindly. In the example, 27 items fill groups of 4. Predict the leftover count before running it, then change the total to 28 and watch the remainder become zero. A zero remainder means every item fits into a complete group.
items = 27
size = 4
groups = items // size
leftover = items % size
print(groups, leftover)
print(groups * size + leftover == items)6 3
True- 27 // 4 counts six complete groups.
- 27 % 4 finds the three items left over.
- The final comparison verifies that 6 × 4 + 3 reconstructs 27.
Read an expression in evaluation order
An expression can contain several operations, but Python follows precedence rules rather than simply working left to right. Multiplication is evaluated before addition. Parentheses let you make a different intention explicit; they also help readers who do not want to remember the full precedence table. Small numeric examples make this difference easy to see.
The same numbers below produce two different answers because the grouping changed. First predict both results without running the code. Then imagine a price of 2 units plus a fee of 3 units, repeated four times: only the parenthesized expression multiplies the entire per-item amount. Clear grouping can prevent a real billing mistake.
print(2 + 3 * 4)
print((2 + 3) * 4)14
20- Without parentheses, 3 × 4 is evaluated before adding 2.
- With parentheses, 2 + 3 becomes 5 before multiplication.
- The grouping communicates which quantity is being repeated.
Turn a numeric rule into a Boolean check
Operators also answer yes/no questions. A comparison such as >= returns True or False, and and combines two checks that must both hold. For a score to sit inside an inclusive range, it must meet the lower limit and not exceed the upper limit. Here == asks whether two values are equal; a single = would assign a name instead.
You can inspect a condition with print before using it in an if statement later. The sample checks both a range and evenness. Try changing score to 75: the range remains valid, but the evenness check changes. This habit of testing boundary values helps reveal whether you meant < or <=.
score = 74
print(score >= 60 and score <= 100)
print(score % 2 == 0)True
True- Both range comparisons must be true for and to return True.
- A remainder of zero after division by 2 means the number is even.
- The printed Booleans can guide later decisions in a program.
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 runNeed input()? Add one value per line
Your output appears here.
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.
Typical mistakes
Everyone meets these errors. See what causes them and how to fix them.
Using ^ for powers
print(2 ^ 3)print(2 ** 3)What happens: It prints 1 because ^ is bitwise XOR.
Python's exponentiation operator is **.
Dividing by zero
print(10 / 0)print(10 / 2)What happens: ZeroDivisionError.
The divisor must be nonzero.