Python Arithmetic Operators

When a software program deals with values there is often a need to use these values for further calculations and this is when we find it important to use operators. Arithmetic operators are required for mathematical calculations.

Addition

‘+’ operator is used for performing addition of two values. In the following example you will see that that there are two variables defined:  val1 = 20, and val2 = 5. The two values are added and the result is assigned to val3.

val1 = 20
val2 = 5
print('val1 = ' + str(val1) + ' & val2 = ' + str(val2))
print('addition : val1 + val2 = ')
val3 = val1 + val2
print (str(val3))

Download the code Run the code

Subtraction

Subtraction of two values is carried out using the ‘-‘ operator. In following example you will again see two variables – val1 and val2, the value of val2 is subtracted from the value of val1 and the result is assigned to val3.

val1 = 20
val2 = 5
print ('val1 = ' + str(val1) + ' & val2 = ' + str(val2))
print ('Subraction : val1 - val2 = ')
val3 = val1 - val2
print (str(val3))

Download the code Run the code

Multiplication

Multiplication of two values is carried out using ‘*’ operator.

val1=20
val2=5
print('val1 = '+str(val1)+' & val2 = '+str(val2))
print('Multiplication : val1 * val2 = ')
val3= val1 * val2
print(str(val3))

Download the code Run the code

Division

Division of two values is done using ‘/’ operator.  In the following example value of val1 is deivided by the value of val2 and the result, which is the quotient obtained is assigned to val3.

val1=20
val2=5
print('val1 = '+str(val1)+' & val2 = '+str(val2))
print('Division : val1/val2 = ')
val3= val1/val2
print(str(val3))

Download the code Run the code

Modulus

Modulus can be found using ‘%’ operator. While division operator ‘/’ provides quotient as the final result, the modulus is used to divide and then provide the remainder of the division. So, in the following example, val1 is divided by val2 and the remainder is assigned to val3.

val1=20
val2=5
print('val1 = '+str(val1)+' & val2 = '+str(val2))
print('Modulus : val1%val2 = ')
val3= val1%val2
print(str(val3))

Download the code Run the code

Exponent

Exponential values can be found using ‘**’ operator. when we say val1**val2, we are raising val1 to the power of val2. So, in the following example val1**val2 = 20**5= 20*20*20*20*20.

val1=20
val2=5
print('Exponent : val1**val2 = ')
val3= val1**val2
print(str(val3))
print('***')

Download the code Run the code

Floor Value

Floor Value can be found using ‘//’ operator.  ‘/’ operator does floating point division and you get the result in decimal point which is not the case with floor division, also known as integer division.  So, if you divide 10/3=3.333333333 where as 10//3=3.

val1=20
val2=5
print('Floor Value : val1//val2 = ')
val3= val1//val2
print(str(val3))

Download the code Run the code

<<< Python variables and typesPython Lists – basics: define, access, assign, delete, print, concatenate >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .