How to Use Numbers and Math Operator in Python Programming Language

Posted on

Introduction

In this article, the main content is about the basic part of python programming language. That part is focusing on numbers. Furthermore it will show how is the different between integer and float data type throughout simple operation of addition, subtraction, multiplication, division with the usage of several math operators. The execution is actually quite simple. Just simply access the python interpreter in the command line interface. The example in this article is using Command Prompt in the Windows operating system as the command line interface as follows :

Microsoft Windows [Version 10.0.18362.1016]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\Personal>python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Addition using Addition Operator

The first one is the addition using addition operator. The operator for the addition is ‘+’ and it will sum up every number along with the addition operator. The following is the example of the addition operation using several numbers :

>>>  1 + 1
2
>>>

The above example uses two positive integer numbers. It also gives result a positive integer number.

>>> -2 + 3
1

Another example above uses one negative integer number and another one is a positive integer number but has a larger value from the negative one. So, the result will be a positive integer number.

>>> -5 + 3
-2

The above is in the opposite example from the previous one. The negative integer number is larger in value compared to the positive integer number. The result is a negative integer number.

>>> -4 + -7
-11

The last example are using two negative integer number. The addition operation will sum up the number but resulting in a negative integer number. On the other hand, if the addition process is using just only one float data type number, the result will be also in a float data type number as follows :

>>> 5 + 5.0
10.0
>>> 

 

Subtraction using Subtraction Operator

The second one is the subtraction using subtraction operator. The operator for the subtraction process is ‘-’ and it will subtract every number with the sequence order from the left number to the right one along with the subtraction operator. The following is the example of the subtraction operation using several numbers :

>>> 2 - 2
0

Subtracting two positive integer number with the same value resulting zero as in the output above.

>>> 5 - 2
3

Subtracting two positive integer number where the left one has a larger value compared to the right one considering only two numbers involved will result in a positive integer number.

>>> 3 - 9
-6

On the other hand, subtracting two positive integer numbers where the left one is smaller than the right one will result in a negative integer number. The same thing happen with the subtracting process using only one float data type number. The result will also be in a float data type number as follows :

>>> 10.0 - 3
7.0
>>> 7 - 9.0
-2.0

 

Multiplication using Multiplication Operator

The third one is the multiplication operator. It will mutiplicate the numbers along the multiplication operator. Actually, the multiplication operator is ‘*’, the following is an example of it :

SyntaxError: invalid syntax
>>> 3 x 3
File "<stdin>", line 1
3 x 3
^
SyntaxError: invalid syntax
>>> 3 * 3
9

It will resulting on a positive integer number as it exist in the above output.

 

Division using Division Operator

The fourth one is the division operator. It will perform division upon the number involved in the process. But using two positive integers will not result in an integer data type. It ends with a float data type as in the following execution :

>>> 16 / 4
4.0
>>>

Module Operator

Another operator is the modulo operator.

>>> 12 % 4
0
>>>

In python programming language, the division operation will automatically change the type of the result into a float data type.

 

Leave a Reply