Practice With Arithmetic Operators

Practice these programming examples to internalize these concepts.

3. Addition and Subtraction

In Python, addition and subtraction operators perform just as they do in mathematics. In fact, you can use the Python programming language as a calculator.

Let's look at some examples, starting with integers:

print(1 + 5)

Output:

6


Instead of passing integers directly into the print statement, we can initialize variables to stand for integer values:

a = 88
b = 103
print(a + b)

Output:

191


Because integers can be both positive and negative numbers (and 0 too), we can add a negative number with a positive number:

c= -36
d = 25
print(c + d)

Output:

-11


Addition will behave similarly with floats:

e = 5.5
f = 2.5
print(e + f)

Output:

8.0

Because we added two floats together, Python returned a float value with a decimal place.


The syntax for subtraction is the same as for addition, except you'll change your operator from the plus sign (+) to the minus sign (-):

g = 75.67
h = 32
print(g - h)

Output:

43.67

Here, we subtracted an integer from a float. Python will return a float if at least one of the numbers involved in an equation is a float.