Monday, June 15, 2015

Subtraction in Python - Python Numbers - Python Tutorial

Subtraction in PythonSubtraction in Python


In the previous tutorial, we covered addition which was a very simple tutorial. In this tutorial, we will look at subtraction in Python which is another very simple subject. Subtraction works as expect in Python as simple as it was in grade school.

To perform subtraction, in Python we use the minus(-) symbol. In this tutorial, we will be doing all our code in the Python Interpreter. To follow along open your Python interpreter and try some of the examples below.


Integer Subtraction In Python


The following are just basic examples of subtracting Python integers.


#Integer Subtraction in Python

>>> 8 - 4
4
>>> 345 - 67
278

Float Subtraction in Python


The following are just basic examples of subtracting Python floats or floating point numbers.


#Float Subtraction in Python

#simple float subtraction
>>> 6.7 -98
-91.3
>>> 67.43 - 98.76
-31.33

#fixing float accuracy
>>> 8.8 - 1.5
7.300000000000001

#default round function no numbers after the decimal
>>> round(8.8 - 1.5)
7

#fix for one number after the decimal
>>> round(8.8 - 1.5, 1)
7.3

Now that you have seen how easy subtraction in Python you can now simply add and subtract numbers using your Python code.  If you have any questions about subtraction in Python leave a comment below and we will do our best to help you out.



1 comment: