- What arithmetic operators are ā symbols that perform math operations
- Addition (+) ā adding two operands
- Subtraction (-) ā subtracting one operand from another
- Multiplication (*) ā multiplying two operands
- Division (/) ā dividing one operand by another
- Modulus (%) ā finding the remainder
- Exponentiation (**) ā raising to a power
- Floor Division (//) ā integer division
- Hands-on practice with the interactive editor
What are Arithmetic Operators?
In Python programming, arithmetic operators are symbols used to perform mathematical operations on operands. They work on numerical values (integers, floats, and complex numbers) and are fundamental to any Python program.
Before diving into arithmetic operators, let's understand some key terms:
- Operator: A symbol that performs an action on operands (e.g., +, -, *, /)
- Operand: A variable or constant on which the operation is performed
- Expression: A combination of operands and operators that computes a value
Python provides 7 arithmetic operators that work on two operands (binary operators).
ā Addition (+)
Adds two operands
ā Subtraction (-)
Subtracts second from first
āļø Multiplication (*)
Multiplies two operands
ā Division (/)
Returns float result
š§® Modulus (%)
Returns remainder
š¢ Exponentiation (**)
Raises to a power
š Floor Division (//)
Integer division
1. Addition (+)
The addition operator (+) is used to add two operands together. It works with integers, floats, and even strings (concatenation).
# Addition examples
x = 10
y = 5
z = x + y
print("x + y =", z) # Output: x + y = 15
# Addition with floats
a = 3.5
b = 2.5
print("a + b =", a + b) # Output: a + b = 6.0
# String concatenation
first = "Hello"
second = "World"
print(first + " " + second) # Output: Hello World
2. Subtraction (-)
The subtraction operator (-) subtracts the right operand from the left operand.
# Subtraction examples
x = 10
y = 5
z = x - y
print("x - y =", z) # Output: x - y = 5
# Subtraction with floats
a = 7.5
b = 3.2
print("a - b =", a - b) # Output: a - b = 4.3
3. Multiplication (*)
The multiplication operator (*) multiplies two operands together.
# Multiplication examples
x = 9
y = 4
z = x * y
print("x * y =", z) # Output: x * y = 36
# Multiplication with floats
a = 2.5
b = 4.0
print("a * b =", a * b) # Output: a * b = 10.0
4. Division (/)
The division operator (/) divides the left operand by the right operand and returns a float result.
# Division examples
x = 9
y = 4
z = x / y
print("x / y =", z) # Output: x / y = 2.25
# Division that results in integer
a = 10
b = 2
print("a / b =", a / b) # Output: a / b = 5.0
5. Modulus (%)
The modulus operator (%) returns the remainder of the division of the left operand by the right operand.
# Modulus examples
x = 9
y = 4
z = x % y
print("x % y =", z) # Output: x % y = 1
# Modulus with different numbers
a = 10
b = 3
print("a % b =", a % b) # Output: a % b = 1
6. Exponentiation (**)
The exponentiation operator (**) raises the left operand to the power of the right operand.
# Exponentiation examples
x = 9
y = 4
z = x ** y
print("x ** y =", z) # Output: x ** y = 6561
# Square of a number
a = 5
print("a ** 2 =", a ** 2) # Output: a ** 2 = 25
# Cube of a number
b = 3
print("b ** 3 =", b ** 3) # Output: b ** 3 = 27
7. Floor Division (//)
The floor division operator (//) divides the left operand by the right operand and returns the largest integer less than or equal to the result (rounds down).
# Floor division examples
x = 9
y = 4
z = x // y
print("x // y =", z) # Output: x // y = 2
# Floor division with negative numbers
a = -9
b = 4
print("a // b =", a // b) # Output: a // b = -3
# Positive numbers
c = 10
d = 3
print("c // d =", c // d) # Output: c // d = 3
Complete Example with All Operators
Let's see all arithmetic operators in action with a complete program.
# Working of arithmetic operators in Python
x = 9
y = 4
z = x + y
print("x + y =", z) # Output: x + y = 13
z = x - y
print("x - y =", z) # Output: x - y = 5
z = x * y
print("x * y =", z) # Output: x * y = 36
z = x / y
print("x / y =", z) # Output: x / y = 2.25
z = x % y
print("Remainder when", x, "divided by", y, "=", z)
# Output: Remainder when 9 divided by 4 = 1
z = x // y
print("x // y =", z) # Output: x // y = 2
z = x ** y
print("x ** y =", z) # Output: x ** y = 6561
# Output:
# x + y = 13
# x - y = 5
# x * y = 36
# x / y = 2.25
# Remainder when 9 divided by 4 = 1
# x // y = 2
# x ** y = 6561
š” Program Explanation: The arithmetic operators +, -, *, /, %, //, and ** compute addition, subtraction, multiplication, division, remainder, floor division, and exponentiation respectively. In our program, x=9 and y=4. The results are as shown above.
Try It Yourself!
Experiment with arithmetic operators directly in your browser. Modify the code and see the results in real time.
ARITHMETIC OPERATORS
========================================
x = 15, y = 4
1. ADDITION (+)
x + y = 19
2. SUBTRACTION (-)
x - y = 11
3. MULTIPLICATION (*)
x * y = 60
4. DIVISION (/)
x / y = 3.75
5. MODULUS (%)
x % y = 3
6. FLOOR DIVISION (//)
x // y = 3
7. EXPONENTIATION (**)
x ** y = 50625
ā Arithmetic operators are essential for math operations!
š You've Mastered Python Arithmetic Operators!
You understand addition, subtraction, multiplication, division, modulus, floor division, and exponentiation. These are essential tools for any Python programmer.
Quick Quiz ā Test Your Knowledge
Let's see what you've learned about Python arithmetic operators:
9 // 4 in Python?2 ** 3 in Python?Frequently Asked Questions
š¤ What's the difference between / and //?
ā¼
/ (division) returns a float result, while // (floor division) returns the largest integer less than or equal to the result. For example, 9/4 = 2.25, but 9//4 = 2.
š§ What is the modulus operator used for? ā¼
% returns the remainder of a division. It's commonly used to check if a number is even (num % 2 == 0) or for cycling through ranges.
š Does ** work with negative exponents?
ā¼
2 ** -2 returns 0.25. Python handles negative exponents by returning a float result.
š What is operator precedence? ā¼
** has the highest precedence, followed by *, /, //, %, and then + and -. Use parentheses to control evaluation order.
š Where to Go From Here
Now that you understand Python arithmetic operators, here are some related topics to explore:
š Assignment Operators
Learn about assignment operators
š Relational Operators
Learn about comparison operators
š§ Logical Operators
Learn about logical operators