Arithmetic operators are also mathematical operators used to perform mathematical operations on numbers, such as addition, subtraction, multiplication, and division. The following table lists all the basic arithmetic operators supported by Python.
Arithmetic Operators | Explanation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division (same rules in mathematics) |
// | Division but retain only the integer part of the quotient |
% | Take the remainder of the division |
** | Power operation which returns a to the power of b |
Let’s continue to explain each arithmetic operator in detail.
The addition operator is very simple, and like the rules in mathematics, look at the following code:
m = 12 n = 56 sum_1 = m + n x = 5.2 y = 35.3 sum_2 = x + y print("sum_1=%d, sum_2=%.2f" % (sum_1, sum_2) )
The output is:
sum_1=68, sum_2=40.50
When + is used for numbers, it means addition, but when + is used for strings, it also has the function of concatenating strings (concatenating two strings into one), please see the code:
name = "Free Learning Points" age = 8 info = name + " is " + str (age) + " years old." print (info)
The output is:
Free Learning Points is 8 years old.
The str () function is used to convert an integer age to a string.
The subtraction operation is also the same as the rules in mathematics, please see the code:
d = 35 m = -d x = -85.7 y = -x print(m, ",", y)
The output is:
-35 , 85.7
Negative number:
In addition to being used for subtraction, it can also be used for negative operations (positive numbers become negative numbers, negative numbers become positive numbers), please see the following code:
d = 85 d_neg = -d f = -73.2 f_neg = -f print(d_neg, ",", f_neg)
The output is:
-85 , 73.2
Note that using + alone is invalid and does not change the value of the number, for example:
d = 85 m = +d x = -73.2 y = +x print(m, ",", y)
The output is:
-85 , 73.2
Multiplication is also the same as the rules in mathematics, please see the code:
d = 3 * 26 f = 32.6 * 3 print(d, ",", f)
The output is:
78 , 97.80000000000001
In addition to being used for multiplication, it can also be used to repeat strings, that is, to concatenate n identical strings, please see the code:
str1 = "welcome" print(str1 * 3)
The output is:
welcomewelcomewelcome
Python supports the two division operators / and //, but there are differences between them:
a. / Indicates ordinary division, and the result calculated using it is the same as that in mathematics.
b. // means divisible, only the integer part of the result is retained, the decimal part is discarded; note that the decimal part is directly discarded, not rounded.
For example:
print ("27/5 =", 27/5) #Normal mathematics division print ("27 // 5 =", 27//5) #Integer can be divided but with remainder print ("27.0 // 5 =", 27.0//5) print ("35/5 =", 35/5) #Normal mathematics division print ("35 // 5 =", 35//5) print ("35.0 // 5 =", 35.0//5) print ("12.3 / 2.5 =", 12.3/2.5) #Decimal division print ("12.3 // 2.5 =", 12.3//2.5) #Decimal division
The output is:
27/5 = 5.4
27 // 5 = 5
27.0 // 5 = 5.0
35/5 = 7.0
35 // 5 = 7
35.0 // 5 = 7.0
12.3 / 2.5 = 4.92
12.3 // 2.5 = 4.0
It can be found from the above results:
The result of / is always a decimal, regardless of whether it can be divided, or whether it is an integer or a decimal. When a decimal is involved in the operation, // the result is a decimal, otherwise it is an integer.
should be noted that the divisor cannot always be 0, and it is meaningless to divide by 0, which will cause ZeroDivisionError. In some programming languages, the result of dividing by 0 is infinity (both positive and negative infinity).
The Python% operator is used to find the remainder when two numbers are divided, including integers and decimals. Python divides the first number by the second number to get the quotient of an integer, and the remaining value is the remainder. For decimals, the result of the remainder is usually a decimal.
Note that the essence of the remainder operation is division, so the second number cannot be 0, otherwise it will cause a ZeroDivisionError error.
For example:
print ("----- Integer remainder -----") print ("25% 3=", 25% 3) print ("-25% 3 =", -25% 3) print ("25% -3 =", 25% -3) print ("-25% -3 =", -25% -3)
The output is:
27/5 = 5.4
27 // 5 = 5
27.0 // 5 = 5.0
35/5 = 7.0
35 // 5 = 7
35.0 // 5 = 7.0
12.3 / 2.5 = 4.92
12.3 // 2.5 = 4.0
print ("----- decimal remainder -----") print ("9.7% 2.3 =", 9.7% 2.3) print ("-9.7% 2.3 =", -9.7% 2.3) print ("9.7% -2.3 =", 9.7% -2.3) print ("-9.7% -2.3 =", -9.7% -2.3)
The output is:
----- decimal remainder -----
9.7% 2.3 = 0.5
-9.7% 2.3 = 1.7999999999999998
9.7% -2.3 = -1.7999999999999998
-9.7% -2.3 = -0.5
print ("--- Integer and decimal operations ---") print ("22.7% 6 =", 22.7% 6) print ("22% 7.5 =", 22% 7.5) print ("22.7% -6 =", 22.7% -6) print ("-22.7% 6.5 =", -22.7% 6.5) print ("-22.7% -6.5 =", -22.7% -6.5)
The output is:
--- Integer and decimal operations ---
22.7% 6 = 4.699999999999999
22% 7.5 = 7.0
22.7% -6 = -1.3000000000000007
-22.7% 6.5 = 3.3000000000000007
-22.7% -6.5 = -3.1999999999999993
Two points can be found from the results:
a. The result of the remainder will be negative only when the second number is negative. In other words, the sign of the remainder is not related to the first number, it is only determined by the second number.
b. When the numbers on both sides are integers, the result of the remainder is also an integer; but if one number is a decimal, the result of the remainder is a decimal.
The Python ** operator is used to find an x to the power of y, which is the power operator. Since the square is the inverse of the power, the square root can also be implemented indirectly using the ** operator.
For example:
print ('---- Power operation ----') print ('2 ** 4 =', 2 ** 4) print ('3 ** 6 =', 3 ** 6)
The output is:
---- Power operation ----
2 ** 4 = 16
3 ** 6 = 729
print ('---- Square root operation ----') print ('71 ** (1/4) = ', 71 ** (1/4)) print ('30 ** (1/5) = ', 30 ** (1/5))
The output is:
---- Square root operation ----
71 ** (1/4) = 2.9027831081870996
30 ** (1/5) = 1.97435048583482
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises