In Python, arithmetic operators are used to perform basic mathematical operations on numeric data types, such as integers and floating-point numbers. Here are the common arithmetic operators in Python:
+
Addition: Adds two operands together.-
Subtraction: Subtracts the right operand from the left operand.*
Multiplication: Multiplies two operands./
Division: Divides the left operand by the right operand.%
Modulus: Returns the remainder of the division of the left operand by the right operand.**
Exponentiation: Raises the left operand to the power of the right operand.//
Floor Division: Returns the quotient of the division, rounded down to the nearest integer.Let's see some examples of using arithmetic operators in Python:
# Addition
num1 = 10
num2 = 5
result = num1 + num2
print("Addition:", result)
# Subtraction
x = 15
y = 7
difference = x - y
print("Subtraction:", difference)
# Multiplication
a = 5
b = 3
product = a * b
print("Multiplication:", product)
# Division
dividend = 20
divisor = 4
quotient = dividend / divisor
print("Division:", quotient)
# Modulus
dividend = 17
divisor = 5
remainder = dividend % divisor
print("Modulus:", remainder)
# Exponentiation
base = 2
exponent = 3
result = base ** exponent
print("Exponentiation:", result)
# Floor Division
dividend = 17
divisor = 5
quotient = dividend // divisor
print("Floor Division:", quotient)
Understanding arithmetic operators is essential for performing mathematical calculations and writing expressions in Python.