Python is a high-level programming language that provides various built-in functions to perform arithmetic operations.
The arithmetic operators in Python are used to perform mathematical calculations. In this article, we will discuss the different types of arithmetic operations in Python.
Addition
Addition is one of the most common arithmetic operations in Python. The addition operator is represented by the symbol ‘+’. It is used to add two or more numbers. For example:
a = 10 b = 20 c = a + b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 10 and 20 respectively. We are adding them using the addition operator ‘+’ and storing the result in the variable ‘c’. The output of this program is 30.
Subtraction
The subtraction operator is represented by the symbol ‘-’. It is used to subtract one number from another. For example:
a = 20 b = 10 c = a - b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 20 and 10 respectively. We are subtracting ‘b’ from ‘a’ using the subtraction operator ‘-’ and storing the result in the variable ‘c’. The output of this program is 10.
Multiplication
The multiplication operator is represented by the symbol ‘*’. It is used to multiply two or more numbers. For example:
a = 2 b = 3 c = a * b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 2 and 3 respectively. We are multiplying them using the multiplication operator ‘*’ and storing the result in the variable ‘c’. The output of this program is 6.
Division
The division operator is represented by the symbol ‘/’. It is used to divide one number by another. For example:
a = 10 b = 2 c = a / b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 10 and 2 respectively. We are dividing ‘a’ by ‘b’ using the division operator ‘/’ and storing the result in the variable ‘c’. The output of this program is 5.
Floor Division
The floor division operator is represented by the symbol ‘//’. It is used to divide one number by another and return the result as an integer (rounded down). For example:
a = 10 b = 3 c = a // b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 10 and 3 respectively. We are performing the floor division of ‘a’ by ‘b’ using the floor division operator ‘//’ and storing the result in the variable ‘c’. The output of this program is 3.
Modulus
The modulus operator is represented by the symbol ‘%’. It is used to find the remainder of a division operation. For example:
a = 10 b = 3 c = a % b print(c)
In this example, we have two variables ‘a’ and ‘b’ with values 10 and 3 respectively. We are finding the remainder of ‘a’ divided by ‘b’ using the modulus operator ‘%’ and storing the result in the variable ‘c’. The output of this program is 1.
Exponentiation
The exponentiation operator is represented by the symbol ‘**’. It is used to raise a number to a power. For example:
>>> 2 ** 3 8 >>> 10 ** 2 100
In the first example, we raise 2 to the power of 3, which is 8. In the second example, we raise 10 to the power of 2, which is 100.
Modulus Operator
The modulus operator is represented by the symbol ‘%’. It returns the remainder of a division operation. For example:
>>> 7 % 3 1 >>> 15 % 4 3
In the first example, 7 divided by 3 gives a quotient of 2 with a remainder of 1, so the result is 1. In the second example, 15 divided by 4 gives a quotient of 3 with a remainder of 3, so the result is 3.
Floor Division Operator
The floor division operator is represented by the symbol ‘//’. It performs a division operation and returns the floor of the quotient, which is the largest integer that is less than or equal to the result. For example:
>>> 7 // 3 2 >>> 15 // 4 3
In the first example, 7 divided by 3 gives a quotient of 2 with a remainder of 1. The floor of 2 is 2, so the result is 2. In the second example, 15 divided by 4 gives a quotient of 3 with a remainder of 3. The floor of 3 is 3, so the result is 3.
Operator Precedence
When performing multiple arithmetic operations in a single expression, Python follows a specific order of precedence. The order of precedence, from highest to lowest, is as follows:
- Parentheses
- Exponentiation
- Multiplication and division
- Addition and subtraction
Parentheses can be used to group operations and override the default order of precedence. For example:
>>> 3 + 4 * 2 11 >>> (3 + 4) * 2 14
In the first example, Python performs the multiplication operation before the addition operation, resulting in a value of 11. In the second example, we use parentheses to group the addition operation, forcing Python to perform it before the multiplication operation, resulting in a value of 14.
Conclusion
In Python, arithmetic operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, exponentiation, modulus, and floor division.
Understanding how to use these operators and their order of precedence is essential to writing effective and efficient code.
By mastering the basics of arithmetic operations, you will be well on your way to becoming a proficient Python programmer.