Skip to main content

Operators: Arithmetic Operators (+, -, *, /, %, //, **)

Following our exploration of Comments and Docstrings, this article dives into the world of arithmetic operators in Python. These operators allow you to perform mathematical calculations in your code.


📚 Prerequisites

A basic understanding of Python variables and number data types.


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • The standard arithmetic operators.
  • The difference between division and floor division.
  • How to use the modulus and exponentiation operators.
  • The order of operations in Python.

🧠 Section 1: Standard Arithmetic Operators

Python supports the following standard arithmetic operators:

OperatorNameExample
+Addition10 + 5
-Subtraction10 - 5
*Multiplication10 * 5

💻 Section 2: Division Operators

Python has two division operators:

  • / (Division): This operator performs "true division" and always returns a float.

    print(10 / 3) # Output: 3.333...
  • // (Floor Division): This operator divides and rounds the result down to the nearest whole number.

    print(10 // 3) # Output: 3

🛠️ Section 3: Modulus and Exponentiation

  • % (Modulus): This operator returns the remainder of a division.

    print(10 % 3) # Output: 1
  • ** (Exponentiation): This operator raises the first number to the power of the second.

    print(2 ** 3) # Output: 8

🔬 Section 4: Order of Operations

Python follows the standard mathematical order of operations, often remembered by the acronym PEMDAS/BODMAS:

  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication *, Division /, Floor Division //, Modulus % (from left to right)
  4. Addition +, Subtraction - (from left to right)

💡 Conclusion & Key Takeaways

You've now learned how to use arithmetic operators to perform mathematical calculations in Python.

Let's summarize the key takeaways:

  • Standard Operators: +, -, *.
  • Division: / for float division, // for floor division.
  • Modulus and Exponentiation: % for remainder, ** for powers.
  • Order of Operations: Python follows the standard mathematical order of operations.

Challenge Yourself: Write a script that converts a temperature from Fahrenheit to Celsius. The formula is (F - 32) * 5/9.


➡️ Next Steps

In the next article, we'll learn about "Operators: Comparison Operators".

Happy coding!


Glossary (Python Terms)

  • Operator: A symbol that performs an operation on one or more operands.
  • Operand: A value that an operator acts on.
  • Expression: A combination of values, variables, and operators that evaluates to a single value.

Further Reading (Python Resources)