Arithmetic Operators
6 min ยท The Basics
Arithmetic Operators
Python supports all standard mathematical operations you're familiar with. These operators allow you to perform calculations and manipulate numeric values in your programs.
Understanding arithmetic operators is fundamental to programming, as they're used in almost every program for calculations, comparisons, and data manipulation.
Detailed Operator Explanation
Let's break down each operator with more examples:
Assignment Operators
Assignment operators provide shortcuts for updating variables. They combine an arithmetic operation with assignment:
Operator Precedence (Order of Operations)
Python follows standard mathematical order of operations. When an expression has multiple operators, Python evaluates them in a specific order:
๐ Order of Operations (PEMDAS)
Parentheses (highest priority)
Exponents (**)
Multiplication, Division (*, /, //, %)
Addition, Subtraction (+, -)
Practical Examples
Here are real-world examples using arithmetic operators:
Important Notes
๐ก Division Always Returns Float
Regular division (/) always returns a float, even if the result is a whole number. Use floor division (//) if you need an integer result.
๐ก Modulus is Useful
The modulus operator (%) is great for checking if numbers are even/odd, wrapping around values, and many other useful patterns.
๐ก Use Parentheses for Clarity
When in doubt, use parentheses to make your expressions clear. It's better to be explicit than to rely on operator precedence!