Loading...

Python Operators

A single slash keeps the half. print(5 / 2) writes 2.5. The same line can subtract or multiply. print(5 - 2) writes 3. print(5 * 2) writes 10. These signs work on numbers. They do not turn text into a number. That was the previous page.

Subtract, multiply, then keep the half

python
print(5 - 2)
print(5 * 2)
print(5 / 2)

The three lines are 3, then 10, then 2.5.

Two slashes cut, percent is the leftover, two stars raise

print(5 // 2) writes 2. The half is cut off, not kept. print(5 % 2) writes 1. That is the remainder, what is left after the even split. It is not a percent. print(2 ** 3) writes 8. That is 2 raised to 3, which is 2 * 2 * 2. Two slashes are not a comment. A comment starts with #.

Cut, remainder, then a power

python
print(5 // 2)
print(5 % 2)
print(2 ** 3)

The three lines are 2, then 1, then 8.

Multiply happens before plus

print(2 + 3 * 4) writes 14. The program multiplies 3 * 4 first, then adds 2. It does not add 2 + 3 first. print((2 + 3) * 4) writes 20. The parentheses force the addition first. A power happens even earlier: 2 * 3 2 is 18, because 3 2 is 9 and then 2 * 9 is 18.

Multiply first, unless parentheses say otherwise

python
print(2 + 3 * 4)
print((2 + 3) * 4)

The first line is 14. The second line is 20.

Dividing by zero stops the program

5 / 0, 5 // 0, and 5 % 0 all stop the program. There is no number that is the result of splitting by zero.

The next page asks which one is larger

This page does not ask whether two values are the same, does not choose a branch, and does not join two conditions. The next page asks whether one value is larger, smaller, or the same as another.

Say these yourself

  1. 5 - 2 is 3. 5 * 2 is 10. 5 / 2 is 2.5.
  2. 5 // 2 is 2. 5 % 2 is 1. 2 ** 3 is 8.
  3. 2 + 3 * 4 is 14. (2 + 3) * 4 is 20.
  4. Dividing by zero stops the program.
  5. The next page asks whether one value is larger, smaller, or the same.

Easy to misread

  • Reading 5 / 2 as 2
  • Reading 5 // 2 as 2.5
  • Reading % as a percent
  • Reading // as a comment
  • Adding before multiplying in 2 + 3 * 4
  • Expecting 5 / 0 to write a number

Keep

One slash keeps the half. Two slashes cut it. % is the remainder, not a percent. ** raises a number to a power.
5 / 2 is 2.5. 5 // 2 is 2. 5 % 2 is 1. 2 ** 3 is 8.
Multiply and divide happen before plus and minus. Parentheses happen first. A power happens before multiply.
Dividing by zero stops the program.
The next page asks whether one value is larger, smaller, or the same.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top