Python Arithmetic Operators

Operators are one of the most important aspect of programming language. The operators give the ability to do various types of computation in programming languages.


The arithmetic operator is the most common and basic type of operators you will come across in programming languages. Python also supports the basic arithmetic operators and some new operators.

Here is the list of common arithmetic operators in python.

%Remainder (Modulus)

Special arithmetic operators in python.


Now we will see some examples of each other and discuss the properties.

Addition Operator (+)

The addition operator will add two numbers and display the result of the expression.

num1 = 20
num2 = 23
result = num1 + num2 

The output of the program is 43. See below.

============== RESTART: C:/Python_projects/ ==============

The python will add floating point number and integer and result will be float. This is because the floating point representation always take more memory bits than integers.

num3 = 345
num4 = 67.255
result2 = num3 + num4
print("The result is",result2)

The output of the above program is 412.255. See output below.

=========== RESTART: C:/Python_projects/ ===========

The addition operator is also good for strings. It can be used to concatenate two strings. For example.

str1 = "Demo "
str2 = "Program"
result3 = str1 + str2
print("This is a",result3)

The above code will add two strings into a common string and display the output.

=============== RESTART: C:/Python_projects/ ===============
This is a Demo Program

Subtraction Operator (-)

The subtraction operator also known as minus. It will subtract from first variable, a quantity equal to the second variable.

num5 = 200
num6 = 50
result4 = num5 - num6 
print("The final value is",result4)

A quantity of 50 which is num6 will be subtracted from num5 and output is displayed. See the output below.

============ RESTART: C:/Python_projects/ ============
The final value is 150

Multiplication Operator (*)

The multiplication operator will multiply a number with another number to get a final value.

num7 = 10
num8 = 22
result5 = num7 * num8 
print("The product is", result5)

The first number is multiplied with the second number to produce the product. Then the program output the result.

=========== RESTART: C:/Python_projects/ ===========
The product is 220

Division Operator(/)

The output of division operator is always floating point number. The operator divides first number with the second number and return the quotient value.

For example.

dividend = 2300
divisor = 100
result6 = dividend/divisor
print("The quotient is", result6)

The output is in floating point as follows.

============== RESTART: C:/Python_projects/ ==============
The quotient is 23.0

Modulus Operator (%)

The modulus operator will give remainder of a division. It is useful in some mathematical expression where we need to check the class of number( just by checking the number we can decide the class).

For example.

number1 = 23
number2 = 5
remainder_value = number1 % number2
print("The remainder is", remainder_value)

The output of the above is 3 which is remainder of the division 23/5. See the output below.

============= RESTART: C:/Python_projects/ =============
The remainder is 3

Special Operators

The two special operators are floor and the exponential. In many programming languages like C/C++ the exponential and floor are part of the math library. However, despite having a math module, python created these two operators due to the popularity and usage of these operator in programming world.

Floor Operator (//)

Since, the division of python is floating point number, sometimes you need an integer output. The floor operator will result the lowest possible integer value of a division. Take for example, 23/5 which will return 4 as the result, instead of 4.6.

number_1 = 23
number_2 = 5
quotient = 23/5
result_floored = 23 // 5

The output of the above is given below. Note the difference between quotient and the floor.

============== RESTART: C:/Python_projects/ ==============

Exponential (**)

Exponential are frequently used in the both mathematics and computer programming. Exponents have two values – integer part and power part. An integer is raised to power of another number. For example,

23  \hspace{3px} means \hspace{3px} 2 * 2 * 2 = 8
number = 2
power = 4
expo = number ** power
print("The output is", expo)

The output is equal to 2 raised to power of 4 which is 16. See answer below.

================= RESTART: C:\Python_projects\ =================
The output is 16

Arithmetic Operator With Mixed Data Types

Now, you are familiar with the data types and arithmetic operators. The arithmetic operators are naturally for numbers. However, you can also use them with certain combination of other data types.

Number With String

out1 = "Jet Lee" * 5

The above code will not give error but print following.

====== RESTART: C:\Python_projects\ ======
Jet LeeJet LeeJet LeeJet LeeJet Lee

Number With Boolean

You can even multiply with a Boolean value and number because Boolean is 1 or 0 when converted to numbers. Therefore,

out2 = True * 5

The output will be as follows.

============= RESTART: C:/Python_projects/ =============

You may try different combination and will find that the only combination that does not work is string * float.

out3 = "Hello" * 3.3

The output is not printed, but you will get an error.

 RESTART: D:/Users/username/AppData/Local/Programs/Python/Python37-32/ 
Traceback (most recent call last):
  File "D:/Users/username/AppData/Local/Programs/Python/Python37-32/", line 1, in <module>
    out3 = "Hello" * 3.3
TypeError: can't multiply sequence by non-int of type 'float'

In the next few articles, we will talk more about relational operators and Boolean operators in detail.


Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.