Photo by Ries Bosch on Unsplash
Mastering mathematical operations in Python: Arithmetic Operators and more
Arithmetic operators in Python are used to perform mathematical computations on numeric values. These operators interact with different data types and produce different outcomes. For example, if I use the +
operator with a pair of integers or with a pair of strings, it will output a different result. The operations we are going to perform below utilize integers and floats. There are several operators available in Python, including:
Addition
Subtraction
Multiplication
Division
Modulus
Exponentiation
Floor division
By the end of this article, you will be able to comprehend and implement arithmetic operators in your programs.
Addition
The addition operator is used to add two values. In Python, the addition operator is represented by the +
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 3
y = 2
result = x + y
print(result) # Output => 5
Subtraction
The subtraction operator is used to subtract the second value from the first value. In Python, the subtraction operator is represented by the -
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 7
y = 5
result = x - y
print(result) # Output => 2
Multiplication
The multiplication operator is used to calculate the product of two values. In Python, the multiplication operator is represented by the *
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 4
y = 6
result = x * y
print(result) # Output => 24
Division
The division operator is used to calculate the quotient when the first value is divided by the second value. In Python, the division operator is represented by the /
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 7
y = 2
result = x / y
print(result) # Output => 3.5
Modulus
The modulus operator is used to calculate the remainder of a division. In Python, the modulus operator is represented by the %
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 5
y = 3
result = x % y
print(result) # Output => 2
Exponentiation
The exponentiation operator is used to raise the first value to the power of the second value. In Python, the exponentiation operator is represented by the **
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 3
y = 2
result = x ** y
print(result) # Output => 9
Floor division
The floor division operator is used to floor the quotient of a division. In Python, the floor division operator is represented by the //
. In the following code, we are storing the operation in the result
variable which we are then going to print.
x = 7
y = 3
result = x // y
print(result) # Output => 2.0
Order of operations
In mathematics, arithmetic operators follow a basic order of precedence. The rules of operator precedence to determine the order of operations when evaluating an expression are followed by Python and many other programming languages as well. The operations are executed according to this order:
( )
**
*, /, %, //
+, -
Changing the value of a variable with arithmetic operators
There will often be scenarios where you want to operate on the original value of a variable and then reassign the result of that operation to the same variable. Let's take a look at the following code:
subscribers = 30
subscribers = subscribers + 10
print(subscribers) # Output => 40
To shorten these steps, Python provides shorthand assignment operators that will simplify and accelerate our program, including:
+=
-=
*=
/=
age = 24
age += 3
print(age) # Output => 27
count = 5
count -= 1
print(count) # Output => 4
discount = 20
discount *= 2
print(discount) # Output => 40
price = 12
price /= 4
print(price) # Output => 3.0
Conclusion
In conclusion, Python provides a rich set of arithmetic operators for performing mathematical calculations with numerical data. These operators can be combined with other expressions to create complex calculations. Overall, mastering arithmetic operators is a fundamental skill in programming with Python and can be applied to a wide range of computational tasks.