Introduction
Arithmetic operators in Python are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more. These operators work on numeric values and return a result based on the operation performed.
List of Arithmetic Operators
Here is a list of the arithmetic operators available in Python, along with their descriptions and examples:
1. Addition (+
)
Adds two numbers.
Example:
x = 10
y = 5
result = x + y
print(result) # Output: 15
2. Subtraction (-
)
Subtracts the second number from the first number.
Example:
x = 10
y = 5
result = x - y
print(result) # Output: 5
3. Multiplication (*
)
Multiplies two numbers.
Example:
x = 10
y = 5
result = x * y
print(result) # Output: 50
4. Division (/
)
Divides the first number by the second number and returns a floating-point result.
Example:
x = 10
y = 5
result = x / y
print(result) # Output: 2.0
5. Modulus (%
)
Returns the remainder of the division of the first number by the second number.
Example:
x = 10
y = 3
result = x % y
print(result) # Output: 1
6. Exponentiation (**
)
Raises the first number to the power of the second number.
Example:
x = 2
y = 3
result = x ** y
print(result) # Output: 8
7. Floor Division (//
)
Divides the first number by the second number and returns the largest integer less than or equal to the result (i.e., the floor of the division).
Example:
x = 10
y = 3
result = x // y
print(result) # Output: 3
Examples of Using Arithmetic Operators
Here are some more examples demonstrating the use of arithmetic operators in Python:
# Example 1: Addition
a = 15
b = 7
sum_result = a + b
print("Sum:", sum_result) # Output: Sum: 22
# Example 2: Subtraction
difference_result = a - b
print("Difference:", difference_result) # Output: Difference: 8
# Example 3: Multiplication
product_result = a * b
print("Product:", product_result) # Output: Product: 105
# Example 4: Division
division_result = a / b
print("Division:", division_result) # Output: Division: 2.142857142857143
# Example 5: Modulus
modulus_result = a % b
print("Modulus:", modulus_result) # Output: Modulus: 1
# Example 6: Exponentiation
exponentiation_result = a ** b
print("Exponentiation:", exponentiation_result) # Output: Exponentiation: 170859375
# Example 7: Floor Division
floor_division_result = a // b
print("Floor Division:", floor_division_result) # Output: Floor Division: 2
Conclusion
Arithmetic operators are used to perform mathematical calculations. By mastering these operators, you can perform a wide range of numerical operations, from basic addition and subtraction to more advanced operations like exponentiation and floor division.