How to Use Arithmetic Operators in Python: Simple Guide
In Python, you use
+, -, *, /, //, %, and ** as arithmetic operators to perform addition, subtraction, multiplication, division, floor division, modulus, and exponentiation respectively. These operators work with numbers to calculate and return results directly in your code.Syntax
Python uses simple symbols called arithmetic operators to do math. Here are the main ones:
+adds two numbers-subtracts one number from another*multiplies two numbers/divides one number by another (result is a float)//divides and gives the whole number part (floor division)%gives the remainder after division (modulus)**raises a number to the power of another (exponent)
python
result = 5 + 3 # addition result = 5 - 3 # subtraction result = 5 * 3 # multiplication result = 5 / 3 # division (float) result = 5 // 3 # floor division result = 5 % 3 # modulus result = 5 ** 3 # exponentiation
Example
This example shows how to use each arithmetic operator and prints the result.
python
a = 10 b = 3 print('Addition:', a + b) # 13 print('Subtraction:', a - b) # 7 print('Multiplication:', a * b) # 30 print('Division:', a / b) # 3.3333333333333335 print('Floor Division:', a // b) # 3 print('Modulus:', a % b) # 1 print('Exponentiation:', a ** b) # 1000
Output
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000
Common Pitfalls
Some common mistakes when using arithmetic operators in Python include:
- Using
/expecting an integer result (it always returns a float). - Confusing
//(floor division) with/(true division). - Forgetting operator precedence, which means multiplication and division happen before addition and subtraction.
- Trying to use arithmetic operators on incompatible types like strings without conversion.
python
x = 7 / 2 print(type(x)) # Outputs: <class 'float'> # Wrong: expecting integer # y = 7 / 2 # y is 3.5, not 3 # Right: use floor division for integer result y = 7 // 2 print(y) # Outputs: 3 # Operator precedence example result = 2 + 3 * 4 # multiplication first print(result) # Outputs: 14, not 20
Output
<class 'float'>
3
14
Quick Reference
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 5 + 3 | 8 |
| - | Subtraction | 5 - 3 | 2 |
| * | Multiplication | 5 * 3 | 15 |
| / | Division (float) | 5 / 3 | 1.6667 |
| // | Floor Division | 5 // 3 | 1 |
| % | Modulus (remainder) | 5 % 3 | 2 |
| ** | Exponentiation | 5 ** 3 | 125 |
Key Takeaways
Use +, -, *, /, //, %, and ** to perform basic math operations in Python.
Division (/) always returns a float; use // for integer division.
Operator precedence means * and / happen before + and - unless you use parentheses.
Modulus (%) gives the remainder after division.
Exponentiation (**) raises a number to a power.