Arithmetic operators are used to perform mathematical operations in Python. They include basic mathematical operations such as addition, subtraction, multiplication, division, modulus, and exponentiation. In this tutorial, we will take a look at how to use these operators and how they can be used in Python code.
The addition operator is used to add two or more values together. For example:
x = 5
y = 2
result = x + y
print(result) # 7The subtraction operator is used to subtract one value from another. For example:
x = 5
y = 2
result = x - y
print(result) # 3The multiplication operator is used to multiply two or more values together. For example:
x = 5
y = 2
result = x * y
print(result) # 10The division operator is used to divide one value by another. For example:
x = 5
y = 2
result = x / y
print(result) # 2.5It's important to note that when dividing two integers in python, the result will be a float.
The modulus operator is used to find the remainder of a division. For example:
x = 5
y = 2
result = x % y
print(result) # 1The exponentiation operator is used to raise a value to a power. For example:
x = 5
y = 2
result = x ** y
print(result) # 25Floor division, represented by the symbol //, is an operator in Python that performs division and rounds down to the nearest whole number. It is similar to the regular division operator (/), but the result is always rounded down to the nearest whole number.
x = 7
y = 3
print(x / y) # 2.3333...
print(x // y) # 2In this tutorial, we've covered the basic arithmetic operators in Python and how to use them to perform mathematical operations in your code.