Arithmetic operators in Python

In Python there are the usual arithmetic operators, as in most other programming languages, namely addition, subtraction, multiplication, division and the remainder operator (modulo). In addition, there are the one-digit operators for positive and negative sign as well as operators for exponential (power) calculation and floor division. The arithmetic operators expect numeric operands and return a numeric return value.

OperatorDescription
+Addition / sign
Subtraction / sign
*Multiplication
/Division (float)
//Division (floor)
%Rest (modulo)
**Exponent (power)

Code Example

a, b, c, z = 6, 4, 8, 0

print("Starting values")
print("a = " + str(a) + "\nb = " + str(b) + "\nc = " + str(c) + "\nz = " + str(z))

print("Calculations")
z = a / b
print("z = " + str(a) + " / " + str(b) + " = " + str(z))

z = a // b
print("z = " + str(a) + " // " + str(b) + " = " + str(z))

z = a ** b
print("z = " + str(a) + " ** " + str(b) + " = " + str(z))

a = z - a * b
print("a = " + str(a))
Code Explanation

The code defines 4 variables a, b, c, and z with values 6, 4, 8, and 0 respectively.

The first section of code prints the starting values of these variables using string concatenation.

The next section of code performs a few calculations and updates the value of the z variable. The first calculation, z = a / b, divides a by b and assigns the result to z. The second calculation, z = a // b, performs integer division of a by b and assigns the result to z. The third calculation, z = a ** b, calculates the power of a raised to b and assigns the result to z.

The final calculation, a = z - a * b, calculates the expression on the right-hand side and assigns the result to the a variable.

Output
Starting values
a = 6
b = 4
c = 8
z = 0
Calculations
z = 6 / 4 = 1.5
z = 6 // 4 = 1
z = 6 ** 4 = 1296
a = 1272