Python is a popular and flexible programming language. One of the best features of Python is its ability to perform arithmetic operations easily and quickly. In this article, we will discuss various types of arithmetic operations available in Python and how you can use them in your code.
Addition
The first arithmetic operation we will discuss is addition. Addition in Python can be done using the + operator. Here is a simple example:
a = 10 b = 20 c = a + b print(c) #Output: 30
In the example above, we have two variables a and b each with a value of 10 and 20 respectively. Then, we add the values of both variables and store the result in the variable c. Finally, we print the result using the print function.
Subtraction
In addition to addition, Python also supports subtraction operation. Subtraction is done using the – operator. Here is an example:
a = 50 b = 30 c = a - b print(c) #Output: 20
In the example above, we have two variables a and b each with a value of 50 and 30 respectively. Then, we subtract the value of variable b from variable a and store the result in the variable c. Finally, we print the result using the print function.
Multiplication
The next arithmetic operation is multiplication. Multiplication in Python is done using the * operator. Here is a simple example:
a = 5 b = 10 c = a * b print(c) #Output: 50
In the example above, we have two variables a and b each with a value of 5 and 10 respectively. Then, we multiply the values of both variables and store the result in the variable c. Finally, we print the result using the print function.
Division
In addition to multiplication, Python also supports division operation. Division is done using the / operator. Here is an example:
a = 100 b = 5 c = a / b print(c) #Output: 20
In the example above, we have two variables, namely a and b, each of which has a value of 100 and 5, respectively. Then, we divide the value of the a variable by the b variable and store the result in the c variable. Finally, we print the result using the print function.
Integer Division
If you want the result of the division to be an integer, you can use the // operator. This operator will ignore the numbers behind the decimal point and produce an integer. Here’s a simple example:
a = 100 b = 5 c = a // b print(c)
In the example above, we have two variables a and b each with a value of 100 and 5 respectively. Then, we divide the value of variable a by variable b using the // operator and store the result in the variable c. Finally, we print the result using the print function.
Modulus
In addition to division, Python also supports modulo or remainder operation. This operation is done using the % operator. Here is an example:
a = 100 b = 7 c = a % b print(c)
In the example above, we have two variables a and b each with a value of 100 and 7 respectively. Then, we calculate the remainder of the division of variable a by variable b using the % operator and store the result in the variable c. Finally, we print the result using the print function.
Exponentiation
Python also supports exponentiation operation. Exponentiation is done using the ** operator. Here is an example:
a = 2 b = 3 c = a ** b print(c)
In the example above, we have two variables, namely a and a , each with a value of 2 and 3 respectively. Then we calculate the result of raising the variable a to the power of variable b and store the result in variable c. Finally, we print the result using the print function.
Order of Operations
Now we have seen various types of arithmetic operations available in Python. However, when there are multiple operations in a single line of code, we must pay attention to the order of operations.
Python follows the standard mathematical order of operations, which is exponentiation, multiplication/division/floor division/modulus, and addition/subtraction. However, if you want to change the order of operations, you can use parentheses to clarify which should be calculated first. Here is a simple example:
1. Let’s say we want to calculate the following expression:
3 + 4 * 5
According to the order of operations, we need to perform the multiplication operation first, and then the addition operation. So, the correct result would be:
3 + 4 * 5 = 3 + 20 = 23
However, if we want to change the order of operations, we can use parentheses to clarify which operation should be computed first. For example, if we want to perform the addition operation first, we can write the expression as:
(3 + 4) * 5
Now, the expression inside the parentheses will be computed first, and then the multiplication operation will be performed. So, the result would be:
(3 + 4) * 5 = 7 * 5 = 35
Here’s the code to demonstrate this:
# Example of order of operations # First example: multiplication before addition result1 = 3 + 4 * 5 print("Result 1:", result1) # Output: Result 1: 23 # Second example: addition before multiplication using parentheses result2 = (3 + 4) * 5 print("Result 2:", result2) # Output: Result 2: 35
2. Let’s say we want to calculate the result of the following expression:
2 + 3 * 4
According to the order of operations, we should perform the multiplication first, and then the addition:
2 + 3 * 4 = 2 + 12 = 14
However, if we want to change the order of operations and perform the addition first, we can use parentheses:
(2 + 3) * 4 = 5 * 4 = 20
As you can see, by using parentheses, we can control the order of operations and get the result we want.
Here is an example code that demonstrates the order of operations:
# exponentiation a = 2 ** 3 print(a) # output: 8 # multiplication and division b = 10 / 5 * 2 print(b) # output: 4.0 c = 10 // 3 * 2 print(c) # output: 6 d = 10 % 3 * 2 print(d) # output: 4 # addition and subtraction e = 2 + 3 * 4 print(e) # output: 14 f = (2 + 3) * 4 print(f) # output: 20
Summary:
Addition, Subtraction, Multiplication, Division
The most basic arithmetic operations are addition, subtraction, multiplication, and division. In Python, the operators for these operations are as follows:
Addition: + Subtraction: – Multiplication: * Division: /
Example of using operators in Python code:
a = 10 b = 5
c = a + b d = a – b e = a * b f = a / b
print(c) # Output: 15 print(d) # Output: 5 print(e) # Output: 50 print(f) # Output: 2.0
In the example above, we use the + operator to add variables a and b, the – operator to subtract variables a and b, the * operator to multiply variables a and b, and the / operator to divide variables a and b.
Note that the / operator will produce a decimal number if one or both operands are floats. If both operands are integers, the / operator will produce a float. If you want the division result to always be an integer, you can use the // operator.
Example of using the // operator:
a = 10 b = 3
c = a // b
print(c) # Output: 3
In the example above, the result of dividing a by b is 3.33333, but since we use the // operator, the result is rounded down to 3.
Modulus
The modulus operator (%) is used to generate the remainder of the division of two operands. An example of using the modulus operator:
a = 10 b = 3
c = a % b
print(c) # Output: 1
In the above example, we generate the remainder of the division of a and b, which is 1.
The modulus operator is often used in testing numbers. For example, to check whether a number x is odd or even, we can use the following code:
x = 5
if x % 2 == 0: print(“x is an even number”) else: print(“x is an odd number”)
In the above example, we check whether the remainder of x divided by 2 is equal to 0.
Exponentiation
The exponentiation operator (**) is used to generate the result of raising one operand to the power of another. An example of using the exponentiation operator:
a = 2 b = 3
c = a ** b
print(c) # Output: 8
In the above example, we generate the result of raising a to the power of b, which is 8.
Order of Arithmetic Operations
The order of arithmetic operations in Python follows the BODMAS (Brackets, Order, Division, Multiplication, Addition, Subtraction) rule. This means that Python will perform operations within brackets first, then division and multiplication, and finally addition and subtraction. An example of using the order of arithmetic operations:
a = 10 b = 5 c = 2
d = a + b * c
print(d) # Output: 20
In the above example, we use the * operator to multiply variables b and c, then use the + operator to add variable a and the result of multiplying b and c.
If we want to change the order of arithmetic operations, we can use brackets. For example:
a = 10 b = 5 c = 2
d = (a + b) * c
print(d) # Output: 30
In the above example, we use brackets to add variables a and b first, then multiply the result by variable c.
Conclusion
Python has many operators for arithmetic operations, such as addition, subtraction, multiplication, division, modulus, and exponentiation. The order of arithmetic operations follows the BODMAS rule. When using operators, it is important to pay attention to the data type of the operands, as some operators will generate different data types.
The code examples mentioned above are just simple examples of using arithmetic operators in Python. You can try combining several operators in one code to solve more complex problems. We hope this article can help you learn Python and master arithmetic operations in this programming language.
Thank you for reading this article and see you again in another interesting article.